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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Quaternion.Lerp not working properly

Discussion in 'Scripting' started by unity_user_12345, Aug 10, 2015.

  1. unity_user_12345

    unity_user_12345

    Joined:
    May 21, 2015
    Posts:
    14
    I'm using Quaternion.Lerp to rotate a joint with a certain angle, but when I use the below code I find the joint rotating very far although I specified it to rotate with only 5. Any thoughts please?

    Vector3 newRot.y += currentTransform.localRotation.y +5f;
    blendWeight =0;
    if( blendWeight <1)
    {
    animationRotation = currentTransform.transform.localRotation;
    newRotation =Quaternion.Euler(newRot.x, newRot.y, newRot.z);
    blendWeight +=Time.deltaTime/0.9f;
    currentTransform.transform.localRotation =Quaternion.Lerp(animationRotation,newRotation, blendWeight);​
    }
     
    Last edited: Aug 10, 2015
  2. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Next time use code tags as it's easier to read ^-^

    First off, if it were me I would use Quaternion.Slerp instead of Lerp. This is because the docs say Lerp looks worse when rotations are far apart. (Probably at the expense of a little performance) Secondly, if you are updating newRot.y for every frame, of course it will go more than 5. Since currentTransform is changing every frame (due to the Lerp) and you are adding to that + 5 every frame, then it will increase beyond just 5. So you should set your newRot only once at the start.

    Third, your first parameter of Quatnerion.Lerp should be the initial. But yet you are updating it every frame because you're setting it to the localRotation. So instead, at the same time you should be setting newRot only once at start, you should set animationRotation only once at start. That way it can move between the two at the correct blendweight instead of setting a new initial rotation every frame. (It still works the other way, but you end up with an exponential blend, which is ugly--it never actually reaches newRotation--and is probably not what you want) Well maybe exponential isn't ugly considering it's an animation joint haha but it's probably bad practice. If you still want exponential might be a better idea to add exponentially to blendweight instead of resetting initial every frame.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Typically you use EulerAngles instead of directly adding to a quaternion value.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    "currentTransform.localRotation.y +5f;" isn't anything that makes any sense. Quaternions are 4D, with each element ranging from -1.0 to 1.0, and the y element isn't related to the Y axis. Also, to use Lerp correctly, you need to advance the third parameter from 0.0 to 1.0 over time while keeping the first and second parameters the same; your code does something quite different from that. I'd recommend using coroutines instead of Update for Lerp, since they're easier to start and stop when needed. See here for an example of slerping over time with a coroutine.

    --Eric
     
    blizzy and Kiwasi like this.
  5. unity_user_12345

    unity_user_12345

    Joined:
    May 21, 2015
    Posts:
    14
    Could you please advise how?
     
  6. unity_user_12345

    unity_user_12345

    Joined:
    May 21, 2015
    Posts:
    14
    but like if I didn't use the lerp and only used this "currentTransform.Rotate(newRot);" it works as anticipated and the limb's joint only rotates a little bit, but it kinds of jump directly to the new rotation without smooth transition. So not sure how can I apply this rotation with smooth rotations.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Click on the link I posted.

    --Eric