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);
}
}
}
@PauloFilho852
Copy link

PauloFilho852 commented Oct 4, 2019

lines 44 and 52: did you mean t.position.y/(float)Screen.height instead t.position.y/(float)Screen.width?

@Fonserbc
Copy link
Author

Fonserbc commented Oct 4, 2019

lines 44 and 52: did you mean t.position.y/(float)Screen.height instead t.position.y/(float)Screen.width?

No, that is intentional, note that MIN_SWIPE_DISTANCE is a factor of the screen width in this code, so I factor (normalize) both position.y and positon.x relative to the Screen.width, to keep the pixel ratio the same for each component (x and y).
This way both startPos, endPos and swipe keep a real-world equivalence.

Basically, I make this so that I can ensure that if swipe.x and swipe.y are the equal, that means you moved your finger the same amount on x and y in the real world as well.

@PauloFilho852
Copy link

lines 44 and 52: did you mean t.position.y/(float)Screen.height instead t.position.y/(float)Screen.width?

No, that is intentional, note that MIN_SWIPE_DISTANCE is a factor of the screen width in this code, so I factor (normalize) both position.y and positon.x relative to the Screen.width, to keep the pixel ratio the same for each component (x and y).
This way both startPos, endPos and swipe keep a real-world equivalence.

Basically, I make this so that I can ensure that if swipe.x and swipe.y are the equal, that means you moved your finger the same amount on x and y in the real world as well.

Ok. I understood you. Thank you for make me clear.

@vzapertistuqdio
Copy link

Thank you.Very helpfull

@STRTSNM
Copy link

STRTSNM commented Nov 21, 2020

Thanks

@SomrakPhotisar
Copy link

SomrakPhotisar commented Dec 13, 2020

Thank

@SedemQuame
Copy link

This is awesome, can't wait to test.

@nullmoongames
Copy link

Thank you very much for sharing, very helpful.

@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