Homing missiles in Unity

Hal Brooks
4 min readJul 20, 2021

--

Objective: Add powerup allowing the player to fire two homing missiles that target the nearest enemy.

After collecting powerup, player has missiles that target nearest enemy.

Adding homing missiles is a complex task, requiring a powerup for the player to get the missiles, visual effects for the missiles, code for the player to fire the missile and identify the enemy, and code for the missile move to and destroy the target once fired. The initial step is to create the missile.png file, using PowerPoint®. Drag this file into the Unity asset folder and the Texture Type set to Sprite (2D & UI).

Missile.png

This sprite is the basis for the powerup animation shown below, also created using PowerPoint. The png files are dragged into the Unity asset folder and the Texture Type set to Sprite (2D & UI).

Sprites for Missile_Powerup animation.

The Missile_Powerup prefab is made by dragging the powerup6_0000.png into the hierarchy window with Sorting Layer set to Foreground. Add a Circle Collider 2D with Is Trigger and Rigidbody 2D with gravity 0.

Missile_Powerup prefab

Add the C# script powerup, adding a case 5 as shown below to the switch statement within the OnTriggerEnter2D function that detects collision with the player. Assign the Powerup ID for the Missile_Powerup to 5 and assign power_up_sound to Powerup Audio in the inspector.

New case in switch statement within Powerup script

Create an animation, Missile_Powerup_anim, and drag the powerup6 sprite sequence above into the animation window. Drag the Missile_powerup into the Unity Asset Prefabs folder and destroy the copy in hierarchy window.

In the SpawnManager script increase the int powerupID to Random.Range(0, 6), and add the new Missile_Powerup the Powerups Prefab array as element 5, see below.

The Player script needs the following variables.

required missile variables in Player script

Create a Missile game object by dragging the Missile sprite into the hierarchy. Add a Circle Collider 2D with Is Trigger and Rigidbody 2D with gravity 0. Add a copy of the Player Thruster to the Missile, reducing the X scale to 0.2. Set Sorting Layer of Sprite Renderers to Foreground, order in layer 4. Create a Missile C# script and assign to the game object. Drag the Missile game object into the Unity Asset Prefabs folder and destroy the copy in hierarchy window. Assign the Missile to Missile Prefab in the Player script using the inspector window.

On collision with the Missile powerup the AddMissiles() function in the Player script adds two missiles onto the rails of the player’s ship, as shown below.

Missile function in Player script

To fire a missile, the player presses the F key, which activates one missile, as shown below.

C# code to fire missile within void Update() function in Player script.

The code above requires the FindClosestEnemy() function shown below. This code searches through the list of enemies to find the nearest enemy and returns the enemy game object.

FindClosestEnemy() function in Player script.

The FireMissile() function within the Missile script, see below, is called by the Player script on being fired, which passes the target game object. The FireMissile() function activates the missile, _missileFired = true, and activates the thruster animation. Void Update() contains the physics to move the missile to the target when active. If the target is destroyed the missile is destroyed.

Missile Script.

Homing missiles created by the SpawnManager require complex interactions between the Missile_Powerup, Player, Missile and Enemy game objects.

PowerPoint is a registered trademark of Microsoft Corporation.

--

--

No responses yet