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 4 equals the Homing Missiles.
We then need to adjust the Switch statement to capture the possibility that the _powerUpID may equal 4 (case 4:), in which case we would execute the PlayerHomingMissiles() method inside the PlayerScript script.
Now that you’ve added homing missiles to your _powerUpID list, you need to go back to your SpawnManager, increase the size of the Player Weapons Power Ups array to 3 and add the PlayerMissilesPowerUp prefab to the array.
Once the Power-Up is collected, the PlayerHomingMissiles(5) method is called.
This basically adds 5 missiles to the Player’s inventory and set the bool _isPlayerhomingMissilesActive to True, allowing the Player to fire this new weapon. We then call the UpdatePlayerMissileStores() method to update the UI data.
UpdatePlayerMissileStores() caps the missile stores lower limit to zero, and caps the upper limit to the value stored in _maxMissileStores. We then update the UI to reflect the updated quantity of missiles.
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 :)