- Home /
How to use accelerometer in a 2d game
Hello,
I'm trying to make a new game for android and I need the character to move left and right when you move your phone left and right. I konw that there is a script called "AccelerometerInput" which is in the unity tutorials (I think). The problem is that it also moves the character in the z axis but I only want it to move in the x axis (left and right).
This is the script:
using UnityEngine;
using System.Collections;
public class AccelerometerInput : MonoBehaviour
{
void Update ()
{
transform.Translate(Input.acceleration.x, 0, Input.acceleration.y);
}
}
I would like to know if there is a script that works well or there is a way to change this script to make it the way I want it. I'm pretty sure this is a stupid question but I'm new on scripting.
Thanks in Advance.
Answer by moodymagyar · Sep 19, 2015 at 08:53 PM
@Mario15gl I would suggest removing the Input.acceleration.y as the third argument in transform.Translate() and replace it with 0. I hope that helps.
Thanks, but I've already found another script that works for me:
#pragma strict
function Update () {
var dir : Vector3 = Vector3.zero;
dir.x = Input.acceleration.x*.5;
transform.position.x += dir.x;
}
I will check if your suggestions work anyways.
Certainly helped me! You're a legend! I couldn't figure out why my character was disappearing off into the background, was hard to tell in 2D mode. Thanks.