The essence of Sine Movement has it’s basis in the movement of a circle and a Sine Wave:
Basically it oscillates from top to bottom in an even pattern. (or in our case, left to right)
So we can create a Sine Movement script starting with these variables:
Using the Sine Wave above, these variables can be explained as such:
- CurveSpeed = how fast the oscillation moves up and down
- MoveSpeed = how fast it moves on the X axis
- _curveRadius = the Max and Min height of the oscillation
And using this function will give us our Sine Movement:
- * You can visualize the Sine Movement by implementing the commented out code.
We can now attach the SineMovement script to our Enemy Prefab to give our Enemies their new Sine Movement, which in conjunction with our already downwards movement give us the effect we are looking for!
But we also have to disable this Sine Movement script on the Enemy when they are destroyed, or else their explosion sequence will continue to oscillate, which looks pretty funny.
To do this we create a reference in our Enemy Script to the SineMovement script, and disable it when our Enemy is destroyed:
We’ve successfully added some difficulty to our Enemies, and they now can sometimes dodge our Lasers! It also makes it more challenging to dodge incoming Enemies when they swarm the Player!
In the next article, we’ll look at how to implement a Wave system. Increasing the amount of Enemies spawned each wave!