Learn to code by making a 2D space shooter game.

Suleiman Abdullah
3 min readMar 25, 2022

Objective: Implement New Enemy Movement.

First, we need to understand what we need our movement is going to be a zig-zag to do this we need to understand how cos and sin work.

The first thing we need to understand is how to find the sin of an angle, which is opposite over hypotenuse.

This shows if you want to manipulate the y vector you need to use the sin of an angle.

In the second step, we need to understand how cos of an angle work, the formula to find the cos of an angle is adjacent over hypotenuse.

This shows if you want to manipulate x vector use cos.

Since our enemy is moving downward to move the enemy zig zag we need to manipulate the axis, we use Mathf. cos but before we need to create x float variable then assign to Mathf. cos and we pass Time. time to move the Player in real-time.

In the next step, we need to create an int variable for the amplitude to give the enemy the ability how long to move from right to left.

Now we need to multiply this amplitude, to make the enemy move one unit(one unit is equal 1meter ) from left to right while is moving down to create a zig-zag movement.

In the next step, we need to create y and z coordinate and assign them to the transform but remember we need to leave y and z as it is we only need to manipulate the x-axis.

if you define it like this then use spawnManager to spawn enemy random enemies will be cluttering on top of each other.

In the next step, we need to create a float frequency variable and multiply it by Time. time inside Mathf. cos, this specifies how quickly the enemy moves zig zag.

this will have an issue of spawning enemies on top of each other to fix

To fix this do this inside Translator and remove y and z

Let's see the final result.

if you notice an enemy spawned in the same position Center

Fixed solution Result.

Fixed solution result.

--

--

Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development