Wow! We’re coming to the end of our first game project and I’m really excited about the upcoming challenges we’re going to be facing. The first one seems simple enough. Create a new enemy with new behaviors like moving side to side, circling, or coming into play from any angle.
I decided to add 3 more enemies, 1 that swoops halfway down the screen, changes their mind and goes back up and swoops down again from another spot. another enemy that sways from side to side, and an eccentric little enemy that likes to fly in a circle pattern from side to side. Since there’s not going to be many enemies for now, I’m going to stick with the same enemy script and use switch cases and give each enemy an ID like how it was done with the powerups.
There’s not a wave system in yet, so I just implemented the same method of spawning powerups of the spawner randomly choosing from an array of whatever enemy types I put in.
In the enemy code, just like the powerup code, there’s a switch statement where an enemy ship given its own id will fall pachinko style into the machine and depending on its id, the appropriate code will be run. Nothing can be easier!
We’ve covered the first enemy before, but since I’ve refactored the code a little, let’s go over it. It’s actually very simple! The EnemyMovement function merely keeps the enemy moving straight down by passing the direction parameters and enemy speed into the move function. and then checks to see if it’s hit the bottom of the screen. Once it does, it picks a new top position anywhere on top of the screen along the horizontal axis to drop down again. Nothing too complex.
Here’s the Move() function. I could have easily typed in transform.Translate in every enemies move method, but I figured I could just save myself a little bit of typing and putting it into its own function with a few parameters, plus it’s much easier to type Move(direction, speed) than to type out the whole transform command every time!
Speaking of SCIENCE! We’re going to get into a little bit of math. Be not afraid!
The second enemy sways from side to side using sinusoidal movement. To be honest, I was never any good at math and never understood these concepts up until recently thanks to GameDevHQs awesome video on the subject. If you’re interested in this kind of movement, I highly recommend checking the video out. If someone like me can understand it after watching it, anyone can!
Long story short: The MathF.Cos function will ping pong back and forth between -1 and 1 along the x axis, we can take advantage of this behavior by multiplying it against Time.time, we can also multiply it by a frequency to make it faster, and an amplitude to stretch out and widen the movement. Again, check out the video for a great explanation!
Wait? What? There’s more Sin and Cos? Don’t go away yet! I can explain!
This is exactly like the previous enemy except we add the Cosine movement to the Y axis. Rememember how Sin pingpongs back and forth on x? Well, Cosine ping-pongs back and forth on the y! Add them together and you get movement in a circle! I made the mistake for hours of saying the transform.position was equal to those x and y positions over time, and the enemy ship was stuck moving in the center of the screen. It finally dawned on me I need to be adding those positions and sure enough, the enemy went flying in a circular pattern to the left when I combined it with a regular Move() function to compliment it.
There’s a boolean,isMovingRight, that gets randomly changed to true or false at the Start() function:
Yep! I put a switch case in the start function as well to initialize the various enemies. There’s really only one other case, case 2 that initializes the amplitude and frequency of the side to side movements that are given a random range.
Anyway, the numbers in the amplitude and random range for the circling ship were brought about with a lot of trial and error. That is, just me typing in different number combinations and seeing what worked best. a temporary variable called boolNum that gives us a 0 or a 1 which is assigned to a boolean which then figures out whether the circling ship is moving left or right, and if it is moving right, set it on the left side of the screen, and the right side if otherwise.
And with that, we now have a little more variety in our game! Stay tuned as we update our bullet counter!