Advice for Swipe controll

Hello Unity community, I’d like request some advice developing a mobile touch control that i started. I started up by making this code and made the gui text areas so i could test this on my phone and see the returned values. What i was initially was thinking that if the value returned by deltaposition is greater, I’d have the character jump and if X is greater, let’s just say he would do something else. When you slide down though, Y returns a negative value so it’s smaller than X. I want to be able to swipe in the positive y and negative and do different things and of course the same for the X. Can i get any advice on achieving this?

Thanks in advance!!


public float ChangeInY;
public float ChangeInX;

void Start ()
{

}

// Update is called once per frame
void Update ()
{
if(Input.touches.Length > 0 Input.touches[0].phase == TouchPhase.Moved)
{
ChangeInX = Input.touches[0].deltaPosition.x * Time.deltaTime;
ChangeInY = Input.touches[0].deltaPosition.y * Time.deltaTime;
}

}

void OnGUI()
{
GUI.TextArea (new Rect((Screen.width/2),(Screen.height/2), 350,350), "the value of X is " + ChangeInX);
GUI.TextArea (new Rect((Screen.width/4),(Screen.height/4), 350,350), "the value of Y is " + ChangeInY);

}


I made something similar to this before except it involved angles. How well does it work? Do you have a specific problem?

Hi there!

I’m not really sure if I’ve understood your question correctly. Do you want to know which direction has received the largest input? The approach I usually take when I want a single direction from a swipe is to record the first and the last touch locations.

But if you want to use your approach, try using the absolutes of your values.

public float ChangeInY;
public float ChangeInX;

void Update ()
{
    if(Input.touches.Length > 0  Input.touches[0].phase == TouchPhase.Moved)
    {
        ChangeInX = Input.touches[0].deltaPosition.x * Time.deltaTime;
        ChangeInY = Input.touches[0].deltaPosition.y * Time.deltaTime;
        
        if(Mathf.Abs(ChangeInX) > Mathf.Abs(ChangeInY))
            DoX(ChangeInX);
        else
            DoY(ChangeInY);
    }
}

private void DoX(float xValue)
{
    // X had the biggest change, do things with the x value
}

private void DoY(float yValue)
{
    // Y had the biggest change, do things with the y value
}

The absolute value of a number is simply a positive version of that number. So for example Mathf.Abs(-5) returns 5 and Mathf.Abs(3) returns 3.

what I actually wanted was to be able to swipe going in the positive Y or negative Y or the X. would you mind showing me a quick example of the approach you usually take?

Well, you just save the start position and compare it to the end position.

private Vector3 mStart;

void Update()
{
    if(Input.GetMouseButtonDown(0))
        mStart = Input.mousePosition;
    if(Input.GetMouseButtonUp(0))
        DoThings(Input.mousePosition - mStart);
}

void DoThings(Vector3 move)
{
    // ...
}

The vector indicates the difference between the place the player first put down their finger and the position the player was at when lifting his finger. If I recall correctly the mouse related Input uses the first touch on smartphones. The function for comparing the vector and doing different things would be something like this:

void DoThings(Vector3 move)
{
    if(Mathf.Abs(move.x) > Mathf.Abs(move.y))
    {
        if(move.x > 0)
            // Positive movement in X was the largest input
        else
            // Negative movement in X was the largest input
    }
    else
    {
        if(move.y > 0)
            // Positive movement in Y was the largest input
        else
            // Negative movement in Y was the largest input
    }
}

So this is what i Came up with. I also layed down a GUI text area to test it and it doesn’t work. I tried just touching the screen and sometimes, my text area randomly displayed a value. Any Ideas?
using UnityEngine;
using System.Collections;

public class SwipingInput : MonoBehaviour
{
float Starttime;
Vector2 StartPosition;
float Box_Swipe_Must_Be_within = 60f;
public static bool A_Proper_Swipe;
float MaximumSwipeTime = 2f;
public static string MyStringyVar;

void Start ()
{

}

void Update ()
{
if(Input.touchCount > 0)
{
Touch thetouched = Input.touches[0];

switch(thetouched.phase)
{
case TouchPhase.Began:
{
StartPosition = thetouched.position;
Starttime = Time.time;
A_Proper_Swipe = true;
break;
}

case TouchPhase.Moved:
{
float Time_Youve_Been_Swiping = Time.time - Starttime;
if(Mathf.Abs (thetouched.position.x - StartPosition.x) > Box_Swipe_Must_Be_within)
{
A_Proper_Swipe = false;
}
else if(Mathf.Abs (thetouched.position.y - StartPosition.y) > Box_Swipe_Must_Be_within)
{
A_Proper_Swipe = false;
}
else if(Time_Youve_Been_Swiping > MaximumSwipeTime)
{
A_Proper_Swipe = false;

}
break;
}
case TouchPhase.Ended:
{
if(A_Proper_Swipe)
{
float TheStoredfinalChangeInX = Mathf.Abs (thetouched.position.x - StartPosition.x);
float TheStoredfinalChangeInY = Mathf.Abs (thetouched.position.y - StartPosition.y);

if(TheStoredfinalChangeInX > TheStoredfinalChangeInY)
{
float TheSignOftheX = Mathf.Sign (thetouched.position.x - StartPosition.x);
if(TheSignOftheX > 0)
{
MyStringyVar = “you went Right”;
}
else
{
MyStringyVar = “you went left”;
}
}
else
{
float TheSignOftheY = Mathf.Sign (thetouched.position.y - StartPosition.y);
if(TheSignOftheY > 0)
{
MyStringyVar = “you went Up”;
}
else
{
MyStringyVar = “you went Down”;
}
}

}
break;
}

}

}

}

void OnGUI()
{
GUI.TextArea (new Rect((Screen.width/2),(Screen.height/2),200,200), "value = " + MyStringyVar);
}
}

okay, never mind; I knew I planned this really well and It shouldn’t have been problematic. I removed my Moved Case which was a case determining what i called " a proper swipe, With that in mind, i guess I’ll revise it. The code works without it. Thank You.