# smooth rotation?

I want my character to rotate left or right depending on the keys pressed (left or right).
How can I make the rotation frame rate independent and smooth.

Here’s a script(not mine, took it from here http://forum.unity3d.com/threads/keyboard-controlled-rotation.142437/#post-974226)

``````// Smoothly tilts a transform towards a target rotation.
var smooth = 2.0;
var tiltAngle = 30.0;
function Update () {
var tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;
var tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
var target = Quaternion.Euler (tiltAroundX, 0, tiltAroundZ);
// Dampen towards the target rotation
transform.rotation = Quaternion.Slerp(transform.rotation, target,
Time.deltaTime * smooth);;
}
``````

is this the right way to do it?

transform.rotation = Quaternion.Euler (transform.rotation.eulerAngles.x, (transform.rotation.eulerAngles.y + 100f * Time.deltaTime * Input.GetAxis (“Horizontal”)), transform.rotation.eulerAngles.z);

After this, make sure you set the gravity of the Horizontal axis to a lower number, this will increase smoothness.
The settings for it can be found under Edit → Project Settings → Input

This should smoothly rotate the character around it’s y-axis, which is what I think you tried to do

edit: Or did you want to clamp the rotation between 2 values? The code I wrote allows you to turn the transform 360°

The following code will use SmoothDamp for smooth rotation and is frame independent (I only have the script for C#):

``````public static Vector4 ToVector4 (this Quaternion quaternion) {
return new Vector4 (quaternion.x, quaternion.y, quaternion.z, quaternion.w);
}

public static Quaternion ToQuaternion (this Vector4 vector) {
return new Quaternion (vector.x, vector.y, vector.z, vector.w);
}

public static Vector4 SmoothDamp (Vector4 current, Vector4 target, ref Vector4 currentVelocity, float smoothTime) {
float x = Mathf.SmoothDamp (current.x, target.x, ref currentVelocity.x, smoothTime);
float y = Mathf.SmoothDamp (current.y, target.y, ref currentVelocity.y, smoothTime);
float z = Mathf.SmoothDamp (current.z, target.z, ref currentVelocity.z, smoothTime);
float w = Mathf.SmoothDamp (current.w, target.w, ref currentVelocity.w, smoothTime);

return new Vector4 (x, y, z, w);
}

public static Quaternion SmoothDamp (Quaternion current, Quaternion target, ref Vector4 currentVelocity, float smoothTime) {
Vector4 smooth = SmoothDamp (current.ToVector4 (), target.ToVector4 (), ref currentVelocity, smoothTime);
return smooth.ToQuaternion ();
}
``````

How to use it:

``````private Quaternion currentRotation; // The current rotation
private Quaternion targetRotation; // The rotation it is going towards
private float rotationV; // Current rotation velocity
private float smoothTime = 0.5f; // Smooth value between 0 and 1

void Start () {
// Set currentRotation and targetRotation
currentRotation = transform.position;
targetRotation = someValue;
}

void Update () {
currentRotation = SmoothDamp (currentRotation, targetRotation, ref rotationV, smoothTime); // Smoothly change the currentRotation towards the value of targetRotation

transform.rotation = currentRotation; // Or whatever it is you are trying to rotate
}
``````