Project 1: Space Shooter

How to make a homing missile in Unity

Adding a new weapon type

Showing staged gameplay of the homing missile working and gaining extra points if they don’t use the missiles and collect a new stack of them.
If the player saves the homing missiles they will get additional points if a new stack is collected.

Thus far all the weapons have replaced the normal laser with a different sort of laser for a short period of time. This time I will make a new independent weapon called a homing missile for the player.

At the beginning of the game the player has zero ammunition for the homing missile, so they have to wait for an ammunition drop, but with this weapon the player doesn’t have to shoot directly at the enemy.

The main game object has the SpriteRenderer for the missile sprite, RigidBody2D and BoxCollider2D to handle what happens when a target is hit, and the HomingMissile script.

Then it has a child game object which has the RigidBody2D and the CircleCollider2D to handle the targeting and the HomingDetector script.

The HomingDetector script uses OnTriggerEnter2D and OnTriggerExit2D to acquire the target and then to lose the target if the enemy manages to evade by exiting the detection zone.

Since there possibly are more than one enemy in the game area at the same time, and their paths might be close to one another, the HomingDetector script uses the CalculateClosestEnemy method to decide which enemy will be targeted.

Showing the CalculateClosestEnemy script of the HomingDetector script.
The CalculateClosestEnemy script of the HomingDetector script.

The OnTriggerEnter2D adds every enemy that enters the detection zone into a list. The CalculateClosestEnemy goes through this list in a foreach-loop and picks the one that is closest to the homing missile as a target using the Vector3’s Distance method for the calculation. Then the method passes the closest enemy GameObject as an argument for the HomingMissile script’s public method TargetAcquired.

The TargetAcquired method itself toggles a Boolean variable _targetAcquired either from to when the target is acquired or from to when the target is lost. At the same time it saves the passed argument into a variable _target, so that it can be used elsewhere in the script.

Showing the HomingMissile script’s TargerAcquired method.
The HomingMissile script’s TargerAcquired method.

Once there is a target, the _targetAcquired variable overrides the normal movement of the missile. First the overridden movement calculates a vector pointing to the target from the missile’s current position called direction. Then it calculates a rotation for the missile from the y-axis, Vector3.up, towards the vector direction and saves it as a new rotation for the missile.

Showing the homing missiles Movement method.
The homing missiles Movement method.

Done this way the missile will turn and move towards the target when the movement is done with the Translate method. The normal speed of the missile is , but when the target has been acquired the speed increases to .

Lastly the player can shoot the missile by pressing the E-key, this will be checked inside the Update method with the Input.GetKeyDown method and the KeyCode signal.

A code snippet showing the the code for firing the missile with the E-kay if the player has missiles left.
The the code for firing the missile with the E-kay if the player has missiles left.

If the conditions are met the actual firing happens in the FireMissile method, which reduces the amount of missiles the user has and updates the visuals for it. After that it will instantiate the missile.

Showing the FireMissile method inside the Player script.
The FireMissile method inside the Player script.

--

--

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