Part 2— 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 2 — Creating the collectible Power-Up to arm the Player with Homing Missiles.
We spent Part 1 (read it here) setting up the Homing Missile and the basic logic associated with it. We are now able to instantiate it when they are in the player’s inventory, keep track of how many we have left, and adjusted the Enemy script to detect collisions between the Enemy ships and the Homing Missile.
The Homing Missile is not yet moving, but before we get to this phase, let’s set up the collectible Power Up required to give the Player this awesome new weapon.
In order to create the Missiles Power Up, I started by creating an Empty Game Object, renaming it PlayerMissilePowerUp, adding a 2D Collider centered on its mass, and adding the PowerUps script which sets the speed, ID, and associated Audio Clip of the collectible when it is randomly selected and instantiated by the Spawn Manager.
Within this Game Object, I added 5 additional Game Objects as children, each composed of the Homing Missile Sprite, and a RotatingObject script.
This allowed me to stack 5 individual missiles, each rotating in a different direction and speed, but moving globally down the Game Scene as a single collectible Power Up.
The RotatingObject script simply allows a Game Object to rotate along its Z-axis at a speed set by the float _rotationSpeed.
The result is a cluster of Homing Missiles floating around in the emptiness of space.
Looks pretty cool, eh?
😎
Let’s go back to the PowerUps script. Make life easy on ourselves by adding a comment to the _powerUpID private integer, stating that 5 equals the Homing Missiles.
We then need to adjust the Switch statement to capture the possibility that the _powerUpID may equal 5 (case 5:), in which case we would execute the PlayerHomingMissiles() method inside the PlayerScript script.
Now that you’ve added the 5th case to your _powerUpID list, you need to go back to your SpawnManager script and adjust the Random.Range of the SpawnPowerUps() coroutine to allow the int randomPowerUp to possibly equal an integer from 0 to 5 inclusive.
With the SpawnManager selected, remember to set the size of the Player Power Ups to 6 in the Inspector, and drag the PlayerMissilesPowerUp prefab into the Element 5 field.
Once the Power-Up is collected, the PlayerHomingMissiles() method is called. This basically adds 5 missiles to the Player’s inventory. It also checks to see if the maximum capacity has reached a quantity of 25, and if it has, it caps the max at 25. We then update the UI to reflect the updated quantity of missiles and set the bool of _isPlayerhomingMissilesActive to True, allowing the Player to fire this new weapon.
You can see below how the UI presents the number of missiles held, as well as a demonstration of the Homing Missiles collectible getting picked up by the Player.
Now that we have all the pieces in place, let’s examine how we get these Homing Missiles to seek out and destroy those Enemy ships. Thanks for reading and I’ll see you in Part 3 (read it here) :)