- Home /
Question by
andre_95 · Aug 19, 2017 at 04:14 PM ·
rotationcharacter controllerquaternionsinfinite runner
How to do a smooth rotation of the character
I'm making a infinite runner. My character is already moving forward by itself.
This is the code i have to the rotation (on Update):
moveDirection.x = Input.GetAxisRaw ("Horizontal") * runningSpeed;
if (Input.GetKey (KeyCode.LeftArrow)) {
transform.eulerAngles = new Vector3 (0, -45, 0);
}
if (Input.GetKeyUp (KeyCode.LeftArrow))
transform.eulerAngles = Vector3.zero;
if (Input.GetKey (KeyCode.RightArrow)) {
transform.eulerAngles = new Vector3 (0, 45, 0);
}
if (Input.GetKeyUp (KeyCode.RightArrow))
transform.eulerAngles = Vector3.zero;
Now, what i would like to see is making the rotation smooth. I don't understand quite the Quaternions.
Thanks, in advance!
Comment
Answer by Nomenokes · Aug 21, 2017 at 04:07 PM
transform.rotation = Quaternion.Lerp(
transform.rotation,
Quaternion.Euler(0, 1, 0), //change for different rotations
speed
);
That should work
@Nomenokes Thanks, it works when the key is pressed, but not when i release the key!
On the return, it's supposed to be like this, right?
transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.Euler (0, 0, 0), speed);
Answer by PankajOnUnity · Apr 25, 2018 at 05:54 PM
you can find solution: https://youtu.be/TORjRsNJMWc
Your answer
![](https://koobas.hobune.stream/wayback/20220612133102im_/https://answers.unity.com/themes/thub/images/avi.jpg)