Enemies moving side to side

Let’s go over the implementation of adding a new movement of the enemies where they will be moving from side to side instead of straight down.

The basic enemy was added in this article and the look of the enemy was changed in this article.

Open the Enemy script and add a new enum to the bottom outside of the class to be able to set the enemy type. Add two values, one for the new movement and another for the already existing enemy:

Add two bools and the new enum, which will be used to check which direction the enemy should move and determine enemy type. Set a random start direction in the Start function:

Amend the EnemyMovement with a switch statement to make it easier to set enemy type movement. When the enemy reaches either side of the screen, it will swap the bool and start to move in the opposite direction it came from:

Lastly, set the enemy type in the inspector:

Good luck!

Unity / C# Game developer