Road to Game Dev: Phoning Home (Part 1)
Now that we’ve made enemies much more dangerous, let’s start putting a bit of power back into the player’s hands. We’re going to look at “vacuum” commands for powerups, and the classic: Homing Missiles.
Basic Vacuum Effects
This part is pretty simple; we want powerups to move towards the player once “C” is pressed. There’s a few pieces of this, but none too major. We want the powerups to recognize when the key is pressed, and we want the powerup to recognize where the player is and move towards them. This can all be handled on the powerup’s own script:
Whenever C is pressed, and as long as it isn’t a hazard (such as a mine), normal movement is now overridden! The new movement looks to see where the player is relative to the current position, and then normalizes that value; this keeps the direction the same, but gives it an overall value of 1, so you can multiply it by the speed you want and have it travel towards the player.
Basic Homing Missiles
Now, let’s apply this same knowledge with a bit more advanced math to create a homing missile that looks a little nicer. There’s a lot that goes into the targeting system here, but we’ll look at that in part 2. For now, let’s make a basic missile script, and expand on the powerup code to make it curve a bit more nicely:
There’s a few other things in the gif above, but this solves the basic movement problems! However, there’s a few bugs that can arise; For example, if the target dies before the missile reaches it. We want enemies to have a way of checking their state, and check that state whenever the missile tries to move. If the target is dead, the missile can explode immediately, or we can force it to continue towards the original destination and explode when it reaches that point.
Not too bad! The hard part is when we have to start actually choosing our targets. More on that soon…