# How do I rotate an object at a constant rate a specific angle on key down?

I want to rotate an object 180 degrees (turn around) at a constant rate when either the ‘A’ or ‘D’ keys are pressed down to turn left or right respectively. This is the code that I want it to work with:

``````public float rotationSpeed = 50;
void Update() {
if (Input.GetKeyDown(KeyCode.A)) {
// Turn left 180 degrees at 50 degrees per second
}
if (Input.GetKeyDown(KeyCode.D)) {
// Turn left 180 degrees at 50 degrees per second
}
}
``````

How would I go about implementing this?

If you want it to stop at 180… Quick and dirty idea for you:
Note - this is done quickly on iPad so you will have to actually put the below into code and test BUT it should do what you need!

``````rotationTracker = 0
rotationModifier = 1

Function update()
If keycode.a
rotationModifier =1

If keycode.d
rotationModifier =-1

if (rotationTracker>-180)&&(rotationTracker<180)
rotationTracker += Time.deltaTime x rotationModifier
transform.Rotate(0, rotationModiferx50xTime.deltaTime, 0)
``````

Here is some rotation code. Note that RotateTowards() takes the shortest angle. When it is exactly 180 degrees, the direction is arbitrary. You can set the direction of rotation by an angle that is every so slightly more/less than 180 for your rotation. But it’s going to turn forward and then turn back on the same path, it’s not going to turn all the way around around. A few lines of code can change that.

@MickM approach will also work (and may be a bit more flexible) but I think there is a bug in his logic. It will quit rotating when the angle is >= 180, but nothing gets it back in bounds. Easy fix.

``````using UnityEngine;
using System.Collections;

public class Turn180 : MonoBehaviour {

public float speed = 50.0f;
private Quaternion qTo = Quaternion.identity;
private Quaternion qForward = Quaternion.identity;
private Quaternion qBack = Quaternion.Euler (0.0f, 180.0f, 0.0f);

void Start () {
}

// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.A))
qTo = qBack;
else if (Input.GetKeyDown (KeyCode.D))
qTo = qForward;

transform.rotation = Quaternion.RotateTowards (transform.rotation, qTo, Time.deltaTime * speed);
}
}
``````

I have an object that I want to rotate left, right, forward and backward using the arrow keys. It works, but my beta testers are saying the motion is too linear and want to be able to control the amount of rotation more.

I thought of doing something like adding exponential interpolation or Quaternion, but don’t know how to implement it.

What I want to happen is have it to where when an arrow key is pressed, the object starts rotating in that direction, but slowly at first, then exponentially getting faster the longer its held down.

My code so far:

public class Tilt : MonoBehaviour {

``````public int tiltSpeed = 1;

void FixedUpdate () {

if(Input.GetButton("Tilt Forwards")) {
transform.Rotate(tiltSpeed * 1,0,0);
Debug.Log("Up Arrow Key Pressed!");
}

if(Input.GetButton("Tilt Backwards")) {
transform.Rotate(tiltSpeed * -1,0,0);
Debug.Log("Down Arrow Key Pressed!");
}

if(Input.GetButton("Tilt Left")) {
transform.Rotate(0,0,tiltSpeed);
Debug.Log("Left Arrow Key Pressed!");
}

if(Input.GetButton("Tilt Right")) {
transform.Rotate(0,0,tiltSpeed * -1);
Debug.Log("Right Arrow Key Pressed!");
}
}
``````

}