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

Question [3D] How to normalize a rotation that uses Quaternion.LookRotation and Quaternion.Lerp?

Discussion in 'Scripting' started by SovPenguin, May 22, 2023.

  1. SovPenguin

    SovPenguin

    Joined:
    Feb 26, 2021
    Posts:
    5
    Hello,

    I have following code snippet:

    Code (CSharp):
    1. [SerializeField] GameObject Azimuth;
    2.     [SerializeField] GameObject Elevation;
    3.     [SerializeField] float AziRotSpd;
    4.     [SerializeField] float EleRotSpd;
    5.  
    6. Vector3 projectedPos = transform.InverseTransformPoint(predictedPos);
    7.             projectedPos.y = 0;
    8.             projectedPos = transform.TransformPoint(projectedPos);
    9.  
    10.             Quaternion azimuthRot = Quaternion.LookRotation(projectedPos - transform.position, transform.up);
    11.             Azimuth.transform.rotation = Quaternion.Lerp(Azimuth.transform.rotation, azimuthRot, AziRotSpd * Time.deltaTime);
    12.  
    13.             projectedPos = Azimuth.transform.InverseTransformPoint(predictedPos);
    14.             projectedPos.x = 0;
    15.             projectedPos = Azimuth.transform.TransformPoint(projectedPos);
    16.  
    17.             Quaternion elevationRot = Quaternion.LookRotation(projectedPos - Elevation.transform.position, transform.up);
    18.             Elevation.transform.rotation = Quaternion.Lerp(Elevation.transform.rotation, elevationRot, EleRotSpd * Time.deltaTime);
    19.             Elevation.transform.transform.localRotation = Quaternion.Euler(Elevation.transform.localEulerAngles.x, 0, 0);
    What this currently does is rotate the turret (its individual sections, the Azimuth GameObject and Elevation GameObject) into a specific direction, which all works well, however I noticed the rotation to not be at a constant speed, instead it eases in at the end of its movement. How could I normalize these rotations to be at a constant speed? (Would like to point out, the turret is a child of another gameobject that can rotate freely around, which is why this code could possibly seem unnecessarily complicated at first, but its the only solution I was able to come up with so far)

    Also its my first post here, hope Im doing it right
     
  2. SovPenguin

    SovPenguin

    Joined:
    Feb 26, 2021
    Posts:
    5
    Oh right, everything from Vector3 projectedPos is in the Update function if it matters
     
  3. RyanGarber

    RyanGarber

    Joined:
    May 20, 2021
    Posts:
    11
    You're using Lerp wrong. That said, that's not your fault -- pretty much everyone does, either accidentally or because they like the 'wrong' result.

    Lerp takes a Start Point, an End Point, and a Percentage from 0.0 - 1.0. When given those, it has no easing in or out. You're giving it a Current Point, an End Point, and a Speed. That's what MoveTowards is built for.

    Use MoveTowards with the exact same parameters and you'll have the result you're looking for.
     
    Last edited: May 23, 2023
    orionsyndrome likes this.
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Exactly, plain lerping is a static, absolute thing. Not a dynamic, relativistic one.
    To use a lerp you need to know two end states apriori, in order to interpolate between them.

    Most commonly, to produce a stable motion with lerp, one would have to know how long it would take, but this is impossible given that you're incrementally going there and that your target (or start) is constantly repositioning.

    Here's an ok video about this (watched it to confirm it).

     
  5. SovPenguin

    SovPenguin

    Joined:
    Feb 26, 2021
    Posts:
    5
    Did that (however with RotateTowards instead of MoveTowards) and it worked perfectly! Thanks a lot, your explanation of Lerp will also come in useful for sure.
     
  6. SovPenguin

    SovPenguin

    Joined:
    Feb 26, 2021
    Posts:
    5
    Will also take a look at this, thanks!