Question by
Pauciloquent · Oct 03, 2016 at 06:01 AM ·
c#unity 5platformer
Hold touch at the screen to move object
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");
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How To Generate Platforms Down Each other in a pattern with some different positions Unity 1 Answer
Having a couple of problems regarding 2D Movement. 1 Answer
Return key triggering a button OnClick? 1 Answer
Objects are created in the same location. 0 Answers
Suggest best hosting server for multiplayer Game??? 0 Answers