- Home /
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?
Answer by Razacx · Jul 18, 2014 at 01:42 AM
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°
Yes that works razacx. but in my case i want something else. my x and z position are to be set to 0 and y remains same. Using your code i cant get a smooth rotation
@Razacx This is making my object rotate smoothly, but can you help me get this: Rotate object smoothly by 90 deg on single keyboard input: if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.LeftArrow))
I tried editing the script but I am not able to understand how to use Quaternion.Euler
.
Answer by Ruben_Chris · Aug 03, 2017 at 01:40 PM
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
}
Your answer
Follow this Question
Related Questions
need my object to rotate between specific angles on the x axis 2 Answers
Rotation of an object with quaternion 1 Answer
Choppy rotation of character around y-axis 1 Answer
Get slerp to work just as LookAt(,Vector3.right) does 1 Answer
i want to get my to cube to rotate 90 degrees on the x-axis as it jumps 0 Answers