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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Some things I wish I knew as a young startup dev

how to install rasa-x on ubuntu.

AWS Storage File Gateway Walk Through

Making a static website with C#

How to develop a dApp that displays ETH-USD price feed using the DIA Oracle

CS373 Fall 2021 Entry 12: Justin Lee

Top 3 WebSocket Libraries For Real-Time Apps

Distributed Tensorflow Deploymemt — Part II

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Frank Warman

Frank Warman

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

More from Medium

Switching Character Control in Unity: Part II, Implementation

An animated gif. The window is split. On the left, a camera looks at a tank as it first moves towards the camera. The tank then stops and the turret begins to rotate left and right. The animation finishes with the turret elevating towards the camera. On the right, the same scene is shown from the perspective of a camera following the tank. After the tank finishes moving, the camera switches to one looking just over the turret and rotates with the turret as it first trains, then elevates.

How to format a double value according to culture-specific formatting in Unity C#

Improving Upon Selection Highlighting in Our Game — Part 3

Variables! — The building blocks of programming