Homing missiles tutorial.


Lately I’ve been working on a flying game prototype, after playing Ace Combat 2(PS1) a couple of weeks ago I really liked the homing missiles mechanic, which basically is locking on the enemy and try to be at a good distance and angle from the enemy so the missiles reach the target. And when the missiles are used against the player, he needs to do some evasive maneuvers to avoid the missile. So I decided to add homing missiles to my prototype, and that give me a good excuse to share it.

How they work?

Homing missiles are always trying to approach another object to destroy it, always changing their direction to face the target. We want a missile that rotates towards the target gradually, that makes us feel the inertia in the missile and makes us think is possible to avoid it.

In geometry

We have this information to work with:

The position of the missile.
The position of the target.
The forward vector of the missile.
The vector pointing from the missile to the target.

Let’s take a look at how we work with this elements.

The position of the missile. A point (x, y, z).

Vector3 missilePosition = transform.position;

The Position of the target.

Vector3 targetPosition = target.tranform.position;

The forward vector of the missile. The vector which is always pointing to the front of the missile. Unity provides a method to have this any time and get this as a unit vector.

Vector3 forwardVector = transform.forward;

The target vector, which is the vector pointing from the missile to the target position. To get this we need to we make a point subtraction to get the vector.

Vector3 targetDirection = target.transform.position - transform.position;

So, we’ll use the forward vector and the target vector to create a new orientation vector which will be at some point between the two. Then we orientate the missile with this new vector.

This is the rotation speed. This says how faster the missile rotates(in radians), if the .035 coefficient is higher, the missile will turn faster.

float step = speed * Time.deltaTime * .035f;

Ok, now we have the forward and target vector, we create a new direction vector halfway the target. We use the Unity3D method RotateTowards to rotate the forward vector towards the target vector. In the future I’d like to make this method manually so I can understand how this works internally.

Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, step, 0.0F);

And then we apply the new orientation to our missile.

transform.rotation = Quaternion.LookRotation(newDirection);

Finally we apply some ‘force’ to the missile to move it to the front.

Vector3 movingVector = (transform.forward * Time.deltaTime * speed);
transform.position += movingVector;

In summary, this is what happens every frame.

You can download the example for mac or windows. Or even better, you can try the WebGL version on your browser.

Let me know in the comments if you have any question or any suggestion to improve future posts.