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”

    Implementation

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

    working-of-homing-missile.thumb.png.0a8fea8e4db1cd6c3934074ecb967024.png

    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;
            }
        }

    Variables

    • 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.

    actual-path-of-homing-missile.png.41be56328c62d1be321d2be11d604e8d.png

    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);

    vector-substraction.png.1c8b1fd2c8ec0837710a652d869adcff.png

    Step 2

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

    direction.Normalize();

    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.

    vector-cross-product.gif

    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.

    missile-heading-to-the-target.png.0e57cdc81fd2267a974ad1b78df9c283.png

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

    velocity-force-vs-angular-velocity.png.0daafa2818fbe37ff50b079dc7b3e854.png

    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).

    velocity-force-vs-angular-velocity-2.png.6de28c58312f7302b6a5cc38fd6979cd.png

    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.

    orbit-dead-loop.png.1b6000ad44ab9086c6fb4b9fa2570bdd.png

    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!

    real-life-exsmple-homing-missile.png

     

    [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 exzizt
      Hey everyone! I just wanted to announce that after eight months of hard work, we're approaching the launch of our second BETA! Everyone who signs up gets to be the first to play.
      Signup here: https://www.playbirdie.com/

    • By midn
      Since I'm not a good 2D artist (learning sloowly with low motivation but that's a different topic) here's what the current art process is:
      Model a thing in Blender. Render it in a low resolution. Using ImageMagick apply a color palette. The palette is I think the SNES palette that I took from Wikipedia and cropped a little bit.
      There's a problem though, below is a screenshot of the game. Excuse the size :P.
      It's extremely dark, I can't say if it can be passed off as just a "style" but it's even a bit hard on my eyes. And see the background, the black with green spots? That's supposed to be tall grass, but the palette reduces detail until it just looks like overly peppered lettuce.
      The white rectangles will be ladder sprites, it's unfinished at the moment.

    • By ThomasG_art
      A complete scene and prefabs created in unity. allow you to get you started rapidly.
      Mountains, ruins and cave areas are all included:

      http://u3d.as/1ypw 

      The cliffs/rocks and ruins feature a trilanar shader, you can customize the colors/textures and overlays. The water and vegetation shader are also customisable. 
      The trees/vegetation prefabs are using unity's terrain system and are already set-up for you. The ruins prefabs are modular and fit on a grid. 
      The cloths prefabs are using unity cloth's system and can be adjusted. 
      Particle effects, air particles, clouds, mist , butterflies, etc...

      Trailer:
       




    • By horror_man
      Hello, I'm currently searching for additional talented and passionate members for our team that's creating a small horror game.
      About the game: The game would be a small 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 more than 4 years of experience and I'm currently leading the team that's 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. Also I'm currently learning how to use Unity and learning how to program.
      Our team right now consists of: Me (Game Designer, Creator, Music Composer, Writer), 3 3D Artists, 4 Game Programmers, 1 Sound Effect Designer, 2 Concept Artists, 1 3D Animator.
      Who am I looking for: We are looking for a passionated and talented 3D Environment Artist that's experienced in hard surface models and the creating of closed/underground scenes with a realistic feeling to them. Familiarity with the sci-fi/horror genre is a plus.
      Right now the game is in mid-early development and you can see more information about it and follow our progress in our game jolt page here: https://gamejolt.com/games/devilspunishment/391190 . We expect to finish some sort of prototype in 2 months from now.
      This is a contract rev-share position
      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
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!