detect Swipe in four directions (android)

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

Hello Guys i want to detect tap also with swipes i.e if i tap player jumps and if i swipe down player slides.??

7 Answers

7

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…

Why wont this code work for you and looking for additional answers? Although this code will only work if only one finger is moving on the screen because delta position will get zero if more than one finger moving on the screen

@ozturkcompany are you sure that deltaPosition will be zero? Usually I just use the input from the first touch and it doesn´t become zero.

@ozturkcompany sağ ol

Does it apply only on the camera as well also?

I think your question answer is "yes" but I not tested it in other objects yet

This works to me: http://pfonseca.com/swipe-detection-on-unity/

works amazing

This link appears to be malware, I would avoid clicking on it

Seems like the domain is sold, the site displays weird Chinese fap games. I had just stopped being so paranoid about the links I click but now I'm back on it.

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;
			}
		}
	}
}

thanks @Nomibuilder , this question is solved

Hu guys, I wrote this 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.

Hello. Can you reload script [here][1] [1]: http://gamedevelopertips.com/how-detect-swipe-direction-unity/ Because its not working for downloading (((

I found this post RTV Studios: Detecting swipe from Unity3D c# scripts

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

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

(Easy Swipe & Touch Controller)

Use the SwipeManager from the Unity forum:

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

Be sure and remember: Quaternion angles != euler angles. It would be better to record the Quaternion.eulerAngles into a Vector3 and then set the values to zero: //Convert rotation into readable angles. Vector3 eulerRotation = transform.rotation.eulerAngles; //Set non essential angles to 0. eulerRotation.x = 0; eulerRotation.z = 0; //Reset the transform rotation. transform.rotation = Quaternion.Euler(eulerRotation);