Fire-And-Forget: Homing Missiles (Game Dev day 38)

Jack Leavey
4 min readSep 25, 2021

--

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.

Lasers just move up.

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 made several sprites with different rotations so I can animate the missile later.

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.

In game Appearance

Next I turned the Missile into a prefab so I can reference it easily.

Just like the laser, the missile should be a prefab as we will be instantiating many of them.

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.

Added to the Player Script.

Next, I added a statement to the FireLaser method that would instantiate the Missile prefab if the MissileActive status is set to true.

Still in Player Script.

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

Yet another Audio Source.

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.

This is the Missile_Powerup, not the actual Missile itself.

I added this Powerup to the Powerup.cs array.

See my previous articles for a breakdown of this array.

Onward to the Spawn Manager GameObject, I added a new element (5) to the Inspector and assigned the Missile_Powerup there as well.

Our 6th powerup.

In the SpawnManager.cs script, I increased the range of values to include the new powerup.

Line 54, the second integer (was previously 5 and is now 6).

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.

The speed of the missile itself, and the location of its target.

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.

Private variables are assigned in this function that are not used anywhere else.

Next, ChaseTarget instructs the missile to move towards the enemy in the fastest way possible (setting a pseudo collision course).

See the Unity Manual for a breakdown of Mathf and Euler.

Next, in Void Start the TrackTarget method is called.

Followed by Void Update, which runs each frame to tell the missiles what to do.

If the enemy the missile was targeting is destroyed, it will now update to the nearest alive enemy and move towards them instead.

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.

This if statement is nearly identical to the one applied to the laser previously.

Now let’s see it in action!

The missiles will seek the nearest enemy, then swap if their intended target is destroyed enroute.

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!

--

--

Jack Leavey
Jack Leavey

Written by Jack Leavey

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!

No responses yet