Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Helicopter Sight LookAt not staying on target

Discussion in 'Scripting' started by chuckfvl, Feb 13, 2020.

  1. chuckfvl

    chuckfvl

    Joined:
    Jul 10, 2019
    Posts:
    8
    I have a helicopter object that has a rigidbody attached to it. On this helicopter, I have a sight that has a follow target option using the LookAt function. I also have a line renderer to simulate a laser and verify where the sight is looking and get a target distance. If I stop the helicopter from moving(set kinematics to false) or go into a hover the sight will settle and look right at the center of my target, but if I start flying around the sight line renderer is all over the place until I pause or hover again.

    Is there a reason the LookAt isn't always spot on regardless of flying vs hovering and that there is a delay where it needs to settle in? I have tried to code it in 2 ways and both behave the same. Any and all help is greatly appreciated.

    Code (CSharp):
    1. public Transform heli;
    2. public Transform target;
    3.  
    4. void Update()
    5. {
    6.      transform.LookAt(target, heli.transform.up);
    7. }
    Also tried it this way

    Code (CSharp):
    1. public Transform heli;
    2. public Transform target;
    3.  
    4. void Update()
    5. {
    6.      Vector3 relPos = target.position - transform-position;
    7.      Quaternion rot = Quaternion.LookRotation(relPos, heli.transform.up);
    8.      transform.rotation = rot;
    9. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Try putting it in LateUpdate. It might be that your lasersight's Update() is executing before your helicopter moves, and assuming it's a child, the helicopter's movement will throw it off.
     
  3. chuckfvl

    chuckfvl

    Joined:
    Jul 10, 2019
    Posts:
    8
    Thanks, I will give that a shot, also I made a duplicate sight outside of the rigidbody, moved it around and it doesn't exhibit the same behavior, it's always on the target. Is there some aspect of the rigidbody causing this?
     
  4. chuckfvl

    chuckfvl

    Joined:
    Jul 10, 2019
    Posts:
    8
    Tried using LateUpdate and that had no effect unfortunately.