# Accelerometer based input on android devices (480306)

**URL:** https://discussions.unity.com/t/accelerometer-based-input-on-android-devices-480306/480306
**Category:** Mobile
**Tags:** Scripting
**Created:** [July 3, 2012, 9:28am UTC](https://discussions.unity.com/t/accelerometer-based-input-on-android-devices-480306/480306 "2012-07-03T09:28:53Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ghostreddy](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@ghostreddy](https://discussions.unity.com/u/ghostreddy)
#### Post date: [July 3, 2012, 9:28am UTC](https://discussions.unity.com/t/accelerometer-based-input-on-android-devices-480306/480306/1 "2012-07-03T09:28:53Z")

</div>

Hi,  
How can i activate the animation or audio or any other when an android device is rotated to landscape left or landscape right, actually my idea is to move a car based on accelerometer so for that i need to activate the animation when moves fwd or bkwd.

Thanks!!!

---

<div class="post-metadata">

### Author: ![Afisicos](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/afisicos/32/131583_2.png) [@Afisicos](https://discussions.unity.com/u/Afisicos)
#### Post date: [July 3, 2012, 10:45am UTC](https://discussions.unity.com/t/accelerometer-based-input-on-android-devices-480306/480306/2 "2012-07-03T10:45:41Z")

</div>

Yes, you can do this.

Its easy. Try to find some information in the scripting reference about Input.acceleration or Input class in mainly.

---

<div class="post-metadata">

### Author: ![ghostreddy](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@ghostreddy](https://discussions.unity.com/u/ghostreddy)
#### Post date: [July 3, 2012, 11:43am UTC](https://discussions.unity.com/t/accelerometer-based-input-on-android-devices-480306/480306/3 "2012-07-03T11:43:11Z")

</div>

I haven’t found exact Input method for this issue…could you pls guide me…

Thanks!!!

---

<div class="post-metadata">

### Author: ![hafizmrozlan](https://avatars.discourse-cdn.com/v4/letter/h/b9e5f3/32.png) [@hafizmrozlan](https://discussions.unity.com/u/hafizmrozlan)
#### Post date: [July 3, 2012, 12:57pm UTC](https://discussions.unity.com/t/accelerometer-based-input-on-android-devices-480306/480306/4 "2012-07-03T12:57:40Z")

</div>

> **[Unity - Scripting API: Input](https://docs.unity3d.com/ScriptReference/Input.html)**

here you go, you can look under acceleration class.  
you are welcome btw. 🙂

---

<div class="post-metadata">

### Author: ![Adam-Buckner\_1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/adam-buckner_1/32/374478_2.png) [@Adam-Buckner\_1](https://discussions.unity.com/u/Adam-Buckner_1)
#### Post date: [July 5, 2012, 12:56pm UTC](https://discussions.unity.com/t/accelerometer-based-input-on-android-devices-480306/480306/5 "2012-07-05T12:56:01Z")

</div>

[Input.acceleration](http://docs.unity3d.com/Documentation/ScriptReference/Input-acceleration.html) returns a Vector3 value each frame.

> [@](#):
>
> static var acceleration : Vector3

You can use this value to “Do Something” in your game.

Something like this can be used to calculate a force to apply to an object:

```auto
var shipPower : float;
function FixedUpdate () {
   var acceleration : Vector3 = Input.acceleration;
   var thrust : float = Mathf.Clamp (acceleration.y * shipPower, 0, 10)
   rigidbody.AddRelativeForce (Vector3.up * thrust * Time.deltaTime, ForceMode.VelocityChange);
}

```

This way you can grab the value from the Input.acceleration and apply it to the rigidbody as a force.

You will need to poll your Input.acceleration for a value and apply it in your game to either trigger your animation or to drive your object.

---

<div class="post-metadata">

### Author: ![ghostreddy](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@ghostreddy](https://discussions.unity.com/u/ghostreddy)
#### Post date: [July 5, 2012, 1:44pm UTC](https://discussions.unity.com/t/accelerometer-based-input-on-android-devices-480306/480306/6 "2012-07-05T13:44:20Z")

</div>

since i am new to coding could you pls elaborate it…

Thanks!!!

---

<div class="post-metadata">

### Author: ![Adam-Buckner\_1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/adam-buckner_1/32/374478_2.png) [@Adam-Buckner\_1](https://discussions.unity.com/u/Adam-Buckner_1)
#### Post date: [July 5, 2012, 2:16pm UTC](https://discussions.unity.com/t/accelerometer-based-input-on-android-devices-480306/480306/7 "2012-07-05T14:16:31Z")

</div>

First: If you need some help with the basics of coding, I have a few suggestions:

- Look at: [The Getting Started with Coding thread in teaching](http://forum.unity3d.com/threads/113937-Getting-Started-with-Coding-for-Unity)
- Do a few of the tutorials listed in:[The List of Tutorials and Resources in teaching](http://forum.unity3d.com/threads/114813-List-of-Tutorials-and-Resources)

Between these two, you should get a basic handle on coding and the Unity API, just like we discussed in the [Online Training Class](http://unity3d.com/support/online-training/).

Second: Can you go into more detail and describe what exactly you are trying to do? This will allow us to be more targeted and less generic about our suggestions.

Lastly: Remember from our [Online Training Class](http://unity3d.com/support/online-training/): Variable hold values and Functions “Do things” to variables.

If you want to move a car based on key input, you would use something like:  
_[Code taken from “Input.GetAxis”](http://docs.unity3d.com/Documentation/ScriptReference/Input.GetAxis.html)_

```auto
var speed : float = 10.0;

function Update () {
    // Get the vertical axis.
    // By default they are mapped to the arrow keys.
    // The value is in the range -1 to 1
    var translation : float = Input.GetAxis ("Vertical") * speed;
    
    // Make it move 10 meters per second instead of 10 meters per frame...
    translation *= Time.deltaTime;
    
    // Move translation along the object's z-axis
    transform.Translate (0, 0, translation);
}

```

Now, with your mobile device, you seem to want to use the accelerometer to drive your object, not the keyboard. In this case, you want to use the values that come from the accelerometer rather than the keyboard:

```auto
var speed : float = 10.0;

function Update () {
    // Get the accelerometer data:
    var acceleration : Vector3 = Input.acceleration;
    // Get the forward value from one of the three channels in the acceleration data:
    var translation : float = acceleration.y * speed;
    
    // Make it move 10 meters per second instead of 10 meters per frame...
    translation *= Time.deltaTime;
    
    // Move translation along the object's z-axis
    transform.Translate (0, 0, translation);
}

```

This way, the value that you are using to move your object is coming from the accelerometer, rather than from the keyboard input.
