- Home /
Touching left or right side of iPhone screen?
I basically just want to know how I would go about detecting if the user is touching either the left or right side of the screen? I'm assuming I would use Input.touches, but I'm not sure where to go from there. Thanks in advance for any help!
Answer by Josh 14 · Jul 15, 2011 at 03:10 AM
After a little bit of research into the Touch class I've answered my own question. A simple script to detect touching the left or right side of the screen would be:
if (Input.touchCount == 1)
{
var touch = Input.touches[0];
if (touch.position.x < Screen.width/2)
{
DoLeftSideStuff();
}
else if (touch.position.x > Screen.width/2)
{
DoRightSideStuff();
}
}
I'm going to give you a few suggestions to maybe make your code better:
The more important one, you would be wise to switch Input.touches[0] with Input.GetTouch(0). The latter does not allocate any temporary variables and on iOS that is an important feature because garbage collection is expensive and the fewer variables you can allocate the less often you will need to.
I would suggest using
if(Input.touchCount > 0 )
ins$$anonymous$$d of==1
because then it will work properly if the user puts two fingers down. You still only want the first touch, but you can still retrieve the first touch even if there are more.
Answer by Valer407 · Feb 05, 2019 at 12:39 AM
When you don't want have multiply clicking when finger touch screen here is code:
void Update () {
if (Input.touchCount > 0)
{
var touch = Input.GetTouch(0);
if (touch.position.x < Screen.width/2)
{
if(Input.GetTouch(0).phase == TouchPhase.Began)
{
Debug.Log("Left click");
}
}
else if (touch.position.x > Screen.width/2)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
Debug.Log("Right click");
}
}
}
}
Your answer
Follow this Question
Related Questions
Ray error with touches.position 1 Answer
Managing Multiple Touch Inputs 0 Answers
Exporting to Touch Screen Monitors or Touch Screen Displays 0 Answers