Jump to content
  • Advertisement
  • 07/01/18 10:53 PM

    How to Create Homing Missiles in Unity

    General and Gameplay Programming

    Vivek Tank

    This article provides a brief introduction to creating Homing Missiles in a 2D game. A Homing Missile is a missile that locks onto and chases a target until it reaches the target and explodes. We'll be using the Unity engine, and a script for the Homing missile is also included and explained in the article.

    Scene Setup

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


    To implement a basic homing missile we could use MoveToward/Lerp until the missile reaches its target.


    Using MoveToward/Lerp, the missiong will reach the target properly, but its movement will be very linear - there is no feel of a homing missile.

    To implement a better feel, we need to simulate with better physics. Let's walk through a solution:

        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;


    • rigidBody: rigidBody of the missile to add velocity and change angle.
    • target: To get the position of the Target.
    • angleChangingSpeed: The speed to change the angle
    • movementSpeed: Speed of movement

    The Math

    Here is a visualization of the path our homing missile needs to take on a moving target.


    Let's talk about how to implement this.

    First, you need “Direction”.

    Step 1

    We subtract the two vectors, targetPosition and missilePosition to get the direction.

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


    Step 2

    We convert the direction vector to a unit vector through normalization.


    Step 3

    Calculate the rotation vector. The rotation vector will be the cross product of our direction vector and our missile's up vector. The rotation vector will be the z-axis of the cross product.

    In the diagram below the blue arrow is our direction, the red arrow is the up vector of our missile's transform, and the green arrow is our resulting z-axis component of the cross product between direction and up.


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

    Step 4

    Next, we change the angularVelocity of the missile's rigidBody to control the orientation of our missile. We use an angleChangingSpeed factor to control the speed of rotation.

    rb.angularVelocity = -angleChangingSpeed * rotationAmount

    Step 5

    And finally we add velocity to our rigidBody, which controls the translation of our missile in the environment. 

    rb.velocity = tranform.up * movementSpeed

    But wait...

    We aren't done yet. In the code above we have two movement factors that need to be balanced: angleChangingSpeed and movementSpeed.


    In the above Image, the missile is moving toward the target, there is no problem here.


    However, in this image we have one tick of the simulation and the target has moved to a new position. The missile is oriented toward the target due to angular velocity (red), but it continues to move toward the target's previous position due to its velocity (black).


    As we can see, the missile's velocity moves it behind the target (black) while the angular velocity (red) tries to pull the missile to the target.


    This can result in the missile falling into a stable orbit around the target and never reaching its goal. We can balance this with the angleChangingSpeed and movementSpeed parameters.

    There is no particular formula to balance these parameters. The necessary parameters will depend on your gameplay and game design.

    Be careful with extremes for movementSpeed, positive or negative. Too slow and your missiles won't feel like homing missiles, and too fast will lead to them being trapped in orbit.

    Same with angleChangingSpeed. Too small it will be in orbit, and too high you will lose the feel of homing missiles.

    I hope this article has been useful and hope to see homing missiles in your game!



    [Wayback machine archived copy]

      Report Article

    User Feedback

    There are no comments to display.

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

  • Advertisement
  • Advertisement
  • Latest Featured Articles

  • Featured Blogs

  • Advertisement
  • Popular Now

  • Similar Content

    • By therax1986
      Hi everyone. For the last few months I’ve been working on a simple HTML5 2D side scrolling action game called Theraxius. It's nothing new and revolutionary, it's more like an evolution of different technology (combination of HTML5, PHP, MySQL). The game also includes a level editor so you can create your own levels. The game and the level editor is written almost completely in JavaScript, no download is required. Just load and play. Here are a few screenshots and the link to the page.
      In the next weeks I’ll try to post some videos, try to add registration (for newsletter and later for public test).
      Release date: when it's done

    • By horror_man
      Hello, I'm currently searching for a talented and passionate programmer to create a small but great horror game that would take around 3 months to be done.
      About the game: The game would be a sci-fi/post-apocalyptic survival horror 3D game with FPS (First person shooter) mechanics and an original setting and story based in a book (which I'm writing) scene, where a group of prisoners are left behind in an abandoned underground facility. It would play similar to Dead Space combined with Penumbra and SCP: Secret Laboratory, with the option of playing solo or multiplayer.
      Engine that'd be used to create the game: Unity
      About me: I'm a music composer with 4 years of experience and I'm fairly new in this game development world, and I'm currently leading the team that'd be creating this beautiful and horrifying game. I decided that making the book which I'm writing into a game would be really cool, and I got more motivated about doing so some time ago when I got a bunch of expensive Unity assets for a very low price. However, I researched about how to do things right in game development so I reduced the scope of it as much as I could so that's why this game is really based in a scene of the book and not the entire thing (and also that's why it would take 3 months). Also I'm currently learning how to use Unity and how to model things with Blender.
      Our team right now consists of: Me (Game Designer, Creator, Music Composer, Writer), 3 3D Modelers, 1 Sound Effect Designer, 1 Concept Artist and 1 Programmer.
      Who am I looking for:
      - A programmer that's experienced in C# and with Unity.
      Right now the game is very early in its development (GDD is completed and all 3D Items, Music and Sound Effects are completed).
      If you are interested in joining, contributing or have questions about the project then let's talk. You can message me in Discord: world_creator#9524
    • By kcirkl
      So, I'm aware this may be a broad question with a vast variety in the anwser. However, I am a game design student that works professionally for a large engineering company conducting software configuration. Naturally, I'm curious about the path ahead and I'm desperately gathering information on the overall process of developement with Unity. Are there professional resources available that can outline the processes and functions of the engine, and what can be used to interact with it? I'm looking for options other than the official documentation. I have already jumped into that. Thanks!
    • By tspartant
      Hey everyone! My name is Ryan and I am the founder of Visualistic Studios, LLC. 
      I'm looking for experienced developers interested in short term and long term contracting. For the past 3 years I've been working in game development contracting, and the past year I've been working full time from home. Since then, I've received more and more contracts and I'm now at the point that I have too many for myself to handle. I have at least another full time job's worth of programming offers, and around 30-80 hours a month for 3D modeling/animation. I also am in need of a UI artist, so if you're talented please contact me! I have contracts using Unity as well as Unreal Engine 4, so if you can program in either please contact me. 
      If you are interested in working on these contracts, please send me links to your work and you hourly rate. (Most contracts range be between 18-25$/h, but please provide your normal hourly rate)
      You can get ahold of me through email - "ryan.hobbs@visualisticstudios.com", or Discord "TSpartanT#4670"
      Thank you everyone for reading, hope to hear from you soon!

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!