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

Lerp angle frame Independece issue.

Discussion in 'Scripting' started by ODINKONG, Mar 13, 2022.

  1. ODINKONG

    ODINKONG

    Joined:
    Nov 4, 2014
    Posts:
    111
    I'm looking for a good solution for a situation I have where I am trying to rotate an object on the Y axis over time to face a player

    Essentially what I have now is

    Code (CSharp):
    1. Vector3 finalLookDir = new Vector3(0, Mathf.LerpAngle(ogEuler.y, lookDir.y, speed *Time.deltaTime), 0);
    The issue with this is that when even when multiplying my Time.delta time the speed is not frame independent because, this is an exponential function.

    I'm wondering if there is a linear function that is similar to lerp angle in that it wraps 180 to -180. I've been trying to write my own, but for some reason I'm having trouble conceptualizing it.
     
  2. ODINKONG

    ODINKONG

    Joined:
    Nov 4, 2014
    Posts:
    111
    Also While I'm at it I'm having a similar issue at a different location in my code.

    Code (CSharp):
    1.  transform.rotation = Quaternion.Lerp(curRot, rotationTarget, Time.deltaTime * 8);
    2.  
    3. transform.rotation = Quaternion.RotateTowards(curRot, rotationTarget, Time.deltaTime * 100);
    Both of these are also not frame rate independent.

    All of these are being used in update of course.
     
  3. coder091

    coder091

    Joined:
    Feb 16, 2021
    Posts:
    23
    thats a wrong way to lerp.


    Code (CSharp):
    1. var timer = 0;
    2. var duration = 1; // lerp duration
    3. while(timer < duration){
    4. Vector3 finalLookDir = new Vector3(0, Mathf.LerpAngle(ogEuler.y, lookDir.y, timer/duration), 0);
    5. timer += Time.deltaTime;
    6. yield return null;
    7. }
     
  4. ODINKONG

    ODINKONG

    Joined:
    Nov 4, 2014
    Posts:
    111
    Yes I know that, but this function needs to be in update because it's constantly looking for the player.

    Anyway someone else solved it. Quarternion rotate towards was that l what I was looking for.
    Thanks for the help.