Rotation speed of the player (Paper Mario like)
I want to create a small Paper Mario like 3D game in unity. I can move in all four directions and if I change the horizontal direction, the player sprite rotates (flips). But it looks like it is just mirrored because the rotation is way too fast.
This is my code (only the important stuff):
void Update ()
{
if(Input.GetKey("right"))
{
transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime, 0f, 0f)); // x, y, z
transform.localRotation = Quaternion.Euler(0, 0, 0);
}
if(Input.GetKey("left"))
{
transform.Translate(new Vector3(-Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime, 0f, 0f)); // x, y, z
transform.localRotation = Quaternion.Euler(0, 180, 0);
}
}
The player should only flip when the right and left arrows are being used and it should happen once! So if the player is looking to the left and I go right, the sprite rotates quickly so the player looks (and walks) to the right. I already tried something like
transform.Rotate(0, 180 rotateSpeed Time.deltaTime, 0);
But the player rotates in all directions as long I press the left arrow key. So overall I receive some weird movements.
Answer by NeilM0 · May 06, 2016 at 02:58 PM
Try using something along the lines of:
Quaternion desiredRotation = Quaternion(0,0,0);
void Update()
{
transform.localRotation = Quaternion.RotateTowards(transform.localRotation, desiredRotation, Time.deltaTime * speed);
}