Published on

Handling flick input on your phone (Smartphone game with Unity)

Authors
  • Evelyn

Please share this article if you like!

Hello! I, Evelyn, am writing this article.

If you have visited this article, you are probably developing games for smartphones. If you are developing a game for smartphones, you may want to move the character according to the user's flick input.

If you want to develop a run game like "Temple run", you can use this code.

I want to judge flick input in the game I'm developing.

This article can fulfill the above needs. The video also explains in detail, if you want to watch it.

What you will gain from this article

It judges the flick input on a smartphone and allows you to perform any operation. In this case, we will move a Cube 3d object as an example.


Table of Contents

Write two scripts

First, let's create two C# files and launch Visual Studio.

The following code shows the entire flick detection process.

FlickInput.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FlickInput : MonoBehaviour
{
  [SerializeField] Vector2 MinMoveVal = new Vector2(50f, 50f);

  private Vector2 StartPos;
  private Vector2 EndPos;

  public enum FlickDirection
  {
    NONE,
    TAP,
    UP,
    DOWN,
    RIGHT,
    LEFT,
  }

  private FlickDirection NowFlick = FlickDirection.NONE;

  void Update()
  {
    JudgeInput();
  }

  public FlickDirection GetNowFlick()
  {
    return NowFlick;
  }

  private void ResetNowFlick()
  {
    NowFlick = FlickDirection.NONE;
  }

  private void HandleFlick()
  {
    Vector2 distance = new Vector2(
      (new Vector3(EndPos.x, 0, 0) - new Vector3(StartPos.x, 0, 0)).magnitude,
      (new Vector3(0, EndPos.y, 0) - new Vector3(0, StartPos.y, 0)).magnitude
    );

    if (distance.x <= MinMoveVal.x && distance.y <= MinMoveVal.y)
    {
      ResetNowFlick();
    }
    else if (distance.x > distance.y)
    {
      float x = Math.Sigh(EndPos.x - StartPos.x);
      if (x < 0) NowFlick = FlickDirection.LEFT;
      else if (x > 0) NowFlick = FlickDirection.RIGHT;
    }
    else
    {
      float y = Math.Sigh(EndPos.y - StartPos.y);
      if (y < 0) NowFlick = FlickDirection.DOWN;
      else if (y > 0) NowFlick = FlickDirection.UP;
    }
  }

  private void JudgeInput()
  {
    if (Application.isEditor)
    {
      if (Input.GetMouseButtonDown(0))
      {
        StartPose = Input.mousePosition;
      }
      else if (Input.GetMouseButtonUp(0))
      {
        EndPos = Input.mousePosition;
        HandleFlick();
      }
      else if (NowFlick != FlickDirection.NONE)
      {
        ResetNowFlick();
      }
    }
    else
    {
      if (Input.touchCount > 0)
      {
        Touch touch = Input.touches[0];
        if (touch.phase == TouchPhhase.Began)
        {
          StartPos = touch.position;
        }
        else if (touch.phase == TouchPhhase.Ended)
        {
          EndPos = touch.position;
          HandleFlick();
        }
      }
      else if (NowFlick != FlickDirection.NONE)
      {
        ResetNowFlick();
      }
    }
  }
}

The following code receives the flick input decision and moves the 3D object Cube.

MovingCube.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(FlickInput))]
public class MovingCube : MonoBehaviour
{
  private FlickInput FlickInput;
  void Start()
  {
    FlickInput = GetComponent<FlickInput>();
  }

  void Update()
  {
    if (FlickInput.GetNowFlick() == FlickInput.FlickDirection.NONE) return;

    switch(FlickInput.GetNowFlick())
    {
      case FlickInput.FlickDirection.UP:
        transform.Translate(0, 1, 0);
        break;
      case FlickInput.FlickDirection.DOWN:
        transform.Translate(0, -1, 0);
        break;
      case FlickInput.FlickDirection.RIGHT:
        transform.Translate(1, 0, 0);
        break;
      case FlickInput.FlickDirection.LEFT:
        transform.Translate(-1, 0, 0);
        break;
    }
  }
}

The script is now complete.

Let's create a script to judge flick input.

Attaching scripts

When you are done creating the script, create a 3D Cube object and attach it to the script.

Please refer to the image above to assign it.

I have applied this code to develop an endless run game.

I hope this information can be useful for your game development.

Summary of this article

Judge the flick input and get on with developing the game!

Please share this article if you like!