Search code examples
unity-game-enginequaternionslerp

Quaternion.Lerp doesn't rotate object with exact degrees


I want to rotate my object 90 degrees with Quaternion.Lerp when user press the upArrow. But after each rotate I can see that it does not rotating my object exactly 90 degrees. approximately 88 degree it rotates. What is the problem? How can I solve this problem?

    if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        to.eulerAngles = transform.eulerAngles + new Vector3(90, 0, 0);
        from.eulerAngles = transform.eulerAngles;
        direction = "up";
    }

    if("up".Equals(direction))
        transform.rotation = Quaternion.Lerp(from, to, Time.deltaTime*10);

    if (transform.eulerAngles==to.eulerAngles)
    {
        direction = "";
    }

Solution

  • This is so weird.

    Just added

    transform.eulerAngles = transform.eulerAngles;
    

    into

    if (transform.eulerAngles==to.eulerAngles)
    {
        direction = "";
    }
    

    and solved. I think Unitys Transform system is not so sensitive, I test it in log, even its rotation was approximately 88 on log, in inspector it was 90. So I add this code and it is done.