# detect Swipe in four directions (android)

**URL:** https://discussions.unity.com/t/detect-swipe-in-four-directions-android/90292
**Category:** Questions & Answers
**Created:** [December 19, 2013, 6:17pm UTC](https://discussions.unity.com/t/detect-swipe-in-four-directions-android/90292 "2013-12-19T18:17:15Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![alone1992](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@alone1992](https://discussions.unity.com/u/alone1992)
#### Post date: [December 19, 2013, 6:17pm UTC](https://discussions.unity.com/t/detect-swipe-in-four-directions-android/90292/1 "2013-12-19T18:17:15Z")

</div>

Hello  
I need to java script code that can detect four directions (up, down , right , left) swipe on screen and debug once when that is happen.  
I write some codes with delta position but never couldn’t have result.  
thanks

---

<div class="post-metadata">

### Author: ![MrVerdoux](https://avatars.discourse-cdn.com/v4/letter/m/eb9ed0/32.png) [@MrVerdoux](https://discussions.unity.com/u/MrVerdoux)
#### Post date: [December 20, 2013, 10:30am UTC](https://discussions.unity.com/t/detect-swipe-in-four-directions-android/90292/2 "2013-12-20T10:30:56Z")

</div>

Why don´t you check

```
float deltaX = deltaPosition.x;
float deltaY = deltaPosition.y;
float biggerDelta = (deltaX > deltaY) ? deltaX : deltaY;
int direction = Mathf.Sign (biggerDelta);

```

With that info you should be able to get it. At least that´s how I do it.

Edit: new script, same idea, it might need some tweak but I don´t see why it shoudn´t work.

```
using System.Collections;

public class MiniGestureRecognizer : MonoBehaviour {

	public enum SwipeDirection{
		Up,
		Down,
		Right,
		Left
	}

	public static event Action<SwipeDirection> Swipe;
	private bool swiping = false;
	private bool eventSent = false;
	private Vector2 lastPosition;
	
	void Update () {
		if (Input.touchCount == 0) 
			return;

		if (Input.GetTouch(0).deltaPosition.sqrMagnitude != 0){
			if (swiping == false){
				swiping = true;
				lastPosition = Input.GetTouch(0).position;
				return;
			}
			else{
				if (!eventSent) {
					if (Swipe != null) {
						Vector2 direction = Input.GetTouch(0).position - lastPosition;

						if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y)){
							if (direction.x > 0) 
								Swipe(SwipeDirection.Right);
							else
								Swipe(SwipeDirection.Left);
						}
						else{
							if (direction.y > 0)
								Swipe(SwipeDirection.Up);
							else
								Swipe(SwipeDirection.Down);
						}

						eventSent = true;
					}
				}
			}
		}
		else{
			swiping = false;
			eventSent = false;
		}
	}
}

```

I am sorry about the amount of ifs, I did this quite fast…

---

<div class="post-metadata">

### Author: ![pfonseca](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@pfonseca](https://discussions.unity.com/u/pfonseca)
#### Post date: [August 13, 2014, 1:07am UTC](https://discussions.unity.com/t/detect-swipe-in-four-directions-android/90292/3 "2014-08-13T01:07:42Z")

</div>

This works to me: [http://pfonseca.com/swipe-detection-on-unity/](http://pfonseca.com/swipe-detection-on-unity/)

---

<div class="post-metadata">

### Author: ![Nomibuilder](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/nomibuilder/32/155775_2.png) [@Nomibuilder](https://discussions.unity.com/u/Nomibuilder)
#### Post date: [January 24, 2015, 1:53pm UTC](https://discussions.unity.com/t/detect-swipe-in-four-directions-android/90292/4 "2015-01-24T13:53:05Z")

</div>

using UnityEngine;  
using System.Collections;

```
public class SwipeDetector : MonoBehaviour 
{
	
	public float minSwipeDistY;

	public float minSwipeDistX;
		
	private Vector2 startPos;

	void Update()
	{
//#if UNITY_ANDROID
		if (Input.touchCount > 0) 
			
		{
			
			Touch touch = Input.touches[0];
			
			
			
			switch (touch.phase) 
				
			{
				
			case TouchPhase.Began:

				startPos = touch.position;
				
				break;
				
				
				
			case TouchPhase.Ended:

					float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;

					if (swipeDistVertical > minSwipeDistY) 
						
					{
						
						float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
						
						if (swipeValue > 0)//up swipe

							//Jump ();
						
						else if (swipeValue < 0)//down swipe

							//Shrink ();
						
					}
					
					float swipeDistHorizontal = (new Vector3(touch.position.x,0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
					
					if (swipeDistHorizontal > minSwipeDistX) 
						
					{
						
						float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
						
						if (swipeValue > 0)//right swipe
							
							//MoveRight ();
						
						else if (swipeValue < 0)//left swipe
							
							//MoveLeft ();
						
					}
				break;
			}
		}
	}
}

```

---

<div class="post-metadata">

### Author: ![Max\_power1965](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@Max\_power1965](https://discussions.unity.com/u/Max_power1965)
#### Post date: [May 10, 2017, 12:29pm UTC](https://discussions.unity.com/t/detect-swipe-in-four-directions-android/90292/7 "2017-05-10T12:29:47Z")

</div>

Hu guys, I wrote [this](http://gamedevelopertips.com/how-detect-swipe-direction-unity/) simple tutorial wich is basically 7 lines of code and works like a charm. Easy and simple. You just have to use te build in Unity event system instead to write long and useless code.  
No need to use update or fixed update.

---

<div class="post-metadata">

### Author: ![tvranjith](https://avatars.discourse-cdn.com/v4/letter/t/9de053/32.png) [@tvranjith](https://discussions.unity.com/u/tvranjith)
#### Post date: [February 10, 2015, 12:57pm UTC](https://discussions.unity.com/t/detect-swipe-in-four-directions-android/90292/5 "2015-02-10T12:57:58Z")

</div>

I found this post [RTV Studios: Detecting swipe from Unity3D c# scripts](http://rtvstudios.blogspot.com/2015/02/detecting-swipe-from-unity3d-c-scripts.html)

It detects swipe in four direction and we can also control the swipe speed and angle for the swipe in four direction.

---

<div class="post-metadata">

### Author: ![MOSTY](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/mosty/32/254142_2.png) [@MOSTY](https://discussions.unity.com/u/MOSTY)
#### Post date: [October 12, 2016, 6:03am UTC](https://discussions.unity.com/t/detect-swipe-in-four-directions-android/90292/6 "2016-10-12T06:03:54Z")

</div>

Very cheap solution for beginners and experts who don’t want to spend much time in coding swipe & touch controls.  
NOW with Playmaker support.

> **[Unity Asset Store - The Best Assets for Game Making](https://assetstore.unity.com/)**
>
> Discover the best assets for game making. Choose from our massive catalog of 2D, 3D models, SDKs, templates, and tools to speed up your game development process.

(Easy Swipe & Touch Controller)

---

<div class="post-metadata">

### Author: ![Overnaut](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/overnaut/32/328362_2.png) [@Overnaut](https://discussions.unity.com/u/Overnaut)
#### Post date: [March 26, 2018, 1:55pm UTC](https://discussions.unity.com/t/detect-swipe-in-four-directions-android/90292/8 "2018-03-26T13:55:28Z")

</div>

Use the SwipeManager from the Unity forum:

[https://forum.unity.com/threads/swipe-in-all-directions-touch-and-mouse.165416/page-2#post-2741253](https://forum.unity.com/threads/swipe-in-all-directions-touch-and-mouse.165416/page-2#post-2741253)
