Improving Enemy Movement!
Improving the normal enemy ship movement by adding some smooth sideways movement.
Today’s Objective: Improving the normal enemy movement by adding smooth sideways movement using Sine maths.
The simplest way to add some extra movement into the standard enemy movement, is a smooth sideways back-and-forth motion.
Now, when thinking about how to implement this via code, does the above movement remind you of anything?
That’s right! A Sine wave!
Using a sine wave instead of completely coding my own solution, allows a nice smooth transition between both directions, without any sharp turns.
Opening the Enemy Movement script, I’m adding the new code in next to the downward movement code.
When the ship spawns or respawns, I record its starting X position as “_originX”, which is used as the midpoint for the Sine wave movement.
From the diagram above, “Amplitude” is the “_sineMoveScale” float variable, and “Time” is the Y position (since that continually moves down).