Rotating a 2D object when the phone is rotated

I am developing a 2D game. I want the character to rotate when the phone is tilted.

For example, if the phone is tilted left; the character would start rotating left.

How would I go about doing this?

You can get acceleration values by using Input.acceleration - based on the orientation of the phone, this will let you know how they are tilting it. You can find out more about that here.

Once you have worked out the orientation of the phone, you can rotate the character using the GameObject.transform.eulerAngles variable. This takes a Vector3, where the x/y/z is the rotation in the x/y/z axis.

So, if you just wanted to rotate your gameobject based on when they tilt left and right, your code might look something like:

float const ROTATE_AMOUNT = 2; // at full tilt, rotate at 2 degrees per update

float GetTiltValue() {
  float const TILT_MIN = 0.05f;
  float const TILT_MAX = 0.2f;
  // Work out magnitude of tilt
  float tilt = Mathf.Abs(Input.acceleration.x);

  // If not really tilted don't change anything
  if (tilt < TILT_MIN) {
    return 0;
  float tiltScale = (tilt - TILT_MIN) / (TILT_MAX - TILT_MIN);
  // Change scale to be negative if accel was negative
  if (Input.acceleration.x < 0) {
    return -tiltScale;
  } else {
    return tiltScale;


void Update() {
  float tiltValue = GetTiltValue();
  Vector3 oldAngles = this.transform.eulerAngles;
  this.transform.eulerAngles = new Vector3(oldAngles.x, oldAngles.y, oldAngles.z + (tiltValue * ROTATE_AMOUNT));

This code should rotate the game object it is attached to at different speeds, relating to how tilted the device is. Let me know if you have any questions!