How to use Accelerometer in Unity

0 1,168

Learn how to use Accelerometer in Unity

Now you can easily implement motion sensing mechanism in your game using the accelerometer.

As you know that Motion sensing games are doing excellent in the market. Some of the top games include

  • Asphalt 6: is the best racing game where you get to race the car and beat the other street cars.
  • Racing Moto: It is a freestyle racing game in which you control your ride using the accelerometer.
  • Penguin skiing 3D is an Action packed 3D racing game in which you steer the penguin using acceleration.

You must be thrilled using these games and if you wish to develop your own motion-sensing game in unity then not to worry, you can add motion detection mechanism in your game using Accelerometer. First, let’s see what is an accelerometer and how does it help in motion detection mechanism.


Android provides different platforms that allow you to monitor the motion of a device. The accelerometer is a motion sensor which measures the orientation and tilting motion of an Android phone.

To control objects in our game we can use the device orientation. Nowadays modern Mobile phone systems have the built-in accelerometer that determines the device orientation.


We use Input.accleration to get input from the accelerometer.

using UnityEngine;
using System.Collections;
public class AccelerometerInput : MonoBehaviour {

                    void Update () {
                       transform.Translate(Input.acceleration.x, Input.acceleration.y, 0);



  • acceleration.x & Input.acceleration.y component are used to keep the cube moving.
  • Before getting directions from Input.acceleration we’ll do some pre-processing.
using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour
    // Use this for initialization
    public float Rotation;
    public float sensitivity = 8;
    public float smooth_obj = 0.6f;
    private Vector3 current_Acce, initial_Acce;

    void Start()
        initial_Acce = Input.acceleration;
        current_Acce =;

    // Update is called once per frame
    void Update()
        current_Acce = Vector3.Lerp(current_Acce, Input.acceleration - initial_Acce, Time.deltaTime / smooth_obj);
        Rotation = Mathf.Clamp(current_Acce.x * sensitivity, -1, 1);
        transform.Rotate(0, 0, -Rotation);

I have altered the Input.acceleration code instead of using it directly, now anyone of you can use it in your game.



Leave A Reply

Your email address will not be published.