I’m currently developing a running game for the Android, and what I want to do is make a reference to whether the player is tilting his/her phone left or right. I don’t know how this would be done on the android, as I am new to the system… Here is the PC version of the script I made:
public class PlayerRotate : MonoBehaviour {
public float rotationRate = 10f;
public Quaternion startRotation = Quaternion.identity;
void Awake(){
transform.rotation = startRotation; //Set rotation to 0,0,0 at start.
}
// Use this for initialization
void Start () {
if(Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.Rotate(0f,0f,-5f);
}
if(Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
{
transform.Rotate(0f,0f,-5f);
}
}
// Update is called once per frame
void Update () {
//Rotate player accordingly
if(Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.Rotate(0f,0f,5f);
}
else if (Input.GetKeyUp(KeyCode.A)| Input.GetKeyUp(KeyCode.LeftArrow))
{
transform.Rotate(0f,0f,-5f);
}
if(Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
{
transform.Rotate(0f,0f,-5f);
}
else if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.RightArrow))
{
transform.Rotate(0f,0f,5f);
}
}
}
It is very easy to do with the PC as you can just tell the player to rotate once a key has been pressed. But with the android, there is no key being pressed… I do not know exactly how to go about this one…