Up And Downward Swipe inside "Touch.Began"

I am able to get Left and Right swipe inside Touch.Began event. Please help me how can i get the Up and Down swipe inside Touch.Began Event.

float xStart = 0.0f;
float xEnd = 0.0f;
float yStart = 0.0f;
float yEnd = 0.0f;
bool sendCall = true;

void Start ()
{
	
}

void Update ()
{
	foreach (Touch touch in Input.touches) {
		
		if (touch.phase == TouchPhase.Began) {
			xStart = touch.position.x;
			yStart = touch.position.y;
		}
		if (touch.phase == TouchPhase.Moved) {
			xEnd = touch.position.x;
			yEnd = touch.position.y;
			
			if ((xStart < xEnd) && sendCall) {
				print ("Right Swipe");
				sendCall = false;
			}
			if ((xStart > xEnd) && sendCall) {
				print ("Left Swipe");
				sendCall = false;
			}
			
		}
		
		if (touch.phase == TouchPhase.Ended) {
			xStart = 0.0f;    // resetting start and end x position.
			xEnd = 0.0f;
			sendCall = true;	  //Reset to send call again after touch has been completed.
			couldBeSwipe = true;
		}
		
	}
}

Sorry, I'm using this script and I suggest this is C#? Well, Unity gives me an error. It says: Error CS0103: The name `couldBeSwipe' does not exist in the current context. Can you help me out please? Because I need this script. Thanks in advance

3 Answers

3

Something like that:

//C#
//SwipeHandler.cs
using UnityEngine;

public class SwipeHandler : MonoBehaviour
{
    public float minMovement = 20.0f;
    public bool sendUpMessage = true;
    public bool sendDownMessage = true;
    public bool sendLeftMessage = true;
    public bool sendRightMessage = true;
    public GameObject MessageTarget = null;

    private Vector2 StartPos;
    private int SwipeID = -1;
    
    void Update ()
    {
        if (MessageTarget == null)
            MessageTarget = gameObject;
        foreach (var T in Input.touches)
        {
            var P = T.position;
            if (T.phase == TouchPhase.Began && SwipeID == -1)
            {
                SwipeID = T.fingerId;
                StartPos = P;
            }
            else if (T.fingerId == SwipeID)
            {
                var delta = P - StartPos;
                if (T.phase == TouchPhase.Moved && delta.magnitude > minMovement)
                {
                    SwipeID = -1;
                    if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
                    {
                        if (sendRightMessage && delta.x > 0)
                            MessageTarget.SendMessage("OnSwipeRight", SendMessageOptions.DontRequireReceiver);
                        else if (sendLeftMessage && delta.x < 0)
                            MessageTarget.SendMessage("OnSwipeLeft", SendMessageOptions.DontRequireReceiver);
                    }
                    else
                    {
                        if (sendUpMessage && delta.y > 0)
                            MessageTarget.SendMessage("OnSwipeUp", SendMessageOptions.DontRequireReceiver);
                        else if (sendDownMessage && delta.y < 0)
                            MessageTarget.SendMessage("OnSwipeDown", SendMessageOptions.DontRequireReceiver);
                    }
                }
                else if (T.phase == TouchPhase.Canceled || T.phase == TouchPhase.Ended)
                {
                    SwipeID = -1;
                    MessageTarget.SendMessage("OnTap", SendMessageOptions.DontRequireReceiver);
                }
            }
        }
    }	
}

edit I modified the sample script so it can actually be used “as it is”. Just attach it to a gameobject. The script will send those 4 messages to other scripts:

  • OnSwipeRight
  • OnSwipeLeft
  • OnSwipeUp
  • OnSwipeDown

So in another script just declare a function like this:

void OnSwipeLeft()
{
    // Do something when user swiped left
}

You can assign any “target” for those messages by dragging it to the “MessageTarget” variable. If no other GameObject is given it will send the messages to the own gameobject so other scripts can receive them.

You can disable certain messages be setting the boolean values to false.

Thanks man that was super.

How is this script working? I attached this script to my character, but it doesn't work. maybe you can help me out with this?

What do you mean it doesn't work? It is just a "framework". As you can see there are 4 Debug.Logs in the code which show you what happened at this point. The Debug.Logs of course don't showup anywhere when you test this on a device. You can use this to do whatever you want when you "swipe" up / down / left / right.

Sorry my bad, This is not where I was looking for. I was searching for a Javascript or C# for my Character. If I swipe the screen to the left, the character needs to go to left with X Axis = -80 when I release the screen with my finger. and for right the same X Axis = 80. I can't find it and I really would like to give up. But that isn't an option. I hope you can help me out. :( Thanks in advance.

I know how to move an object. That's not the problem. but the problem is that I would move my character like Subway surfers. It goes to the left and right. It moves from one position to another position. By example. If character is on x=0 and you swipe the character to the left. the character moves to x=-80 and else if you swipe to right from x=0 it moves to x=80.

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...!!

Guys , why so complicated heres the answer

public float raz=0;

void Update () 
	{
		Touch[] touch = Input.touches;
		for (int i=0; i<Input.touchCount; i++) {
			raz = touch*.deltaPosition.x;*

_ if (touch*.phase == TouchPhase.Moved){_
_
if(raz > 0) //swipe left*_
if(raz < 0) //swipe right
* }*
* }*
* }*