Here is a screen swipe script for you

Here is a simple swipe script for you. You just need to adjust two values in your inspector ‘Give’ and ‘Velocity’:

using UnityEngine;
using System.Collections;

public class SimpleSwipe : MonoBehaviour {

    //Set give and velocity check in
    public int Velocity;    //Set this to 10 or so to test
    public int Give;         //Set this to 5 or so to test
    public enum Swiping { None, Up, Right, Down, Left }
    public Swiping swiping = Swiping.None;

    private enum State { Reset, RecordMousePosition, CheckForMovement, CompareValues }
    private State state;
    private int Timer = 0;
    private float FirstX = 0;
    private float FirstY = 0;
    private float SecondX = 0;
    private float SecondY = 0;
    private float MovedX = 0;
    private float MovedY = 0;
    private int CooldownTimer = 0;

    void Update() {
        switch (state) {

        case State.Reset:
            if (CooldownTimer > 0) {
                MovedX = 0;
                MovedY = 0;
                CooldownTimer -= 1;
            }
            if (CooldownTimer == 0) {
                swiping = Swiping.None;
            }
            if (Input.GetMouseButton(0)) {
                MovedX = 0;
                MovedY = 0;
                state = State.RecordMousePosition;
            }
            break;


        case State.RecordMousePosition:
            if (Input.GetMouseButton (0)) {
                Timer = 0;
                FirstX = Input.mousePosition.x;
                FirstY = Input.mousePosition.y;
                state = State.CheckForMovement;
            } else {
                state = State.Reset;
            }
        break;

        case State.CheckForMovement:
            if (Input.GetMouseButton (0)) {
                Timer += 1;

                //Is there any movement from one frame to the next?
                //First check:
                if (Timer > 1) {
                    SecondX = Input.mousePosition.x;
                    SecondY = Input.mousePosition.y;
                    //If there is any movement from one frame to the next...
                    if ((FirstX != SecondX) || (FirstY != SecondY)) {
                        Timer += 1;
                    } else {
                        Timer -= 1;
                    }
                }
                //Second, check on more velocity.
                if (Timer > Give) {
                    SecondX = Input.mousePosition.x;
                    SecondY = Input.mousePosition.y;
                    state = State.CompareValues;
                }
            } else {
                state = State.Reset;
            }
        break;

        case State.CompareValues:
            if (Input.GetMouseButton(0)) {
                if (SecondY - FirstY > Velocity) {
                    swiping = Swiping.Up;
                    CooldownTimer = 3;
                    state = State.Reset;
                } else if (SecondY - FirstY < (Velocity*-1)) {
                    swiping = Swiping.Down;
                    CooldownTimer = 3;
                    state = State.Reset;
                } else if (SecondX - FirstX > Velocity) {
                    swiping = Swiping.Right;
                    CooldownTimer = 3;
                    state = State.Reset;
                } else if (SecondX - FirstX < (Velocity*-1)) {
                    swiping = Swiping.Left;
                    CooldownTimer = 3;
                    state = State.Reset;
                }
            } else {
                state = State.Reset;
            }
        break;

        } //switch end

    } //Update() end

} //class end

[/code]

1 Like

Updated the topic with the working C# script.

Thank you, saved me some time. Works perfect.

Thank you very much for this script. Great approach