# Handle two different touch gestures at same time without overlapping

I'm trying to create a Character touch controls for 2d platform.

In my script Im using one horizontal swipe and hold touch gesture and one vertical swipe gesture without hold.Both are different methods called in void Update ().

And here is my script :

void Update ()
{

HorizontalSwipe();
VerticalSwipe ();

}

void HorizontalSwipe()
{
foreach (Touch FingerTouchx in Input.touches)
{
if(FingerTouchx.fingerId<1)//         FINGERID

{
//if(FingerTouchx.position.x < Screen.width/2)

if(FingerTouchx.phase == TouchPhase.Began)
{
FingerInitialPositionx=FingerTouchx.position.x;

}

else if(FingerTouchx.phase == TouchPhase.Moved)
{
FingerMovedPositionx=FingerTouchx.position.x;

if(FingerMovedPositionx>FingerInitialPositionx)
{
charcter.transform.Translate(Vector2.right * speed * Time.deltaTime);
}

else if(FingerMovedPositionx<FingerInitialPositionx)
{
charcter.transform.Translate(-Vector2.right * speed * Time.deltaTime);
}

}

else if(FingerTouchx.phase == TouchPhase.Ended)
{
FingerInitialPositionx=0f;
FingerMovedPositionx=0f;

}

else
{

FingerMovedPositionx=FingerTouchx.position.x;

if(FingerMovedPositionx>FingerInitialPositionx)
{
charcter.transform.Translate(Vector2.right * speed * Time.deltaTime);
}

else if(FingerMovedPositionx<FingerInitialPositionx)
{
charcter.transform.Translate(-Vector2.right * speed * Time.deltaTime);

}

}

}

}
}

void VerticalSwipe()
{
foreach (Touch FingerTouchy in Input.touches)
{

if(FingerTouchy.fingerId<1)      //         FINGERID

{
//if(FingerTouchy.position.y > Screen.width/2)

if(FingerTouchy.phase == TouchPhase.Began)
{
FingerInitialPositiony=FingerTouchy.position.y;

}
if(FingerTouchy.phase == TouchPhase.Moved)
{
FingerMovedPositiony=FingerTouchy.position.y;

}
if(FingerTouchy.phase == TouchPhase.Ended)
{
if(FingerMovedPositiony>FingerInitialPositiony)
{
charcter.transform.Translate(Vector2.up * speed * Time.deltaTime);
}
if(FingerMovedPositiony<FingerInitialPositiony)
{
charcter.transform.Translate(-Vector2.up * speed * Time.deltaTime);
}
FingerInitialPositiony=0f;
FingerMovedPositiony=0f;

}

else
{}

}
}
}


Problem:

1:When i swipe and hold horizontally the character moves fine but when i take my finger back it triggers the vertical swipe method.

2:When i try to swipe only vertically the horizontal swipe is also triggered.

3: The two swipe gestures are overlapping and triggered at same time on a Single touch.

Solution(Guess):Dividing the touch screen equally half and forcing horizontal touch gestures strictly on left side of touch screen and vertical touch gestures on right side.

But I'm beginner(You can see my coding style) and would like to know how to do it programatically. Im still learning.

• @Steven answers for that question didn't help much – user75648 Dec 18 '15 at 7:20
• How did you get that vertical method is executing? Which block of code in Vertical method is executing? What is happening? – Hamza Hasan Dec 18 '15 at 7:28
• @HamzaHasan don't know, you are free to test this script if you want. – user75648 Dec 18 '15 at 7:32
• Mark break points on each check of vertical code block then you will get know – Hamza Hasan Dec 18 '15 at 7:35

You actually want to split screen into two to get gesture Horizontal and Vertical. So use Screen.width for this purpose. You are checking position.y in vertical, which is not correct. You have check X in either case. Here is modified code with 2 touches.

void Update ()
{
HorizontalSwipe();
VerticalSwipe ();
}

void HorizontalSwipe()
{
foreach (Touch FingerTouchx in Input.touches)
{
if(FingerTouchx.fingerId<2)//         FINGERID

{
if(FingerTouchx.position.x > Screen.width/2)
continue;

if(FingerTouchx.phase == TouchPhase.Began)
{
FingerInitialPositionx=FingerTouchx.position.x;
}

else if(FingerTouchx.phase == TouchPhase.Moved)
{
FingerMovedPositionx=FingerTouchx.position.x;

if(FingerMovedPositionx>FingerInitialPositionx)
{
charcter.transform.Translate(Vector2.right * speed * Time.deltaTime);
}

else if(FingerMovedPositionx<FingerInitialPositionx)
{
charcter.transform.Translate(-Vector2.right * speed * Time.deltaTime);
}
}

else if(FingerTouchx.phase == TouchPhase.Ended)
{
FingerInitialPositionx=0f;
FingerMovedPositionx=0f;
}

else
{
FingerMovedPositionx=FingerTouchx.position.x;

if(FingerMovedPositionx>FingerInitialPositionx)
{
charcter.transform.Translate(Vector2.right * speed * Time.deltaTime);
}

else if(FingerMovedPositionx<FingerInitialPositionx)
{
charcter.transform.Translate(-Vector2.right * speed * Time.deltaTime);

}
}
}
}
}

void VerticalSwipe()
{
foreach (Touch FingerTouchy in Input.touches)
{
if(FingerTouchy.fingerId<2)      //         FINGERID

{
if(FingerTouchy.position.x < Screen.width/2)
continue;

if(FingerTouchy.phase == TouchPhase.Began)
{
FingerInitialPositiony=FingerTouchy.position.y;
}

if(FingerTouchy.phase == TouchPhase.Moved)
{
FingerMovedPositiony=FingerTouchy.position.y;
}

if(FingerTouchy.phase == TouchPhase.Ended)
{
if(FingerMovedPositiony>FingerInitialPositiony)
{
charcter.transform.Translate(Vector2.up * speed * Time.deltaTime);
}
if(FingerMovedPositiony<FingerInitialPositiony)
{
charcter.transform.Translate(-Vector2.up * speed * Time.deltaTime);
}
FingerInitialPositiony=0f;
FingerMovedPositiony=0f;
}

else
{}
}
}
}

• Tested your code.When i swipe up the horizontal swipe is trigged, vertical swipe is completely absent or ignored. – user75648 Dec 18 '15 at 7:57
• Yea, I did that. What else you want then? – Hamza Hasan Dec 18 '15 at 7:58
• the vertical and horizontal swipe must be handled at same time or at different time but should not overlap like in this youtube.com/watch?v=djVPd2pO3Co – user75648 Dec 18 '15 at 8:00
• Split screen in two? one for horizontal and other for vertical? – Hamza Hasan Dec 18 '15 at 8:02
• i used if(FingerTouchx.position.x < Screen.width/2) in void HorizontalSwipe() works fine for first time and from second time horizontal swipe is ignored and whole screen accepts vertical swipe only – user75648 Dec 18 '15 at 8:09