Quaternion.Lerp not working as intended.

I am trying to rotate the character with a delayed rotation something like Paper Mario. i am using this to rotate the player in y axis.

Quaternion newRoatation = Quaternion.identity;
            newRoatation = Quaternion.Euler(0f,360f,0f);
            transform.rotation = Quaternion.Lerp(transform.rotation, newRoatation, 10f * Time.deltaTime);

when ever it rotates it ends up in y = -28.07

i dont understand it.

This is the full code.

 Rigidbody2D rb;
    Animator anim;
    public bool isGrounded,facingRight,flip,canFlip;
    public float charSpeed,jumpHeight,maxSpeed = 7f;
   
    Vector2 direction;
   
    // Start is called before the first frame update
    void Start()
    {
        facingRight = true;
        canFlip = true;
        anim = GetComponent<Animator>();
        rb = gameObject.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        direction = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Horizontal"));
        MoveChar(direction.x);
        //transform.Translate(Input.GetAxisRaw("Horizontal") * charSpeed * Time.deltaTime, 0f, 0f);


        if(Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D))
        {
            flip = true;
        }
       
        if (flip == true && canFlip == true)
        {
            Flip();
        }
       
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.W) && isGrounded)
        {
            rb.velocity = Vector2.up * jumpHeight;
            isGrounded = false;
        }
    }

    void MoveChar(float horizontal)
    {
        rb.AddForce(Vector2.right * horizontal * charSpeed);

        anim.SetFloat("magnitude", Mathf.Abs(rb.velocity.x));


       
        if(rb.velocity.x > maxSpeed)
        {
            rb.velocity = new Vector2(Mathf.Sign(rb.velocity.x) * maxSpeed, rb.velocity.y);
        }
    }
    void Flip()
    {
        if(facingRight == false)
        {
            Quaternion newRoatation = Quaternion.identity;
            newRoatation = Quaternion.Euler(0f,360f,0f);
            transform.rotation = Quaternion.Lerp(transform.rotation, newRoatation, 10f * Time.deltaTime);
            float angle = Quaternion.Angle(transform.rotation, newRoatation);
            Debug.Log(angle);
            if (angle == 0)
            {
                Debug.Log("flip done");
                transform.rotation = Quaternion.Euler(Vector3.zero);
                facingRight = true;
                flip = false;
            }
           
        }
        if (facingRight == true)
        {
            Quaternion newRoatation = Quaternion.Euler(0f, 180f, 0f);
            transform.rotation = Quaternion.Lerp(transform.rotation, newRoatation, 10f * Time.deltaTime);

            float angle = Quaternion.Angle(transform.rotation, newRoatation);
            if (angle == 0)
            {
                Debug.Log("flip done");
                transform.rotation = newRoatation;
                facingRight = false;
                flip = false;
            }
        }
    }

The t parameter in all Lerp functions isn’t intended to be used with a “value-over-time”, like other functions such as Mathf.MoveTowards would.

The value should be any number between 0 and 1, where:

  • A value of 0 will return the exact rotation value in the first parameter.
  • A value of 1 will return the exact rotation value in the second parameter.
  • A value of 0.5 will return exactly the half-way point between the two rotations in both parameters.

You probably want to use Quaternion.RotateTowards instead, which does work with a “value-over-time”.

1 Like

Hey, Thanks a lot but it doesn’t do what i want it to do. i fixed the problem by using elseif.

How did you fixed?