Legacy Documentation: Version 4.6.2
Language: English
  • C#
  • JS
  • Boo

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Input.GetTouch

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static function GetTouch(index: int): Touch;
public static Touch GetTouch(int index);
public static def GetTouch(index as int) as Touch

Description

Returns object representing status of a specific touch. (Does not allocate temporary variables).

	// Moves object according to finger movement on the screen

var speed : float = 0.1; function Update () { if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) { // Get movement of the finger since last frame var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition; // Move object across XY plane transform.Translate (-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0); } }
using UnityEngine;
using System.Collections;

public class ExampleClass : 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);
        }
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public speed as float = 0.1F

	def Update() as void:
		if (Input.touchCount > 0) and (Input.GetTouch(0).phase == TouchPhase.Moved):
			touchDeltaPosition as Vector2 = Input.GetTouch(0).deltaPosition
			transform.Translate(((-touchDeltaPosition.x) * speed), ((-touchDeltaPosition.y) * speed), 0)

Another example:

	// Instantiates a projectile whenever the user taps on the screen

var projectile : GameObject; function Update () { for (var i = 0; i < Input.touchCount; ++i) { if (Input.GetTouch(i).phase == TouchPhase.Began) { clone = Instantiate (projectile, transform.position, transform.rotation); } } }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public GameObject projectile;
    void Update() {
        int i = 0;
        while (i < Input.touchCount) {
            if (Input.GetTouch(i).phase == TouchPhase.Began)
                clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
            
            ++i;
        }
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public projectile as GameObject

	def Update() as void:
		i as int = 0
		while i < Input.touchCount:
			if Input.GetTouch(i).phase == TouchPhase.Began:
				clone = (Instantiate(projectile, transform.position, transform.rotation) as GameObject)
			++i

	// Shoot a ray whenever the user taps on the screen
	var particle : GameObject;
	function Update () {
		for (var i = 0; i < Input.touchCount; ++i) {
			if (Input.GetTouch(i).phase == TouchPhase.Began) {
				// Construct a ray from the current touch coordinates
				var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
				if (Physics.Raycast (ray)) {
					// Create a particle if hit
					Instantiate (particle, transform.position, transform.rotation);
				}
			}
		}
	}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public GameObject particle;
    void Update() {
        int i = 0;
        while (i < Input.touchCount) {
            if (Input.GetTouch(i).phase == TouchPhase.Began) {
                Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
                if (Physics.Raycast(ray))
                    Instantiate(particle, transform.position, transform.rotation) as GameObject;
                
            }
            ++i;
        }
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public particle as GameObject

	def Update() as void:
		i as int = 0
		while i < Input.touchCount:
			if Input.GetTouch(i).phase == TouchPhase.Began:
				ray as Ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position)
				if Physics.Raycast(ray):
					(Instantiate(particle, transform.position, transform.rotation) as GameObject)
			++i