Accelerometer Input

Checked with version: 4.3

-

Difficulty: Beginner

Modern mobile devices have a built in accelerometer which allows them to know their orientation in 3D space. We can use the devices orientation to control objects in our games. In this video you will learn about the accelerometer and how to use it in a scene.

Accelerometer Input

Beginner Mobile & Touch

AccelerometerInput

Code snippet

using UnityEngine;
using System.Collections;

public class AccelerometerInput : MonoBehaviour 
{
    void Update () 
    {
        transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);
    }
}
#pragma strict

function Update () 
{
    transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);
}
import UnityEngine
import System.Collections


public class AccelerometerInput(MonoBehaviour):

    private def Update():
        transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z)

Related documentation