GameDev : create an Enemy(Part I)!
Our game is a shooter, so we have to create an enemies to shoot it!
Ok, to begin we create a cube, if you want you can change the dimension, I made it a little smaller than Player.
I create a material, red in my case, and attach it to the enemy.
Nothing new for now
Scale it to 0.7 on every axis (if you prefer you can left to 1, 1, 1)
And make it red with new material
Now, we have to instantiate the enemy at run time during the game, right?
So what we need?
Exactly, the prefab!
We transform our enemy into a prefab
Drag and drop the enemy from the hierarchy to the folder, as we can see it turn blue (it means it is now a prefab).
As always we need behavior for our enemy, so we create a new C # script called “Enemy”, then simply drag and drop it into the prefab, so that the enemy in our scene is automatically updated with the script. It’s amazing!
Everything is set, but obviously we need that enemy move or make something!
Open the Enemy script, and write down Pseudo Code!
What do we want?
This is what I want for the enemy in my game: I want him to spawn high, drop at speed “n” and if the player doesn’t destroy him, when it exits the screen his position is reset to the top, but in a RANDOM position X.
We already know the object instantiation process, but what we need is: how to make it respawn in RANDOM POSITION?
Documentation and a little bit of research is our bread and butter, so we type on Google :
and this the documentation about it
Anyway, back to documentation, and this is what we find
In the example we see this
As always, we don’t worry too much to “How work this code in example?”, we ONLY NEED the syntax
If we analyze a little here, just to know:
var position is assigned to a new position, with Vector3 right?
So THIS PART
new Vector3 (Random.Range (-10.0f, 10.0f), 0, Random.Range (-10.0f, 10.0f))
I want a new position, WHERE X IS A RANDOM POSITION FROM -10 TO 10, NOTHING HAPPENS ON Y AND ON Z IS A RANDOM POSITION FROM -10 TO 10.
In our case, we want the same but only on X POSITION. That’s all.
We start with simple thing : first, we need to Instantiate the object at top screen
Press Play and
We did this in the Start method, because we set the position at the start of the game, so ONLY ONCE, and in the UPDATE method we make it move and we update its position during the game. This is the logic of Start and Update.
In the next articles we finish set the Enemy!