- Home /
How do I make a 2D sprite smoothly rotate upright?
I'm making a 2D game, and I have a sprite that is lying flat on the ground, like a plank. I want it so that when I press a button, it smoothly rotates so it's upright. I know you would use Quaternions, but I'm not sure how. Any help? PS. I don't want it to rotate just 90 degrees, I want it so that it can rotate from any angle to face upright. Thanks!
You can do this via eulerAngles:
this.transform.eulerAngles = new Vector3();
Answer by robertbu · Jul 30, 2014 at 05:50 PM
Execute this line in either Update() or in a Coroutine:
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.identity, speed * Time.deltaTime);
'speed' is a variable you define or a constant you insert and is measured in degrees per second. If you want an eased rotation, change 'RotateTowards' to 'Slerp' and adjust the 'speed' downward a bunch.
Would this be in a condition?
if (Input.Get$$anonymous$$ey($$anonymous$$eycode.D)) { ... }