Creating a 2D Homing Missile in Unity

July 27, 2019

Many 2D games use homing missiles to target players and the movement looks really amazing. Ever wondered how such movement is achieved in Unity.
This blog will explain how can you create a simple 2D homing missile in Unity.


Setting up the Project

To get started, I have imported a rocket sprite into the scene and attached a simple RocketMove.cs script to the rocket.
using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
public class RocketMove : MonoBehaviour
{
    [SerializeField] float MoveSpeed = 350f;
    Rigidbody2D rb;

    // Use this for initialization
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        rb.gravityScale = 0;
    }

    // Update is called once per frame
    void Update()
    {
        rb.velocity = transform.up * MoveSpeed * Time.deltaTime;
    }
}
After attaching the script, the Rocket starts moving upwards as shown in the gif below.

Following the TargetThe Underlying Concept
To make the rocket follow the target, we need to adjust the angular velocity of the rocket dynamically based on the rocket’s current position. To get the angular velocity adjustment we need to make, we need to do the Cross Product of the two vectors,
Vector A: From Rocket to Target
Vector B: transform.up of the Rocket
The Vector Cross product gives a new vector perpendicular to the two vectors with magnitude ranging from -1 to 1(both inclusive).
The cross product changes by the angle change between the two vectors VectorA and VectorB as shown in the gif below.

Updating the PlayerMove Script

Now that we have understood the concept of changing the angular velocity of the Rigidbody2D attached to the Rocket, we need to make changes to the script to make the Rocket follow the Target gameobject.
using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
public class RocketMove : MonoBehaviour
{
    [SerializeField] Transform Target;
    [SerializeField] float MoveSpeed = 350f;
    [SerializeField] float RotateSpeed = 2000f;
    Rigidbody2D rb;

    // Use this for initialization
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        rb.gravityScale = 0;
    }

    // Update is called once per frame
    void Update()
    {
        rb.velocity = transform.up * MoveSpeed * Time.deltaTime;

        Vector3 targetVector = Target.position - transform.position;

        float rotatingIndex = Vector3.Cross(targetVector, transform.up).z;

        rb.angularVelocity = -1 * rotatingIndex * RotateSpeed * Time.deltaTime;
    }
}
Now create a Target gameobject in the scene and assign its transform to the Target transform in the RocketMove.cs file’s Target property in the inspector.
With the following step done, click the Play button and move the target to see the homing missile in action. This is what the end result will look like.

Optional Steps

I tried to keep this tutorial simple and probably you will need to attach the 2D colliders to the Rocket & Target and also handle the collision of the Rocket with the Target. You can also instantiate an explosion prefab to make the collision look awesome.

You Might Also Like

0 comments

Follow by Email