-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerInput
68 lines (64 loc) · 2.6 KB
/
PlayerInput
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerInput : MonoBehaviour
{
private Vector3 fp; //First touch position
private Vector3 lp; //Last touch position
private float dragDistance; //minimum distance for a swipe to be registered
void Start()
{
dragDistance = Screen.height * 15 / 100; //dragDistance is 15% height of the screen
}
void Update()
{
if (Input.touchCount == 1) // user is touching the screen with a single touch
{
Touch touch = Input.GetTouch(0); // get the touch
if (touch.phase == TouchPhase.Began) //check for the first touch
{
fp = touch.position;
lp = touch.position;
}
else if (touch.phase == TouchPhase.Moved) // update the last position based on where they moved
{
lp = touch.position;
}
else if (touch.phase == TouchPhase.Ended) //check if the finger is removed from the screen
{
lp = touch.position; //last touch position. Ommitted if you use list
//Check if drag distance is greater than 20% of the screen height
if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
{//It's a drag
//check if the drag is vertical or horizontal
if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
{ //If the horizontal movement is greater than the vertical movement...
if ((lp.x > fp.x)) //If the movement was to the right)
{ //Right swipe
Debug.Log("Right Swipe");
}
else
{ //Left swipe
Debug.Log("Left Swipe");
}
}
else
{ //the vertical movement is greater than the horizontal movement
if (lp.y > fp.y) //If the movement was up
{ //Up swipe
Debug.Log("Up Swipe");
}
else
{ //Down swipe
Debug.Log("Down Swipe");
}
}
}
else
{ //It's a tap as the drag distance is less than 20% of the screen height
Debug.Log("Tap");
}
}
}
}
}