- Home /
How do I detect where touches are (Mobile)
In the game I am making, I want the player to move left when I touch the left side of the screen and move right when I touch the right side of the screen. I have tried using buttons, but On Click() only detects when it is touched and not how long its touched. I watched the documentation and I only found out how to detect a touch, but not where it touches.
It's pretty simple because it's mentioned directly in the documentation how to do this: here and here
using UnityEngine;
using System.Collections;
public class ExampleClass : $$anonymous$$onoBehaviour {
void Update() {
int fingerCount = 0;
foreach (Touch touch in Input.touches) {
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
{
fingerCount++;
Debug.Log(touch.position.ToString()); // touch.position will give you a vector2
}
}
if (fingerCount > 0)
print("User has " + fingerCount + " finger(s) touching the screen");
}
}
Answer by DoTA_KAMIKADzE · May 02, 2015 at 11:27 PM
With touches you work this way. Firstly you get the amount of touches via touchCount then you loop through all touches and using GetTouch get their respective position, then check the position of Touch and compare it to Screen.Width.
If you'll want to project coordinates from screen to world and back and forth then use ScreenToWorldPoint and WorldToScreenPoint, there are more of those as well, that's just to introduce you to touch world.
Your answer
![](https://koobas.hobune.stream/wayback/20220613194619im_/https://answers.unity.com/themes/thub/images/avi.jpg)