Creating a Homing Missile Projectile

With more Power comes more responsibility… Unless you have a Homing Missile, then you can just let it do it’s thing!

Probably one of the most Powerful Projectiles known to the Game-verse, Homing Missiles are a staple in locking down those pesky Enemies that are just too far away or in odd positions.

Let’s make one!

Is it laziness? Or is it just awesome overpowered weapons?

Creating the Pickup

First thing’s first, we need a Pickup to activate the new Projectile type!

I am using the same Ammunition pack used previously to create the Ammo pickup — it’s a free pack in the Unity store!

I just duplicated 3 large bullets to create the pickup:

And attached a Rigidbody 2D (gravity scale = 0), and a BoxCollider 2D (Is Trigger).

Then attached the Powerup script, and set it’s ID to 6:

Within the Powerup script, I updated the switch statement to accept our new Powerup:

In the Player Script

Updating the Player script doesn’t require much, but it starts with creating new variables:

And the creation of some new functions:

Then update our Firing logic to fire our Missile when our bool is active:

The Homing Missile

Now we need to create our Homing Missile so that we can link the Prefab to the Player script and properly fire it!

I just used a single bullet from the same ammunition as the Pickup, and added a Player thruster sprite to it to create the Missile aesthetic:

Then added our Rigidbody 2D and BoxCollider 2D, and created a HomingMissile script (lol, yes I know I spelled ‘Missle’ wrong in the script… I didn’t notice until after everything was done. Whoops!)

HomingMissile script

To start off like any other script, we’ll need some variables to work with:

Then we’ll need to determine our Closest Enemy that we want to track down. This is done by getting an array of the _activeEnemies in the Scene and cycling through to see which one is closest:

And we’ll call this function as soon as our Homing Missile is instantiated; in the Start() function:

But now we need to utilize this new _target. So we’ll create a MoveToTarget() function:

And also create a BoundCheck() incase our Missile goes haywire:

Then check these two functions within Update():

Enemy Interaction

The last thing we need to do is determine what happens when the Homing Missile finally reaches it’s target.

So we’ll create a separate tag for the Missile:

And inside the Enemy script, inside the OnTriggerEnter2D logic, we’ll add some code to destroy things when hitting an object with the Tag “Missle” (oh man, how I HATE spelling mistakes. But sometimes, you gotta laugh at yourself. Sometimes when coding you get streamlined and forget about the small things. Let it be a lesson to all of us! SPELL CHECK :P) :

BOOM! You now have a fancy Enemy-tracking Missile that all your Enemies should fear!

Our game is almost complete… But we have one last thing to implement to make it really feel finished: a Boss enemy! In the next few articles we’ll be covering the creation of a big bad boss!

Audio Engineer turned Unity Game Dev. Will be combining both my skillsets when appropriate, and will be documenting my Unity Dev growth in these Medium Articles