Skip to content

Instantly share code, notes, and snippets.

@Fonserbc
Last active April 27, 2023 04:28
Show Gist options
  • Save Fonserbc/ca6bf80b69914740b12da41c14023574 to your computer and use it in GitHub Desktop.
Save Fonserbc/ca6bf80b69914740b12da41c14023574 to your computer and use it in GitHub Desktop.
A simple swipe detector for touchscreens for Unity3D. Four cardinal directions.
using UnityEngine;
/*
* Swipe Input script for Unity by @fonserbc, free to use wherever
*
* Attack to a gameObject, check the static booleans to check if a swipe has been detected this frame
* Eg: if (SwipeInput.swipedRight) ...
*
*
*/
public class SwipeInput : MonoBehaviour {
// If the touch is longer than MAX_SWIPE_TIME, we dont consider it a swipe
public const float MAX_SWIPE_TIME = 0.5f;
// Factor of the screen width that we consider a swipe
// 0.17 works well for portrait mode 16:9 phone
public const float MIN_SWIPE_DISTANCE = 0.17f;
public static bool swipedRight = false;
public static bool swipedLeft = false;
public static bool swipedUp = false;
public static bool swipedDown = false;
public bool debugWithArrowKeys = true;
Vector2 startPos;
float startTime;
public void Update()
{
swipedRight = false;
swipedLeft = false;
swipedUp = false;
swipedDown = false;
if(Input.touches.Length > 0)
{
Touch t = Input.GetTouch(0);
if(t.phase == TouchPhase.Began)
{
startPos = new Vector2(t.position.x/(float)Screen.width, t.position.y/(float)Screen.width);
startTime = Time.time;
}
if(t.phase == TouchPhase.Ended)
{
if (Time.time - startTime > MAX_SWIPE_TIME) // press too long
return;
Vector2 endPos = new Vector2(t.position.x/(float)Screen.width, t.position.y/(float)Screen.width);
Vector2 swipe = new Vector2(endPos.x - startPos.x, endPos.y - startPos.y);
if (swipe.magnitude < MIN_SWIPE_DISTANCE) // Too short swipe
return;
if (Mathf.Abs (swipe.x) > Mathf.Abs (swipe.y)) { // Horizontal swipe
if (swipe.x > 0) {
swipedRight = true;
}
else {
swipedLeft = true;
}
}
else { // Vertical swipe
if (swipe.y > 0) {
swipedUp = true;
}
else {
swipedDown = true;
}
}
}
}
if (debugWithArrowKeys) {
swipedDown = swipedDown || Input.GetKeyDown (KeyCode.DownArrow);
swipedUp = swipedUp|| Input.GetKeyDown (KeyCode.UpArrow);
swipedRight = swipedRight || Input.GetKeyDown (KeyCode.RightArrow);
swipedLeft = swipedLeft || Input.GetKeyDown (KeyCode.LeftArrow);
}
}
}
@ToP09
Copy link

ToP09 commented Mar 12, 2022

How to make it that it makes an input while it's swiping? so I can make a movement with Time.deltaTime
if (SwipeInput.swipedRight == true) { player.transform.Translate(Vector3.right * Time.deltaTime * playerSpeed); Debug.Log("Swipe Input Right" + playerPosition); }

@PippoApps
Copy link

Works very well cheers for that.
I took the liberty of adding a delegate:

`using UnityEngine;

/*

  • Swipe Input script for Unity by @Fonserbc, free to use wherever
  • Attack to a gameObject, check the static booleans to check if a swipe has been detected this frame
  • Eg: if (SwipeInput.swipedRight) ...

*/

public class SwipeInput : MonoBehaviour {

// If the touch is longer than MAX_SWIPE_TIME, we dont consider it a swipe
public const float MAX_SWIPE_TIME = 0.5f; 

// Factor of the screen width that we consider a swipe
// 0.17 works well for portrait mode 16:9 phone
public const float MIN_SWIPE_DISTANCE = 0.17f;

public static bool swipedRight = false;
public static bool swipedLeft = false;
public static bool swipedUp = false;
public static bool swipedDown = false;
static bool swiped;


public bool debugWithArrowKeys = true;


public delegate void OnSwipe();
public static event OnSwipe swipeListeners;



Vector2 startPos;
float startTime;

public void Update()
{
	swiped = false;
	swipedRight = false;
	swipedLeft = false;
	swipedUp = false;
	swipedDown = false;

	if(Input.touches.Length > 0)
	{
		Touch t = Input.GetTouch(0);
		if(t.phase == TouchPhase.Began)
		{
			startPos = new Vector2(t.position.x/(float)Screen.width, t.position.y/(float)Screen.width);
			startTime = Time.time;
		}
		if(t.phase == TouchPhase.Ended)
		{
			if (Time.time - startTime > MAX_SWIPE_TIME) // press too long
				return;

			Vector2 endPos = new Vector2(t.position.x/(float)Screen.width, t.position.y/(float)Screen.width);

			Vector2 swipe = new Vector2(endPos.x - startPos.x, endPos.y - startPos.y);

			if (swipe.magnitude < MIN_SWIPE_DISTANCE) // Too short swipe
				return;

			if (Mathf.Abs (swipe.x) > Mathf.Abs (swipe.y)) { // Horizontal swipe
				if (swipe.x > 0) {
					swiped = swipedRight = true;
				}
				else {
					swiped = swipedLeft = true;
				}
			}
			else { // Vertical swipe
				if (swipe.y > 0) {
					swiped = swipedUp = true;
				}
				else {
					swiped = swipedDown = true;
				}
			}
		}
	}

	if (debugWithArrowKeys) {
		swipedDown = swipedDown || Input.GetKeyDown (KeyCode.DownArrow);
		swipedUp = swipedUp|| Input.GetKeyDown (KeyCode.UpArrow);
		swipedRight = swipedRight || Input.GetKeyDown (KeyCode.RightArrow);
		swipedLeft = swipedLeft || Input.GetKeyDown (KeyCode.LeftArrow);
		swiped = swipedDown || swipedLeft || swipedRight || swipedUp;
	}

	if (swiped)
    {
		if (swipeListeners != null)
		{
			swipeListeners();
		}

	}
}

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment