Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class SwipeFinger : MonoBehaviour {
- public enum Directions {Up, Down, Left, Right, None}
- [HideInInspector]
- public Directions direction = Directions.None;
- //The min swipe distance to change direction
- public float sensibility = 2.5f;
- //Touch latched finger
- private int latchedFinger = -1;
- void Update () {
- Vector2 deltaPosition = Vector2.zero;
- #if UNITY_EDITOR
- //Get delta position based on mouse move on editor
- if (Input.GetMouseButton (0)) {
- deltaPosition.x = Input.GetAxis ("Mouse X");
- deltaPosition.y = Input.GetAxis ("Mouse Y");
- }
- #elif UNITY_STANDALONE_WIN
- //Same here, but for standalone application
- if (Input.GetMouseButton (0)) {
- deltaPosition.x = Input.GetAxis ("Mouse X");
- deltaPosition.y = Input.GetAxis ("Mouse Y");
- }
- #else
- //And here gets delta position with touch
- foreach (Touch touch in Input.touches) {
- if (latchedFinger == -1 && touch.phase == TouchPhase.Began)
- latchedFinger = touch.fingerId;
- if (latchedFinger == touch.fingerId &&
- (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled))
- latchedFinger = -1;
- if (touch.fingerId == latchedFinger && touch.phase == TouchPhase.Moved) {
- deltaPosition = touch.deltaPosition;
- }
- }
- #endif
- //Send delta position to a method to check which way finger was swiped
- SetDirection (deltaPosition);
- }
- void SetDirection (Vector2 deltaPosition) {
- //Check direction based on delta position
- if (deltaPosition.y > sensibility)
- direction = Directions.Up;
- else if (deltaPosition.y < -sensibility)
- direction = Directions.Down;
- else if (deltaPosition.x > sensibility)
- direction = Directions.Right;
- else if (deltaPosition.x < -sensibility)
- direction = Directions.Left;
- else
- direction = Directions.None;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement