Unity 3D Game Engine – Android – Swipe – Screen – Move Object

Translate a GameObject swiping screen

Hierarchy, create a scene with:

– Main Camera
– Cube (Game Object), attach the script ‘TouchController.js’

TouchController.js


#pragma strict

// Moves object according to finger movement on the screen
	var speed : float = 3.0;
	function Update () {
	    // Se si sta toccando lo schermo e la fase è Moved (il dito sta strisciano)
		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
			// Time.deltaTime it move xxx meters per second instead of xxx meters per frame
			transform.Translate (touchDeltaPosition.x * Time.deltaTime * speed, touchDeltaPosition.y * Time.deltaTime * speed, 0);
		}
	}

Hierarchy> Cube> Inspector> TouchController.js setup public var speed:

> speed > movement
< speed < movement