Improving Enemy Movement!

Improving the normal enemy ship movement by adding some smooth sideways movement.

Vincent Taylor
2 min readJul 22, 2021

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.

Transform.Translate(), which I use for the down movement, takes a direction x speed, so wouldn’t work for my design of getting the Sine value.

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).

The final result, using an amplitude of 0.5f

Looks perfect!

--

--

Vincent Taylor

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.