Swipe Input Controller

Detects touch swipes in four directions with configurable thresholds and dead zones. Fires UnityEvents for each swipe direction with speed and distance tracking.

File
SwipeInputController.cs
Unity
2022.3+
Level
beginner
Size
3.4 KB · 122 lines
Added
2026-03-23
License
MIT
View source

No APIs that Unity 6.0 to 6.6 made obsolete or removed. How this is checked

What it adds to your GameObject

A MonoBehaviour component: attach it to a GameObject. This is the Inspector you get, read from the source: 9 serialized fields with their defaults.

Inspector
Script SwipeInputController
Swipe Settings
Min Swipe Distance 50
Max Swipe Time 0.5
Dead Zone 20
Events
On Swipe Up ()
List is Empty
On Swipe Down ()
List is Empty
On Swipe Left ()
List is Empty
On Swipe Right ()
List is Empty
On Swipe Start (Vector2)
List is Empty
On Swipe End (Vector2, float, float)
List is Empty

When to use it

Built for mobile games that use swipe gestures — card games, puzzle games, endless runners with lane switching. Essential for any touch-based game that needs directional input beyond simple taps.

Setup

  1. Create an empty GameObject named SwipeManager
  2. Attach the SwipeInputController script to it
  3. Set the minimum swipe distance threshold in the inspector
  4. Wire up the onSwipeUp/Down/Left/Right UnityEvents to your game logic
  5. Test on a mobile device or use mouse input in the editor

Features

  • 4-directional swipe detection
  • Configurable swipe threshold
  • Swipe speed and distance tracking
  • Events for swipe start/end/direction
  • Dead zone filtering

Inspector fields

Every field Swipe Input Controller exposes in the Inspector, with the default it starts at.

FieldTypeDefaultGroup
Min Swipe DistanceminSwipeDistancefloat50Swipe Settings
Max Swipe TimemaxSwipeTimefloat0.5Swipe Settings
Dead ZonedeadZonefloat20Swipe Settings
On Swipe UponSwipeUpUnityEventNo listenersEvents
On Swipe DownonSwipeDownUnityEventNo listenersEvents
On Swipe LeftonSwipeLeftUnityEventNo listenersEvents
On Swipe RightonSwipeRightUnityEventNo listenersEvents
On Swipe StartonSwipeStartUnityEvent<Vector2>No listenersEvents
On Swipe EndonSwipeEndUnityEvent<Vector2, float, float>No listenersEvents

Public API

What other scripts can call on SwipeInputController.

MemberReturns
ResetSwipe()void
LastSwipeDirection propertySwipeDirection
LastSwipeSpeed propertyfloat
LastSwipeDistance propertyfloat
IsSwiping propertybool

Requirements

  • Reads the legacy Input Manager (UnityEngine.Input). If Project Settings > Player > Active Input Handling is set to Input System Package only, Unity throws an InvalidOperationException; set it to Both.

Common mistakes

Test on actual devices, not just the Unity Remote — touch behavior differs significantly. The minSwipeDistance threshold needs tuning per device DPI; what feels natural on a phone may be too sensitive on a tablet. Don't forget to handle multi-touch if your game uses simultaneous gestures.

Source code

SwipeInputController.cs, 122 lines, MIT licensed. Paste it into a new C# file of the same name.

SwipeInputController.cs
C#
using UnityEngine;
using UnityEngine.Events;

public class SwipeInputController : MonoBehaviour
{
    [Header("Swipe Settings")]
    [SerializeField] private float minSwipeDistance = 50f;
    [SerializeField] private float maxSwipeTime = 0.5f;
    [SerializeField] private float deadZone = 20f;

    [Header("Events")]
    public UnityEvent onSwipeUp;
    public UnityEvent onSwipeDown;
    public UnityEvent onSwipeLeft;
    public UnityEvent onSwipeRight;
    public UnityEvent<Vector2> onSwipeStart;
    public UnityEvent<Vector2, float, float> onSwipeEnd; // direction, speed, distance

    public enum SwipeDirection { None, Up, Down, Left, Right }

    public SwipeDirection LastSwipeDirection { get; private set; }
    public float LastSwipeSpeed { get; private set; }
    public float LastSwipeDistance { get; private set; }
    public bool IsSwiping { get; private set; }

    private Vector2 touchStartPos;
    private float touchStartTime;
    private bool tracking;

    private void Update()
    {
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            HandleTouch(touch.phase, touch.position);
        }
        else
        {
            // Mouse fallback for editor testing
            if (Input.GetMouseButtonDown(0))
                HandleTouch(TouchPhase.Began, Input.mousePosition);
            else if (Input.GetMouseButton(0))
                HandleTouch(TouchPhase.Moved, Input.mousePosition);
            else if (Input.GetMouseButtonUp(0))
                HandleTouch(TouchPhase.Ended, Input.mousePosition);
        }
    }

    private void HandleTouch(TouchPhase phase, Vector2 position)
    {
        switch (phase)
        {
            case TouchPhase.Began:
                touchStartPos = position;
                touchStartTime = Time.time;
                tracking = true;
                IsSwiping = true;
                onSwipeStart?.Invoke(position);
                break;

            case TouchPhase.Ended:
            case TouchPhase.Canceled:
                if (!tracking) return;
                tracking = false;
                IsSwiping = false;
                ProcessSwipe(position);
                break;
        }
    }

    private void ProcessSwipe(Vector2 endPos)
    {
        float elapsed = Time.time - touchStartTime;
        if (elapsed > maxSwipeTime) return;

        Vector2 delta = endPos - touchStartPos;
        float distance = delta.magnitude;

        if (distance < minSwipeDistance) return;

        float speed = distance / Mathf.Max(elapsed, 0.001f);
        LastSwipeDistance = distance;
        LastSwipeSpeed = speed;

        SwipeDirection direction = GetSwipeDirection(delta);
        LastSwipeDirection = direction;

        onSwipeEnd?.Invoke(delta.normalized, speed, distance);

        switch (direction)
        {
            case SwipeDirection.Up: onSwipeUp?.Invoke(); break;
            case SwipeDirection.Down: onSwipeDown?.Invoke(); break;
            case SwipeDirection.Left: onSwipeLeft?.Invoke(); break;
            case SwipeDirection.Right: onSwipeRight?.Invoke(); break;
        }
    }

    private SwipeDirection GetSwipeDirection(Vector2 delta)
    {
        if (Mathf.Abs(delta.x) < deadZone && Mathf.Abs(delta.y) < deadZone)
            return SwipeDirection.None;

        if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
        {
            return delta.x > 0 ? SwipeDirection.Right : SwipeDirection.Left;
        }
        else
        {
            return delta.y > 0 ? SwipeDirection.Up : SwipeDirection.Down;
        }
    }

    public void ResetSwipe()
    {
        LastSwipeDirection = SwipeDirection.None;
        LastSwipeSpeed = 0f;
        LastSwipeDistance = 0f;
        tracking = false;
        IsSwiping = false;
    }
}

Next step

Tap Input HandlerHandles single tap, double tap, and long press gestures with configurable timing. Provides tap position in both screen and world space coordinates.