Learn to code by making a 2D space shooter game.
--
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.
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.
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.
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.
To fix this do this inside Translator and remove y and z
Let's see the final result.
Fixed solution Result.