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!

2 Answers

2

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);

you can find solution:

Because its my first project, i just went with a simple clicker thing. The only script I have is for the click counter. It's a 2D game and I didn't really assign any movement across the canvas other than the sprites changing to another picture because of animation. The sprites only start the animation when I press a button.