Enemy Zigzag Made Easy!

addam davis
3 min readJul 12, 2021

--

Objective: Create a method for the enemy to move in a zigzag formation.

Today we are modifying an enemy type to move in a zigzag motion from the top to the bottom of the screen. This may sound intimidating however this method is easy to implicate. We will begin the same way we always do.

Creating variables. We will need to create a variable to represent the enemy’s x axis. When you set a new Vector3, you need to use values for the x, y, and z axis.

If the number value for x is positive, then the direction of movement will be towards the right of the screen. This means that if the value is set to a negative then the direction will be to the left.

If we used a value instead of a variable, changing the value later would result in an equation instead of a replacement of the value.

This means if we used 1, and wanted to change the value to -1, then the acting value would be zero, instead of negative one.

Using a variable, if we change the value from 1, to -1 then the acting value would be -1.

With this in mind we want to use the x values we used earlier to teleport our enemy from one side of the screen to the other and change this acting code in those if statements. We want those x values to change the variable we have for the enemy’s x axis.

Now when the enemy hits the value their direction of movement will be changed to the x axis variable.

This is all there is to it. Your enemy will now move from side to side from the top to the bottom of the screen. As always, don’t be afraid to experiment with your code and I’ll see you in the next tutorial.

--

--