Question by
glennuke · May 22, 2021 at 12:45 PM ·
rigidbodytransform.rotate
Smooth Rotation
I'm working on an ocean game and i need help with smooth rotation, Heres the code for my current rotation
if (Input.GetKey("d"))
{
transform.Rotate(Vector3.up);
}
if (Input.GetKey("a"))
{
transform.Rotate(Vector3.down);
}
Comment
This is not how Rotate
works.
It comes in 6 variants:
public void Rotate ( float xAngle , float yAngle , float zAngle )
public void Rotate ( Vector3 eulers , Space relativeTo )
public void Rotate ( Vector3 eulers )
public void Rotate ( float xAngle , float yAngle , float zAngle , Space relativeTo )
public void Rotate ( Vector3 axis , float angle , Space relativeTo )
public void Rotate ( Vector3 axis , float angle )
All eulers
and angle
are in degrees here.
[SerializeField] float _rotationSpeed = 1f;
void Update ()
{
Vector2 moveInput = new Vector2( Input.GetAxis("Horizontal") , Input.GetAxis("Vertical") );
if( moveInput.magnitude>0f )
{
Transform cameraTransform = Camera.main.transform;
Vector3 lookDir = Vector3.ProjectOnPlane( cameraTransform.right * moveInput.x + cameraTransform.forward * moveInput.y , Vector3.up );
Quaternion src = transform.rotation;
Quaternion dst = Quaternion.LookRotation( lookDir , Vector3.up );
transform.rotation = Quaternion.Slerp( src , dst , _rotationSpeed * Time.deltaTime );
}
}