- Home /
Help with accelerometer (C#)
Hi Guys,
Basically i am trying to make a script to when i tilt my phone in the x axis my player moves right or left.
Thank You
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public float playerSpeed = 20.0f;
// Use this for initialization
void Start () {
transform.position = new Vector3(0 , 4, 0);
}
// Update is called once per frame
void Update () {
if (Input.acceleration.x){
transform.Translate(Vector3.right *Input.GetAxis("Horizontal") * playerSpeed);
}
}
}
Comment
Answer by tanoshimi · Apr 06, 2014 at 01:34 PM
Did you try the example in the documentation?
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
public float speed = 10.0F;
void Update() {
Vector3 dir = Vector3.zero;
dir.x = -Input.acceleration.y;
dir.z = Input.acceleration.x;
if (dir.sqrMagnitude > 1)
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate(dir * speed);
}
}
http://docs.unity3d.com/Documentation/ScriptReference/Input-acceleration.html
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
how to make rail and then make something move on it? 0 Answers
Getting My Character to Move 1 Answer
Android Shake 0 Answers
Distribute terrain in zones 3 Answers