Question by
Allin1gamer · Mar 03, 2017 at 04:25 PM ·
c#mobilecontrolsaccelerometer
How can I change controls to a accelerometer for mobile
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float speed = 15f;
public float mapWidth = 5f;
private Rigidbody2D rb;
void Start ()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate ()
{
float x = Input.GetAxis("Horizontal") * Time.fixedDeltaTime * speed;
Vector2 newPosition = rb.position + Vector2.right * x;
newPosition.x = Mathf.Clamp(newPosition.x, -mapWidth, mapWidth);
rb.MovePosition(newPosition);
}
void OnCollisionEnter2D ()
{
FindObjectOfType<GameManager>().EndGame();
}
}
Comment
Best Answer
Answer by sindam · Mar 03, 2017 at 05:05 PM
Use accelerometer code Void update() { transform.Translatr(Input.acceleration.x, o,Input.acceleration.z) } See in unity roll a ball tutorial below accelerometer code is there