How to Create Homing Missiles in Game using Unity

July 14, 2020
Unity
how to create homing missile in unity

Project: How to Create Homing Missiles in Game using Unity with Source Code

Objective

The main objective of this post is to give a brief introduction in How to Create Homing Missiles in Game (2D) in Unity. A script for Homing missile is also included and explained in the article.

INTRODUCTION

You might have seen Homing Missiles in many movies.

how to create homing missile in game using unity

how to create homing missile in game using unity

Remembered these scenes from “Marvel’s The Avengers” movie?

If you are not a Marvel fan don’t worry, I am here to help you.

“Homing Missile is the missile which locks target and chases the target until or unless it reaches the target and blasts itself.”

There are plenty of games which are using Homing Missiles.

Excited to implement Homing Missiles in your game?

I know you are.

Let’s set the scene setup first

Scene Setup:

  1. Create two 2D Objects name them “Missile ” and “Target”.
  2. Add a Sprite to them. (You can download the project from below link).
  3. Add rigidBody 2D in missile and add Script also name it as ”Homing Missile”

How it actually works?

You might think,

It is easy to Implement Homing Missile. Just use MoveToward/Lerp until it reaches the target. Then just write code and look at what is going on in your scene. (try it and then read further to understand)

create homing missiles in unity

It is reaching the target properly but there is no feel of a rocket. (Right Brain:-What is wrong with my code?)

Checkout an actual script for Homing Missiles.

Scripting:

public class HomingMissile : MonoBehaviour
{
    public Transform target;
    public Rigidbody2D rigidBody;
    public float angleChangingSpeed;
    public float movementSpeed;
    void FixedUpdate ()
    {
        Vector2 direction = (Vector2)target.position - rb.position;
        direction.Normalize ();
        float rotateAmount = Vector3.Cross (direction, transform.up).z;
        rigidBody.angularVelocity = -angleChangingSpeed * rotateAmount;
        rigidBody.velocity = transform.up * movementSpeed;
    }
}

I think script is short but not a sweet one. You can use this homing missile script for your code.

Don’t be scared, I’ll show you how to do it.

Let’s start with the table to introduce variables:

Variable Name Variable Type Description
rigidBody RigidBody2D rigidBody of the missile to add velocity and change angle.
target Transform To get the position of the Target.
angleChangingSpeed float The speed to change the angle
movementSpeed float Speed of movement

Pure Mathematics: (Open your Brain’s left part)

how to create homing missiles in game using unity

You need to understand this Picture. That’s How our Homing Missile Works.

First, what you need is “Direction”.

STEP: 1

direction= targetPosition (target.position) - missilePosition(rb.position);

how to create homing missiles in game using unity

This is how subtraction of two Vector happens (Pythagoras Theorem).

STEP: 2

direction.Normalize();

Normalization means converting vector to unit vector.

Example:

Vector A(3,4)
Normalized(A)=(3/3*3+4*4 , 4/3*3+4*4)=(3/5,4/5)

STEP: 3

Find Amount of rotation to do that.

float rotateAmount=Vector3.Cross(direction,transform.up).z;

how to create homing missiles in game using unity

Blue Arrow(direction)

Red Arrow(Missile Transform)

green(Resulting Vector).

All three Vectors are perpendicular to each other.that’s why we needed z-axis only.

STEP: 4

Now we need to change angularVelocity of our rigidBody.

rb.angularVelocity = - angleChangingSpeed * rotationAmount.

Minus Sign(-) is used to reverse the direction of movement.

STEP: 5

Adding Velocity to our rigidBody.(just changing the Angle is not Enough).

rb.velocity=tranform.up * movementSpeed.

Stop cheering just yet!

This is not the end!

Missile needs balancing between angleChangingSpeed and movementSpeed.

What if there is no balance between angleChangingSpeed and movementSpeed (Great question! Open right brain is needed here ! )

homing Missiles heading to the target

In the above Image , the missile is heading forward to the target, there is no problem here.

velocity force vs angular velocity

In the above Image, the target has moved to a new position. The missile continues to go toward the target due to angular velocity (in red) , while still going to the place where the target used to be (in black) due to its velocity. (Thinking what is wrong.?)

velocity force vs angular velocity -2

In the above Image, the missile’s velocity forces it to move at the side of the target (black) while the angular velocity tries to pull the missile to the target. (Now you might feel there is something suspicious).

orbit dead loop

In the above Image, the missile falls into a stable orbit around the target and never reaches its goal.

The black arrows indicate a velocity vector while the red lines indicates angular velocity. (That’s why need a balance between angleChangingSpeed and velocity).

Considering that there is no friction in space, there is nothing to slow the velocity of the missile down and collapse the orbit.

That is how whole Universe is trapped (Thank God for this error else there is no life!).

Now, How to Fix this Problem ?

Well, There is no particular formula to balance them, but by Increasing angle or decreasing velocity will help you there.

Caution

  • Don’t go with very small speed so your missile loses the feel of Homing Missile and Don’t make it Extreme fast it will be trapped in the orbit.

Caution

  • Don’t make angleChangingSpeed too small else it will fall in to trap and to high angleChangingSpeed loses the feel of your game.

That’s It for Homing Missile from my side..!

Download Here
Facebook Comments

Leave a Reply

Your email address will not be published.