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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question How to make a turret maintain accuracy when rotating prefab

Discussion in 'Scripting' started by OutDoorsScene, Nov 25, 2022.

  1. OutDoorsScene

    OutDoorsScene

    Joined:
    Sep 9, 2022
    Posts:
    31
    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:
    2,173
    What does "maintaining accuracy" mean?
    If you mean in the context of aiming with that turret, then you definitely don't need slerp.
     
  3. OutDoorsScene

    OutDoorsScene

    Joined:
    Sep 9, 2022
    Posts:
    31
    I mean keeping the turret aiming at the small GameObject. even when the helicopter is moving and rotating.
     
  4. Hannibal_Leo

    Hannibal_Leo

    Joined:
    Nov 5, 2012
    Posts:
    493
    Use
    Transform.rotation
    instead of
    Transform.localRotation
    .
     
  5. OutDoorsScene

    OutDoorsScene

    Joined:
    Sep 9, 2022
    Posts:
    31
    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. Hannibal_Leo

    Hannibal_Leo

    Joined:
    Nov 5, 2012
    Posts:
    493
    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. OutDoorsScene

    OutDoorsScene

    Joined:
    Sep 9, 2022
    Posts:
    31
    I would of never figured that out, thank you. Personally I prefer a smooth turret rotation over fixed.
    ezgif-5-75b4c21831.gif