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

Question How to make a turret maintain accuracy when rotating prefab

Discussion in 'Scripting' started by Deleted User, Nov 25, 2022.

  1. Deleted User

    Deleted User

    Guest

    I want to create a helicopter that can maintain accuracy while flying.
    Code (CSharp):
    1.  
    2. void TrackTarget()
    3. {
    4.  Vector3 Relativepos = (CurrentTarget.transform.position + new Vector3(0, 2f, 0)) - this.transform.position;
    5.         Quaternion rotation = Quaternion.LookRotation(Relativepos);
    6.         Turret.transform.localRotation = Quaternion.Slerp(Turret.transform.localRotation, rotation, TrackingSpeed * Time.deltaTime);
    7. }
    ezgif-3-530ca84777.gif
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,070
    What does "maintaining accuracy" mean?
    If you mean in the context of aiming with that turret, then you definitely don't need slerp.
     
  3. Deleted User

    Deleted User

    Guest

    I mean keeping the turret aiming at the small GameObject. even when the helicopter is moving and rotating.
     
  4. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    646
    Use
    Transform.rotation
    instead of
    Transform.localRotation
    .
     
  5. Deleted User

    Deleted User

    Guest

    That has improved the accuracy, especially on the y axis. But it still doesn't have the precision of
    transform.LookAt
    .

    Is there a way to make
    transform.LookAt
    smooth?
    ezgif-2-7fac6aee64.gif
     
  6. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    646
    Yea, you calculated the wrong Vector. Also I suggest Quaternion.RotateTowards instead of Quaternion.Slerp or Quaternion.Lerp because it feels more mechanical which I think fits better for a turret.

    The "Relativepos" is actually the Vector that your Turret will look at, so you have to calculate it from the position of the turret and not from the origin of the Helicopter because that will lead to an offset.
    Seems like you tried correcting this with the
    + new Vector3(0, 2f, 0)
    - this is no longer required because we are now getting the correct Vector.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class LookAt : MonoBehaviour
    4. {
    5.     public Transform CurrentTarget;
    6.     public Transform Turret;
    7.     public float TrackingSpeed = 100;
    8.  
    9.     void LateUpdate()
    10.     {
    11.         TrackTarget();
    12.     }
    13.  
    14.     void TrackTarget()
    15.     {
    16.         Vector3 targetVector = CurrentTarget.position - Turret.position;
    17.         Quaternion rotation = Quaternion.LookRotation(targetVector);
    18.         Turret.rotation = Quaternion.RotateTowards(Turret.rotation, rotation, TrackingSpeed * Time.deltaTime);
    19.  
    20.         Debug.DrawRay(Turret.position, Turret.forward * 1000, Color.red);
    21.     }
    22. }
    23.  
     
    spiney199 likes this.
  7. Deleted User

    Deleted User

    Guest

    I would of never figured that out, thank you. Personally I prefer a smooth turret rotation over fixed.
    ezgif-5-75b4c21831.gif