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;
}
}
}
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.
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;
}
}
}
}
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
* }*
* }*
* }*
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
– androids