Moving Enemies from side to side

Niraj Karki
2 min readMay 31, 2021

--

Day 40- Making 2D Space Shooter Game

Objective: Make our enemy be able to spawn on Right or left and move towards opposite direction.

We are going to start off by creating a script that is going to decide whether to spawn the enemy on the top, right or left.

Now in the enemy script we are going to check the position of the script to determine the position of enemy at the time of their instantiation.

We only checked for two positions because if these two are not true then by default the third one will be true as we have instantiate condition for three position only.

Now we check the bool variables and according to it we make our enemy move. i.e. Move enemy to left if instantiated on right and vice versa as well as move enemy down if instantiated on top.

Now, we are going to set the condition to reposition the enemy when it goes out of bound of the game screen.

And you have now successfully created a game where the enemy appears randomly from either Top or Right or Left and it reposition itself when outside bounds.

--

--