I'm using unity2D and C#, and I have a code for touch screen:
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
startTime = Time.time;
startPos = touch.position;
}
else if (touch.phase == TouchPhase.Ended)
{
endTime = Time.time;
endPos = touch.position;
swipeDist = (endPos - startPos).magnitude;
swipeTime = endTime - startTime;
if (swipeTime < maxTime && swipeDist > minSwipeDist)
{
swipe();
}
And this code for swipe:
void swipe()
{
Vector2 distance = endPos - startPos;
if (Mathf.Abs(distance.x) > Mathf.Abs(distance.y))
{
if (distance.x > 0)
{
Debug.Log("Right swipe");
}
if (distance.x < 0)
{
Debug.Log("Left swipe");
}
}
if (Mathf.Abs(distance.x) < Mathf.Abs(distance.y))
{
if (distance.y > 0)
{
Debug.Log("Up swipe");
}
if (distance.y < 0)
{
Debug.Log("Down swipe");
}
}
}
}
How can I add to this code hold function, for for example move object at the screen?