Unity Learn home
View Tutorial Content
Steps

Getting Mobile Input

Tutorial
Beginner
15 Mins
(6)
Summary
Modern mobile devices have screens that can receive accurate multitouch inputs from the user and from multiple device sensors. In this tutorial you will learn how to get inputs from the touchscreen and accelerometer, and build a basic "pinch to zoom" mechanic.
Select your Unity version
Last updated: December 09, 2021
4.x
Language
English

1.Multi Touch Input

Modern mobile devices have screen that can receive accurate multitouch inputs from the user. In this video you will learn how to access these touches from the Input class.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)

TouchTest

using UnityEngine; using System.Collections; public class TouchTest : MonoBehaviour { void Update () { Touch myTouch = Input.GetTouch(0); Touch[] myTouches = Input.touches; for(int i = 0; i < Input.touchCount; i++) { //Do something with the touches } } }

js

#pragma strict function Update () { var myTouch : Touch = Input.GetTouch(0); var myTouches = Input.touches; for(var i = 0; i < Input.touchCount; i++) { //Do something with the touches } }

boo

import UnityEngine import System.Collections public class TouchTest(MonoBehaviour): private def Update(): myTouch as Touch = Input.GetTouch(0) myTouches as (Touch) = Input.touches for i in range(0, Input.touchCount): pass //Do something with the touches

2.Accelerometer Input

Modern mobile devices have a built in accelerometer which allows them to know their orientation in 3D space. We can use the devices orientation to control objects in our games. In this video you will learn about the accelerometer and how to use it in a scene.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)

AccelerometerInput

using UnityEngine; using System.Collections; public class AccelerometerInput : MonoBehaviour { void Update () { transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z); } }

js

#pragma strict function Update () { transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z); }

boo

import UnityEngine import System.Collections public class AccelerometerInput(MonoBehaviour): private def Update(): transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z)

3.Pinch to Zoom

In this video you will learn to make a basic pinch to zoom mechanic using the multitouch inputs of a mobile device.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)

PinchZoom

using UnityEngine; public class PinchZoom : MonoBehaviour { public float perspectiveZoomSpeed = 0.5f; // The rate of change of the field of view in perspective mode. public float orthoZoomSpeed = 0.5f; // The rate of change of the orthographic size in orthographic mode. void Update() { // If there are two touches on the device... if (Input.touchCount == 2) { // Store both touches. Touch touchZero = Input.GetTouch(0); Touch touchOne = Input.GetTouch(1); // Find the position in the previous frame of each touch. Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition; Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition; // Find the magnitude of the vector (the distance) between the touches in each frame. float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude; float touchDeltaMag = (touchZero.position - touchOne.position).magnitude; // Find the difference in the distances between each frame. float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag; // If the camera is orthographic... if (camera.isOrthoGraphic) { // ... change the orthographic size based on the change in distance between the touches. camera.orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed; // Make sure the orthographic size never drops below zero. camera.orthographicSize = Mathf.Max(camera.orthographicSize, 0.1f); } else { // Otherwise change the field of view based on the change in distance between the touches. camera.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed; // Clamp the field of view to make sure it's between 0 and 180. camera.fieldOfView = Mathf.Clamp(camera.fieldOfView, 0.1f, 179.9f); } } } }

js

#pragma strict public var perspectiveZoomSpeed : float = 0.5f; // The rate of change of the field of view in perspective mode. public var orthoZoomSpeed : float = 0.5f; // The rate of change of the orthographic size in orthographic mode. function Update() { // If there are two touches on the device... if (Input.touchCount == 2) { // Store both touches. var touchZero = Input.GetTouch(0); var touchOne = Input.GetTouch(1); // Find the position in the previous frame of each touch. var touchZeroPrevPos = touchZero.position - touchZero.deltaPosition; var touchOnePrevPos = touchOne.position - touchOne.deltaPosition; // Find the magnitude of the vector (the distance) between the touches in each frame. var prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude; var touchDeltaMag = (touchZero.position - touchOne.position).magnitude; // Find the difference in the distances between each frame. var deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag; // If the camera is orthographic... if (camera.isOrthoGraphic) { // ... change the orthographic size based on the change in distance between the touches. camera.orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed; // Make sure the orthographic size never drops below zero. camera.orthographicSize = Mathf.Max(camera.orthographicSize, 0.1f); } else { // Otherwise change the field of view based on the change in distance between the touches. camera.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed; // Clamp the field of view to make sure it's between 0 and 180. camera.fieldOfView = Mathf.Clamp(camera.fieldOfView, 0.1f, 179.9f); } } }

boo

import UnityEngine public class PinchZoom(MonoBehaviour): public perspectiveZoomSpeed as single = 0.5F // The rate of change of the field of view in perspective mode. public orthoZoomSpeed as single = 0.5F // The rate of change of the orthographic size in orthographic mode. private def Update(): // If there are two touches on the device... if Input.touchCount == 2: // Store both touches. touchZero as Touch = Input.GetTouch(0) touchOne as Touch = Input.GetTouch(1) // Find the position in the previous frame of each touch. touchZeroPrevPos as Vector2 = (touchZero.position - touchZero.deltaPosition) touchOnePrevPos as Vector2 = (touchOne.position - touchOne.deltaPosition) // Find the magnitude of the vector (the distance) between the touches in each frame. prevTouchDeltaMag as single = (touchZeroPrevPos - touchOnePrevPos).magnitude touchDeltaMag as single = (touchZero.position - touchOne.position).magnitude // Find the difference in the distances between each frame. deltaMagnitudeDiff as single = (prevTouchDeltaMag - touchDeltaMag) // If the camera is orthographic... if camera.isOrthoGraphic: // ... change the orthographic size based on the change in distance between the touches. camera.orthographicSize += (deltaMagnitudeDiff * orthoZoomSpeed) // Make sure the orthographic size never drops below zero. camera.orthographicSize = Mathf.Max(camera.orthographicSize, 0.1F) else: // Otherwise change the field of view based on the change in distance between the touches. camera.fieldOfView += (deltaMagnitudeDiff * perspectiveZoomSpeed) // Clamp the field of view to make sure it's between 0 and 180. camera.fieldOfView = Mathf.Clamp(camera.fieldOfView, 0.10000000149F, 179.9F)

Getting Mobile Input
Getting Mobile Input
General Tutorial Discussion
0
0
1. Multi Touch Input
0
0
2. Accelerometer Input
0
0
3. Pinch to Zoom
0
0