Part 1 — Fire & Forget: Creating a Smart Homing Missile for your Unity Game Project!

Objective: Implement a new weapon to your player’s arsenal in your Unity 2D Game Project. Make it smart. It should be able to detect, track and destroy any enemy game objects present in the game scene.

Part 1 — Creating the 2D Sprite and setting up the logic in the PlayerScript and the Enemy scripts.

Thus far, our little spaceship can only defend itself with its dual laser canons. Sure, he sometimes collects a power-up giving him a temporary ability to fire a triple-shot laser spread in an arc. Now, let’s beef things up a bit and give him a smarter weapon: a homing missile to seek out those bad alien vessels!

I should have started with a simple prototype game object, but I’m really a visual type of guy, so I spent some time looking for a suitable “missile” asset. I finally found what I liked in PNGHUT.COM, a Rocket Propelled Grenade (RPG) launcher with an associated RPG rocket png.

I extracted the RPG png, turned it into a 2D Sprite, and created a Prefab with the following parameters (see below). Note the Tag is cut off in the image and should read “PlayerHomingMissile”. I added a 2D Rigidbody, and a 2D Capsule Collider edited to capture the “warhead” portion of the “missile”.

I then duplicated the Player’s thrusters, adjusted the scale and color palette, and set it as a child of the Player’s Homing Missile.

Now that your missile Prefab is complete, let’s jump into the basic setup in the Player and Enemy scripts…

I began by adding the following three private variables: a Game Object _playerHomingMissilePrefab to hold my missile Prefab. The bool _isPlayerHomingMissilesActivate lets us know if the Player is currently armed with homing missiles (this will be flipped to True when the Player collects the homing missile power-up or when the logic determines he has missiles remaining). Finally, an int _homingMissileCount to keep track of the remaining missiles in its arsenal (think ammo count).

PlayerScript script ~ setting up some new variable.

I then added in the Update() method a couple of IF statements. The first looks for the “M” key to be pressed (“M” for Missiles). When this happens, the logic looks to see if the Player has collected or is currently armed with missiles. Only then will the Player be allowed to fire a missile by calling the FireHomingMissile() method. The second IF statement simply checks to see if the “missile count” is depleted, and if so, flips the bool back to False.

PlayerScript script ~ looking for the “M” key instantiate the missile, based on a couple of logic gates confirming they are in our inventory and active.

FireHomingMissile() is the method used to instantiate the Homing Missile Prefab at the player’s position, with a slight offset for visual aesthetics. We reduce our Homing Missile inventory by reducing _homingMissileCount by 1 and finally update the UI Text to reflect that new value.

PlayerScript script ~ instantiate the missile, reduce the inventory, and update the UI.

We also need to ensure collisions between the Homing Missile and the Enemy ships are detected. This is done by adding the TagPlayerHomingMissiles” to the OnTriggerEnter2D() method of the Enemy script.

Enemy script ~ looking for collisions between the Enemy and the Homing Missiles.

The PlayerHomingMissiles() method is called by the PowerUps script whenever a Homing Missiles Power Up has been collected by the Player. This increases the Player’s inventory by 5 missiles, up to a maximum of 25. It also calls the UpdateHomingMissileCount() in the UI Manager to update the missile count variable (_homingMissileCount) and associated UI text.

PlayerScript script ~ adding missiles to the inventory (maximum of 25), updating the UI, and activating the Missiles.

The Player starts the game with no Homing Missiles. Once the first Homing Missiles Power Up has been collected, this bool value flips from False to True, indicating that the player now has missiles in his inventory.

PlayerScript script ~ setting the logic which indicates that the Player is equipped with Homing Missiles.

Moving briefly onto the UI Manager, we need to set up our UI elements so the Player can visualize in the UI the quantity of Homing Missiles held.

UIManager script ~ adding a private Text Mesh Pro text field to represent the quantity of remaining Homing Missiles.

From the Hierarchy, select the Canvas, add a UI TMP Text field and rename it:

“Homing_Missile_Count_text”.

In the Inspector, drag it into the Homing Missile Count text field.

Now every time a homing missile is fired, the method UpdateHomingmissileCount() is called inside the UIManager script. It converts the int value homingMissileCount into a String and appends it following the “Missiles: ” UI text.

It also adjusts the color of the text, setting either to red or green, based on whether or not missiles are held in inventory.

UI Manager script ~ updating the UI Text to indicate the quantity of Homing Missiles remaining.

This about does it for Part 1. Meet me in Part 2 where I will take you through the steps to create the Homing Missiles collectible Power-Up. We will make some slight adjustments to our SpawnManager and PowerUps scrips as well.

Here’s a teaser 😋 …

Thanks for reading :)

Military member with 33+ years of service, undertaking an apprenticeship with GameDevHQ with the objective of developing solid software engineering skills.