• 03/05/14 07:53 PM
    Sign in to follow this  

    Creating a Homing Missile in Unity

    General and Gameplay Programming

    pkMinhas
    Homing missiles have been my favorite in all types of games. There is nothing more fun than a fire & forget weapon. Homing missiles will generally lock on to a target & keep following it till a collision occurs OR the missile might auto-destruct after a certain period of time. This article will talk about creating homing missiles in Unity using a kinematic rigidbody in order to keep things simple. A kinematic rigidbody's transform can be manipulated directly. Also, I would like to introduce you to my game, Hawks, which is a WIP at the moment with 90% of the functionality in place.

    A basic homing missile will operate based on three properties: Speed, Auto Destruct time & target.

    Important: The following code should be attached to a gameobject having a Rigidbody component with isKinematic set to true.
    1. Speed: Since we are using a kinematic rigidbody, the speed of the projectile can be controlled by translating in the Update() function:

      transform.Translate(0,0,speed * Time.deltaTime,Space.Self);
    2. Auto Destruct: This functionality can be implemented by calling a coroutine which self-destructs after a set amount of time:

      function Start () { StartCoroutine(AutoExplode()); } private function ExplodeSelf() { Instantiate(explosion,transform.position,Quaternion.identity); Destroy(gameObject); } private function AutoExplode() { yield WaitForSeconds(autoDestroyAfter); ExplodeSelf(); }
    3. Target: The missile needs to follow the target. We can achieve this by rotating the missile towards the target transform using the wonderful Slerp function:

      var relativePos : Vector3 = target.position - transform.position; var rotation : Quaternion = Quaternion.LookRotation(relativePos); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, homingSensitivity);
    Notice the homingSensitivity variable that is used as the last parameter to the Quaternion.Slerp(from, to, t) function. To understand its usage, we need to look at what the Slerp actually does. Slerp == Spherical Interpolation. Mathematical details available at: http://en.wikipedia.org/wiki/Slerp. For those not so mathematically inclined, given two values, the Slerp function will calculate an intermediate position between from & to based on the third parameter which lies in the range [0,1]. If the third param is 0, the from value will be returned & 1 will return the to value. Try experimenting with different values to achieve the desired effect in your game. Below is the complete code for the HomingMissile.js script: #pragma strict var target : Transform; var speed : float; var autoDestroyAfter : float; //prefab for explosion var explosion : GameObject; /* Represents the homing sensitivity of the missile. Range [0.0,1.0] where 0 will disable homing and 1 will make it follow the target like crazy. This param is fed into the Slerp (defines the interpolation point to pick) */ var homingSensitivity : float = 0.1; function Start () { StartCoroutine(AutoExplode()); } function Update () { if(target != null) { var relativePos : Vector3 = target.position - transform.position; var rotation : Quaternion = Quaternion.LookRotation(relativePos); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, homingSensitivity); } transform.Translate(0,0,speed * Time.deltaTime,Space.Self); } function OnTriggerEnter(other: Collider) { //Damage the other gameobject & then destroy self ExplodeSelf(); } private function ExplodeSelf() { Instantiate(explosion,transform.position,Quaternion.identity); Destroy(gameObject); } private function AutoExplode() { yield WaitForSeconds(autoDestroyAfter); ExplodeSelf(); }

    Hawks: Guardians of the Skies

    I began development on Hawks around 6 months back. The game has come a long way since then & I would like to give back to the community with more articles & insights into the game dev process. Check out Hawks on IndieDB

    Article Update Log

    28 Feb 2014: Initial release


      Report Article
    Sign in to follow this  


    User Feedback

    Create an account or sign in to leave a review

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

    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


    Orymus3

    Report ·

      

    Share this review


    Link to review
    mousetail

    Report ·

      

    Share this review


    Link to review
    xexuxjy

    Report ·

      

    Share this review


    Link to review
    KamilKZ

    Report ·

      

    Share this review


    Link to review
    pkMinhas

    Report ·

      

    Share this review


    Link to review