Hello everybody!
I really need your help cause I’m trying to create an android game based on the space shooter example of Unity, and I need to move the spaceship(player) using the tilt of the device. This is the code that I have used, but it doesn’t work… (the spaceship only rotates but doesn’t move along the x and z axes as I want…)
Please help mee
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Boundary boundary;
void FixedUpdate ()
{
float moveHorizontal = Input.acceleration.x;
float moveVertical = -Input.acceleration.z;
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rigidbody.velocity = movement * speed;
transform.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
);
rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}
In the Unity manual there’s this code but I don’t know how to use it (well I 've tryed but it didn’t work…)
transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);