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 Quaternion.Lerp not working as intended.

Discussion in '2D' started by Arnav_Sanghavi, Aug 4, 2020.

  1. Arnav_Sanghavi

    Arnav_Sanghavi

    Joined:
    Feb 2, 2019
    Posts:
    7
    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.
    Code (CSharp):
    1. Quaternion newRoatation = Quaternion.identity;
    2.             newRoatation = Quaternion.Euler(0f,360f,0f);
    3.             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.

    Code (CSharp):
    1.  Rigidbody2D rb;
    2.     Animator anim;
    3.     public bool isGrounded,facingRight,flip,canFlip;
    4.     public float charSpeed,jumpHeight,maxSpeed = 7f;
    5.    
    6.     Vector2 direction;
    7.    
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         facingRight = true;
    12.         canFlip = true;
    13.         anim = GetComponent<Animator>();
    14.         rb = gameObject.GetComponent<Rigidbody2D>();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void FixedUpdate()
    19.     {
    20.         direction = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Horizontal"));
    21.         MoveChar(direction.x);
    22.         //transform.Translate(Input.GetAxisRaw("Horizontal") * charSpeed * Time.deltaTime, 0f, 0f);
    23.  
    24.  
    25.         if(Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D))
    26.         {
    27.             flip = true;
    28.         }
    29.        
    30.         if (flip == true && canFlip == true)
    31.         {
    32.             Flip();
    33.         }
    34.        
    35.     }
    36.     private void Update()
    37.     {
    38.         if (Input.GetKeyDown(KeyCode.W) && isGrounded)
    39.         {
    40.             rb.velocity = Vector2.up * jumpHeight;
    41.             isGrounded = false;
    42.         }
    43.     }
    44.  
    45.     void MoveChar(float horizontal)
    46.     {
    47.         rb.AddForce(Vector2.right * horizontal * charSpeed);
    48.  
    49.         anim.SetFloat("magnitude", Mathf.Abs(rb.velocity.x));
    50.  
    51.  
    52.        
    53.         if(rb.velocity.x > maxSpeed)
    54.         {
    55.             rb.velocity = new Vector2(Mathf.Sign(rb.velocity.x) * maxSpeed, rb.velocity.y);
    56.         }
    57.     }
    58.     void Flip()
    59.     {
    60.         if(facingRight == false)
    61.         {
    62.             Quaternion newRoatation = Quaternion.identity;
    63.             newRoatation = Quaternion.Euler(0f,360f,0f);
    64.             transform.rotation = Quaternion.Lerp(transform.rotation, newRoatation, 10f * Time.deltaTime);
    65.             float angle = Quaternion.Angle(transform.rotation, newRoatation);
    66.             Debug.Log(angle);
    67.             if (angle == 0)
    68.             {
    69.                 Debug.Log("flip done");
    70.                 transform.rotation = Quaternion.Euler(Vector3.zero);
    71.                 facingRight = true;
    72.                 flip = false;
    73.             }
    74.            
    75.         }
    76.         if (facingRight == true)
    77.         {
    78.             Quaternion newRoatation = Quaternion.Euler(0f, 180f, 0f);
    79.             transform.rotation = Quaternion.Lerp(transform.rotation, newRoatation, 10f * Time.deltaTime);
    80.  
    81.             float angle = Quaternion.Angle(transform.rotation, newRoatation);
    82.             if (angle == 0)
    83.             {
    84.                 Debug.Log("flip done");
    85.                 transform.rotation = newRoatation;
    86.                 facingRight = false;
    87.                 flip = false;
    88.             }
    89.         }
    90.     }
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    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".
     
    Arnav_Sanghavi likes this.
  3. Arnav_Sanghavi

    Arnav_Sanghavi

    Joined:
    Feb 2, 2019
    Posts:
    7
    Hey, Thanks a lot but it doesn't do what i want it to do. i fixed the problem by using elseif.
     
  4. GameDeveloperAf

    GameDeveloperAf

    Joined:
    Jul 3, 2020
    Posts:
    71
    How did you fixed?