A problem of using Slerp to rotate object smoothly.

Hey guys,

I’m trying to use Qaternion.Slerp to rotate an object. It works fine, when I push a key my object rotates smoothly. However once I release the key, the object always turns back to start place, I just want it stops at where it should be after rotation rather than rotating back again, any suggestion would be appreciated.

public float RotationSpeed = 40.0f;
public float smooth = 2.0f;

void FixedUpdate()
    float pitch = 0;
    Quaternion targetRotation = Quaternion.identity;

    pitch = Input.GetAxis ("Pitch") * RotationSpeed;

    targetRotation = Quaternion.Euler(-pitch,0,0);
    rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation,targetRotation,Time.fixedDeltaTime*smooth);

Declare the pitch variable outside of FixedUpdate so it doesn’t reset to 0, then change pitch by constantly adding the Pitch axis. To make it frame independent you would also calculate it together with delta time, for instance:

pitch += Input.GetAxis ("Pitch") * RotationSpeed * Time.deltaTime;

An axis is a normalized value from -1 to 1, when an axis is neutral it returns on 0, this and the fact that you reset the pitch variable each fixed update is why you’ve been seeing the rotation go back to origin position.

I believe you might have to decrease RotationSpeed as well to get a convenient result. Good luck!