- Home /
How to create tilt control similar to temple run?
Hello. I'm creating an endless runner game, and I need to control the player to left and right by tilting the device, exactly as it happens in the game Temple Run.
I'm using this code:
movex=0;
iPx = Input.acceleration.y;
if (Mathf.Abs(iPx) > moveThreshold)
{
movex = Mathf.Sign(iPx) * Time.deltaTime * speed;
transform.Translate(-movex,0,0);
}
The problem is that the movements are not smooth as temple run, and the device must be tilted a lot to make effect.
I already googled a lot, but coudnt do it by myself.
thanks.
Comment
Answer by ramp · Nov 14, 2014 at 05:54 AM
Hi,use given below code for tilting a gameObject like Temple Run character.
using UnityEngine; using System.Collections;
public class AccelerometerInput : MonoBehaviour { void Update () { transform.Translate(Input.acceleration.x*Time. smoothDeltaTime*10, 0, 0); } }
Thanks Ram
Answer by azmat786n · Nov 30, 2012 at 08:57 PM
var dir:Vector3 = Vector3.zero;
var moveSpeed:float = 10.0f;
function Update() {
dir.x = -Input.acceleration.y * moveSpeed;
transform.Translate(dir.x,0,0);
}