Fire-And-Forget: Homing Missiles (Game Dev day 38)
Objective: Create a powerup that replaces the player’s laser shot with a powerful homing missile.
Objective: Provide the player with a powerful alternate projectile that actively seeks out enemies.
Currently our player’s only means of destroying enemies comes from their laser (or a Triple Shot of that laser). These lasers simply move up the screen and have no logic applied to allow them to change their trajectory.
So let’s make a new Missile projectile that can actively “hunt” enemies independently from the player.
First off, I used an AIM-9 Sidewinder missile for my sprite art.
I then used this art to create a new Gameobject and then added a smaller version of the Player’s thruster to the back end of the missile. The thruster is a child of the missile.
Next I turned the Missile into a prefab so I can reference it easily.
Next, I applied a 2D Box Collider followed by a new script (Missile.cs)
Before tackling the logic for tracking our enemies in the Missile Script, I set up the missile as a projectile that spawns from the player when certain conditions are met.
The Player Object needs to see the prefab, so I created a variable to hold the new prefabbed Missile.
Next, I added a statement to the FireLaser method that would instantiate the Missile prefab if the MissileActive status is set to true.
I also chose to make a different sound effect for when the player fires missiles instead of lasers. I added another audio source to the player and an audio clip with the new sound, and played it whenever a missile was fired (see line 162).
Next, I created a Missile_Powerup prefab. This is the object the player has to pick up to be able to fire missiles. I attached the Powerup.Cs script to it as well.
I added this Powerup to the Powerup.cs array.
Onward to the Spawn Manager GameObject, I added a new element (5) to the Inspector and assigned the Missile_Powerup there as well.
In the SpawnManager.cs script, I increased the range of values to include the new powerup.
Great! So now the powerup can drop, and the player will fire lasers for 5 seconds during the powerup period.
However, the missiles won’t do anything. They won’t even move up the screen as the Missile.cs script is still empty.
Now comes the fun (and complicated) part.
Setting up the Missile script, I assigned a few variables to start out.
Two functions were needed to make the missiles operate nicely:
TrackTarget and ChaseTarget.
TrackTarget runs a scan to identify the nearest enemy to the missile.
Next, ChaseTarget instructs the missile to move towards the enemy in the fastest way possible (setting a pseudo collision course).
Next, in Void Start the TrackTarget method is called.
Followed by Void Update, which runs each frame to tell the missiles what to do.
Now that the missiles move towards the enemy and can be spawned in during the powerup, the final step is to assign what happens when a missile hits an enemy.
Now let’s see it in action!
This idea can be further refined to create some interesting new weapons for the player or the enemy. A simple change would be to have the launcher fire “bursts” of smaller missiles (salvos) to clear a large area of targets. You could also add an explosion to the missile when it connects to a target that could damage other targets!