To detect a touch in Unity it's quite simple we just have to use Input.GetTouch()
and pass it an index.
using UnityEngine;
using System.Collections;
public class TouchExample : MonoBehaviour {
void Update() {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
//Do Stuff
}
}
}
or
using UnityEngine;
using System.Collections;
public class TouchExample : MonoBehaviour {
void Update() {
for(int i = 0; i < Input.touchCount; i++)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
//Do Stuff
}
}
}
}
These examples gets the touch of the last game frame.
Inside of the TouchPhase enum there are 5 different kind of TouchPhase's
For example to move the object this script is attached to across the screen based on touch.
public class TouchMoveExample : MonoBehaviour
{
public float speed = 0.1f;
void Update () {
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
}
}
}