Search code examples
rotationunity-game-enginequaternions

Rotation in unity


I am trying to rotate a game object using the lerp command, however I am getting the following error:

error CS0029: Cannot implicitly convert type float' to UnityEngine.Quaternion'

This is the code I am using

box.rotation = Quaternion.AngleAxis(NewAngle, Vector3.right);
boxAngle = box.rotation.x;
targetAngle = 90.0f;
NewAngle = Mathf.Lerp(boxAngle, targetAngle, smooth * Time.deltaTime);

Could anyone point me in the right direction? What am I doing wrong?


Solution

  • Assuming box is an Transform, this should work, but if you're rotating only on one axis it will be much easier to use

    box.eulerAngles=new Vector3(NewAngle, 0f, 0f);
    

    Also, there other two potential issues:

    1. lerping after it's set - you've got a potential one frame lag
    2. gimbal lock or modulo 360 problems (think how will 350 lerp to 90 - it will go the long way around)

    How about:

    Quaternion targetAngle=Quaternion.Euler(90f, 0f, 0f);
    box.rotation=Quaternion.Lerp(box.rotation, targetAngle, Time.deltaTime*smooth);