Implementing Intuitive Mobile Movement in Unity

Damian Dąbrowski
3 min readJan 27, 2024

--

Creating responsive and intuitive movement controls for mobile games in Unity can be a bit different from traditional PC or console games due to the touch-based input. In this article, we’ll explore a simple approach to implement movement for mobile games using touch input.

Understanding Touch Input

Unity’s Input class provides access to touch information through the `Input.touches` array. Each touch is represented by a `Touch` struct, which contains data such as the position, phase, and finger ID.

Creating a Virtual Joystick

One common method for mobile movement is using a virtual joystick. This gives players a familiar control scheme that’s easy to use on touch screens.

Here’s a basic outline of how you might create a simple virtual joystick in Unity:

  1. Create UI elements for the joystick background and thumb.
  2. Detect when and where the player touches the screen.
  3. Move the joystick thumb to follow the player’s finger within a certain radius.
  4. Calculate the direction and magnitude of the input based on the thumb’s position relative to the center of the joystick background.
  5. Apply this input to move the player character.

Scripting Mobile Movement

Below is a simplified script that demonstrates how you might use touch input to move an object in Unity. This script assumes you have a method to determine the direction from your virtual joystick or touch input.

using UnityEngine;
public class MobileMovement : MonoBehaviour
{
public float speed = 5.0f;
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
touchPosition.z = 0; // Ensure we're not moving on the Z-axis
Vector3 direction = (touchPosition - transform.position).normalized;
transform.position += direction * speed * Time.deltaTime;
}
}
}

In this script, we’re checking for touches and using the first touch to determine the movement direction. We convert the touch position from screen space to world space, then calculate the direction by subtracting the object’s current position from the touch position. We normalize this vector to get a direction with a magnitude of 1, then apply it to the object’s position.

Optimizing for Mobile

When developing for mobile, it’s important to consider performance and user experience:

  • Use touch phases to differentiate between different types of touches (e.g., beginning, moving, ending).
  • Implement dead zones to prevent small, unintentional movements.
  • Consider using Unity’s built-in `Input.GetAxis(“Horizontal”)` and `Input.GetAxis(“Vertical”)` with a mobile input library that maps virtual joystick movement to these axes.
  • Test on multiple devices to ensure your controls feel good across different screen sizes and resolutions.

Mobile movement controls in Unity require careful consideration of touch input and user interface design. By using a virtual joystick or similar touch-based control scheme, you can create a mobile gaming experience that feels natural and intuitive. Remember to keep the controls simple and responsive, and always test on actual devices to ensure the best player experience. With these tips, you’re ready to bring smooth and engaging movement to your mobile games.

--

--

Damian Dąbrowski
Damian Dąbrowski

Written by Damian Dąbrowski

Hi, I’m Damian, an Electrical Power Engineer, who loves building AI powered apps.

No responses yet