# Smooth 90 degree rotation on keypress

Hi Unity Anwsers,

I’m trying to make a C# script that’ll rotate a GameObject 90 degress og -90 degress on the y-axis over 1 second when I press a button. I’ve searched around, but I can’t find anything that works.

So far, I’ve made a float that holds rotation, and set it to 90f. Then I’ve put a transform.Rotate in my ‘update’ function when I press the keys ‘e’ or ‘q’ However, this turns the object immediately and not over time - can anybody help med add the time to my code?

``````public float rotation = 90f;

void Update () {
if (Input.GetKeyDown ("e")) {
transform.Rotate (0, rotation, 0);
}
if (Input.GetKeyDown ("q")) {
transform.Rotate (0, -(rotation), 0);
}
}
``````

Thank you so much @Mmmpies.

That did the trick! My final code ended up looking like this:

``````	IEnumerator RotateMe(Vector3 byAngles, float inTime)
{	var fromAngle = transform.rotation;
var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
for(var t = 0f; t < 1; t += Time.deltaTime/inTime) {
transform.rotation = Quaternion.Slerp(fromAngle, toAngle, t);
yield return null;
}
}
void Update () {
if(Input.GetKeyDown("e")){
StartCoroutine(RotateMe(Vector3.up * 90, 0.8f));
}
if(Input.GetKeyDown("q")){
StartCoroutine(RotateMe(Vector3.up * -90, 0.8f));
}
}
}
``````

i know this an old post but it helped me and will help others i altered it a bit to stop you rotating till it had finished or you could get odd angles

``````bool rotating;
IEnumerator RotateMe(Vector3 byAngles, float inTime)
{
var fromAngle = transform.rotation;
var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
for (var t = 0f; t <= 1; t += Time.deltaTime / inTime)
{
transform.rotation = Quaternion.Slerp(fromAngle, toAngle, t);

yield return null;
}

transform.rotation = toAngle;
rotating = false;
}
void Update()
{

if (Input.GetKeyDown("e") && !rotating)
{
rotating = true;
StartCoroutine(RotateMe(Vector3.up * 90, 0.8f));
}
if (Input.GetKeyDown("q") && !rotating)
{
rotating = true;
StartCoroutine(RotateMe(Vector3.up * -90, 0.8f));
}

}
``````

I noticed as I run this code the object (cube in my case) doesn’t rotate a complete 90 degrees. It falls short on each key press. Any idea why?