In my (Google Cardboard) Android app I would like to make my camera move on Touch. That movement state should be controlled via a "switch", so when I touch the screen the camera starts to move and when I tap again it stops. To achieve that I appended this script to the camera:
using UnityEngine;
using System.Collections;
public class TapControl : MonoBehaviour {
public float speed;
public int state = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
state += 1;
}
if (state % 2 == 0)
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
}
}
The state-integer increases every touch. But nonethless, the camera's behavior can be described as a "hold-touch to move" when the state-variable is even.
So, to make it short: Can anyone detect any programming mistakes i made that could cause this strange behavior?