Skip to content

Instantly share code, notes, and snippets.

@RaheelYawar
Created October 15, 2017 19:49
Show Gist options
  • Save RaheelYawar/eb47202bdbeb6a1436b941a211eb2489 to your computer and use it in GitHub Desktop.
Save RaheelYawar/eb47202bdbeb6a1436b941a211eb2489 to your computer and use it in GitHub Desktop.
Swipe Detection in Unity
using UnityEngine;
using System.Collections;
public enum SwipeDirection
{
Null = 0,//no swipe detected
Duck = 1,//swipe down detected
Jump = 2,//swipe up detected
Right = 3,//swipe right detected
Left = 4//swipe left detected
}
public class SwipeControls : MonoBehaviour {
//Constants
private const float fSensitivity = 15;
//VARIABLES
//distance calculation
private SwipeDirection sSwipeDirection; //string to receive swipe output (Debug Only)
private float fInitialX;
private float fInitialY;
private float fFinalX;
private float fFinalY;
private int iTouchStateFlag; //flag to check
private int ButtonStateFlag;
private float inputX; //x-coordinate
private float inputY; //y-coordinate
private float slope; //slope (m) of the the
private float fDistance; //magnitude of distance between two positions
void Start ()
{
fInitialX = 0.0f;
fInitialY = 0.0f;
fFinalX = 0.0f;
fFinalY = 0.0f;
inputX = 0.0f;
inputY = 0.0f;
iTouchStateFlag = 0;
ButtonStateFlag = 0;
sSwipeDirection = SwipeDirection.Null;
}
private KeyCode keyCode;
void Update()
{
if (iTouchStateFlag == 0 && Input.GetMouseButtonDown(0)) //state 1 of swipe control
{
fInitialX = Input.mousePosition.x; //get the initial x mouse/ finger value
fInitialY = Input.mousePosition.y; //get the initial y mouse/ finger value
sSwipeDirection = SwipeDirection.Null;
iTouchStateFlag = 1;
}
if (iTouchStateFlag == 1) //state 2 of swipe control
{
fFinalX = Input.mousePosition.x;
fFinalY = Input.mousePosition.y;
sSwipeDirection = swipeDirection(); //get the swipe direction
if (sSwipeDirection != SwipeDirection.Null)
iTouchStateFlag = 2;
}//end of state 1
if (iTouchStateFlag == 2 || Input.GetMouseButtonUp(0)) //state 3 of swipe control
{
//sSwipeDirection = SwipeDirection.Null;
iTouchStateFlag = 0;
}//end of M.R. swipe control
if (Application.isEditor)
{
if (ButtonStateFlag == 0)//get key down
{
if (Input.GetKeyDown(KeyCode.W))
{
keyCode = KeyCode.W;
ButtonStateFlag = 1;
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
keyCode = KeyCode.UpArrow;
ButtonStateFlag = 1;
}
else if (Input.GetKeyDown(KeyCode.A))
{
keyCode = KeyCode.A;
ButtonStateFlag = 1;
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
keyCode = KeyCode.LeftArrow;
ButtonStateFlag = 1;
}
else if (Input.GetKeyDown(KeyCode.D))
{
keyCode = KeyCode.D;
ButtonStateFlag = 1;
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
keyCode = KeyCode.RightArrow;
ButtonStateFlag = 1;
}
else if (Input.GetKeyDown(KeyCode.S))
{
keyCode = KeyCode.S;
ButtonStateFlag = 1;
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
keyCode = KeyCode.DownArrow;
ButtonStateFlag = 1;
}
}
else if (ButtonStateFlag == 1)//wait for user to release key
{
if (keyCode == KeyCode.W || keyCode == KeyCode.UpArrow)
sSwipeDirection = SwipeDirection.Jump;
else if (keyCode == KeyCode.S || keyCode == KeyCode.DownArrow)
sSwipeDirection = SwipeDirection.Duck;
else if (keyCode == KeyCode.A || keyCode == KeyCode.LeftArrow)
sSwipeDirection = SwipeDirection.Left;
else if (keyCode == KeyCode.D || keyCode == KeyCode.RightArrow)
sSwipeDirection = SwipeDirection.Right;
ButtonStateFlag = 2;
}
else if (ButtonStateFlag == 2)//register the key press as an event
{
if (Input.GetKeyUp(keyCode))
ButtonStateFlag = 0;
}
}//end of if application is editor
}//end of Update function
/*
* FUNCTION: Calculate the swipe direction
*/
private SwipeDirection swipeDirection()
{
//calculate the slope of the swipe
inputX = fFinalX - fInitialX;
inputY = fFinalY - fInitialY;
slope = inputY / inputX;
//calculate the distance of tap start and end
fDistance = Mathf.Sqrt( Mathf.Pow((fFinalY-fInitialY), 2) + Mathf.Pow((fFinalX-fInitialX), 2) );
if (fDistance <= (Screen.width/fSensitivity))//higher the dividing factor higher the sensitivity
return SwipeDirection.Null;
if (inputX >= 0 && inputY > 0 && slope > 1)//first octant JUMP
{
return SwipeDirection.Jump;
}
else if (inputX <= 0 && inputY > 0 && slope < -1)//eighth octant JUMP
{
return SwipeDirection.Jump;
}
else if (inputX > 0 && inputY >= 0 && slope < 1 && slope >= 0)//second octant RIGHT
{
return SwipeDirection.Right;
}
else if (inputX > 0 && inputY <= 0 && slope > -1 && slope <= 0)//third octant RIGHT
{
return SwipeDirection.Right;
}
else if (inputX < 0 && inputY >= 0 && slope > -1 && slope <= 0)//seventh octant LEFT
{
return SwipeDirection.Left;
}
else if (inputX < 0 && inputY <= 0 && slope >= 0 && slope < 1)//sixth octant LEFT
{
return SwipeDirection.Left;
}
else if (inputX >= 0 && inputY < 0 && slope < -1)//fourth octant DUCK
{
return SwipeDirection.Duck;
}
else if (inputX <= 0 && inputY < 0 && slope > 1)//fifth octant DUCK
{
return SwipeDirection.Duck;
}//end of else if
return SwipeDirection.Null;
}//end of SwipeDirection function
/*
* FUNCTION: Return swipe direction.
* RETURNS: Returns NULL if no swipes are detected.
* Returns SwipeDirection if a swipe is detected
*/
public SwipeDirection getSwipeDirection()
{
if (sSwipeDirection != SwipeDirection.Null)
{
SwipeDirection etempSwipeDirection = sSwipeDirection;
sSwipeDirection = SwipeDirection.Null;
return etempSwipeDirection;
}
else
return SwipeDirection.Null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment