Version: 2017.3 (switch to 2017.4)
LanguageEnglish
  • C#
  • JS

Script language

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

Touch.phase

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

Submission failed

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

Close

Cancel

public var phase: TouchPhase;
public TouchPhase phase;

Description

Describes the phase of the touch.

The touch phase refers to the action the finger has taken on the most recent frame update. Since a touch is tracked over its "lifetime" by the device, the start and end of a touch and movements in between can be reported on the frames they occur. The phase property can be used as the basis of a "switch' statement or as part of a more sophisitcated state handling system.

#pragma strict
public var startPos: Vector2;
public var direction: Vector2;
public var directionChosen: boolean;
function Update() {
	// Track a single touch as a direction control.
	if (Input.touchCount > 0) {
		var touch: Touch = Input.GetTouch(0);
		// Handle finger movements based on touch phase.
		switch (touch.phase) {
			// Record initial touch position.
			case TouchPhase.Began:
				startPos = touch.position;

directionChosen = false;

break; // Determine direction by comparing the current touch position with the initial one. case TouchPhase.Moved: direction = touch.position - startPos;

break; // Report that a direction has been chosen when the finger is lifted. case TouchPhase.Ended: directionChosen = true;

break; } } if (directionChosen) { // Something that uses the chosen direction... } }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public Vector2 startPos; public Vector2 direction; public bool directionChosen; void Update() { // Track a single touch as a direction control. if (Input.touchCount > 0) { Touch touch = Input.GetTouch(0);

// Handle finger movements based on touch phase. switch (touch.phase) { // Record initial touch position. case TouchPhase.Began: startPos = touch.position; directionChosen = false; break;

// Determine direction by comparing the current touch position with the initial one. case TouchPhase.Moved: direction = touch.position - startPos; break;

// Report that a direction has been chosen when the finger is lifted. case TouchPhase.Ended: directionChosen = true; break; } } if (directionChosen) { // Something that uses the chosen direction... } } }

Did you find this page useful? Please give it a rating: