I added a new enemy type to my shooter: The Sin Fighter, named after its sine wave movement pattern. Let’s check it out:

Instead of re-writing (er, re-typing) the code from my basic enemy type, I made the Sin Fighter inherit the basic functionality and variables of the Enemy class.

The Sin Fighter is a variant of Enemy

The Enemy class has a method called CalculateMovement. I had to override this method so the Sin Fighter would move differently. Luckily, C# has a built-in sine function to do the heavy lifting:

Mathf.Sin to the rescue!

We can increase _frequency to make the sin fighter touch the highest and lowest parts of the sin wave more often, creating a more erratic-feeling enemy. Increasing the _magnitude will make those high and low points further away from each other, increasing the amount of space on the screen the Sin Fighter can cover.

I also added this bit of code so the Sin Fighter will reverse direction soon after leaving the side of the screen, returning for more chaos until it’s destroyed!

Math is fun!

An independent Unity Developer.