Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Homing Missiles?

Discussion in 'Scripting' started by SpyridonZ, Nov 19, 2009.

  1. SpyridonZ

    SpyridonZ

    Joined:
    Jun 7, 2009
    Posts:
    97
    I'm no expert in math (havent taken trig yet), so I'm wondering if anyone could kindly explain how I could calculate a missile following a target, similar to a tracking or homing missile commonly seen in games?

    Either a quick example of the math involved, or a link that would teach me how to calculate it would be appreciated :p
     
  2. ChrisMann

    ChrisMann

    Joined:
    Oct 21, 2009
    Posts:
    52
    you don't really have to calculate it.

    Code (csharp):
    1. transform.lookAt(positionOfTargetGoesHere);
    Points the missile at the target.

    then you can either translate it forwards, or addforce to it to make it move.
     
  3. SpyridonZ

    SpyridonZ

    Joined:
    Jun 7, 2009
    Posts:
    97
    Sorry I may have left out some information.

    I dont want it to guaranteed hit the target at all times - I want the homing to have a limit in how sharp the missiles turn - that way they -could- be avoided, but it will be harder than if they did not home in.
     
  4. DMJ

    DMJ

    Joined:
    Nov 5, 2009
    Posts:
    83
    If you use the LookAt() suggestion above and go the "addforce" route, you can make the missile easier or harder to evade by altering the force added and the mass of the missile.

    If the force-to-mass ratio is pitched right, it will have difficulty overcoming its own inertia and will be easier to evade.

    That's how I do my missiles, anyway.

    Smarter homing missile techniques involve predicting where the target will be based on the separation and the relative velocities and aiming for that instead.

    You might also want to consider adding some code to prevent the missile from reaching Ludicrous Speed if it gets to accelerate in a straight line for a long time. The physics engine would probably get cranky calculating collisions between objects moving ridiculously fast.
     
  5. magwo

    magwo

    Joined:
    May 20, 2009
    Posts:
    402
    My AH-6 game has quite advanced missile guidance code.. it's basically a "PD" control system that needs to consider both proportional error and derivative error with regards to missile angle (and angular velocity) relative to the predicted position of the moving target.

    That's a long sentence. :)
    http://en.wikipedia.org/wiki/PID_controller

    Based on those calculations, you apply a force in the forward-facing plane of the missile, at the rear of the rigidbody that makes up the missile. This simulates some kind of aft steering fins and/or thrust vectoring controls.

    Also, it's important to have some sort of forward-pointing stabilizing fins on your missiles, as it's very hard to hit targets using only a force pointing backwards. The fins help the missile travel in the direction it's being pointed. Without fins the missile will tumble around a lot and it will be extremely error-prone with regards to changing conditions in the environment (like current velocity, target velocity, altitude etc).


    To see some of this in action, you can play my AH-6 game and cheat your way to "hellfire" missiles, using Shift + 5, and then eventually find some targets. If fired without an available target in the HUD, the missile will not use any form of guidance/homing.

    http://ah-6.surgical.se/
     
  6. Achim

    Achim

    Joined:
    Dec 19, 2007
    Posts:
    199
    maybe you can modify this one, it works if you click on one of the pink targets to change the target like you can see in the webplayer:

    http://www.a-kima.de/carwaypoint.html

    Code (csharp):
    1.  
    2. var target : Transform;
    3.  
    4. var damping = 1.0;
    5. var drivespeed = 15;
    6.  
    7.  
    8. function Update () {
    9.    
    10.            
    11.         transform.Translate(Vector3.forward * Time.deltaTime * drivespeed);
    12.        
    13.         var rotation = Quaternion.LookRotation(target.position - transform.position);
    14.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    15.    
    16.  
    17.     if (Input.GetButtonDown ("Fire1")) {
    18.  
    19.         ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
    20.         var hit : RaycastHit;
    21.         if (Physics.Raycast (ray, hit)) {
    22.             raytarget = hit.point;
    23.             print("HIT SOMETHING");
    24.             target = hit.transform;
    25.         }
    26.        
    27.     }
    28.  
    29. }
     
    purnamasari and Farol like this.
  7. Ilikescifi

    Ilikescifi

    Joined:
    Dec 15, 2010
    Posts:
    17
    This is a very good script. Thx for sharing.