Mobile games have become increasingly popular in recent years, and with good reason. People want to be able to play games on the go, and mobile devices make it easy to do so. However, when it comes to mobile games, the user interface (UI) is incredibly important. A good UI can make or break a game, especially on mobile devices. In this tutorial, we’ll be going over how to make a mobile joystick UI in the Unity engine.
Step 1: Create a Canvas
The Canvas is where all the UI elements will go. To create a new Canvas, go to GameObject > UI > Canvas. This will create a new Canvas object in the Hierarchy window.

Step 2: Create a Panel
A Panel is a UI element that can be used to group other UI elements together. To create a new Panel, go to GameObject > UI > Panel. This will create a new Panel object in the Hierarchy window. Rename it to “JoystickPanel”. Disable the Image component to remove the semi-transparent overlay.
Step 3: Create the joystick background
We’ll start by creating the background for the joystick. To do this, we’ll create a new Image UI element. Right-click on the JoystickPanel object in the Hierarchy window, then go to UI > Image. This will create a new Image object as a child of the JoystickPanel. Rename it to “JoystickBackground”.
Step 4: Set the joystick background image
Now that we have the JoystickBackground object, we need to set its image. To do this, click on the JoystickBackground object in the Hierarchy window, then go to the Inspector window. Click on the “Source Image” dropdown and choose the image you want to use for the joystick background. You can also adjust the “Color” property to change the color of the background if you want.
Step 5: Set the joystick background size
Next, we need to set the size of the joystick background. Click on the JoystickBackground object in the Hierarchy window, then go to the Inspector window. Under the “RectTransform” component, set the “Width” and “Height” values to the size you want. In this tutorial, we’ll set both values to 150.
Step 6: Create the joystick handle
Now we need to create the joystick handle. Right-click on the JoystickPanel object in the Hierarchy window, then go to UI > Image. This will create a new Image object as a child of the JoystickPanel. Rename it to “JoystickHandle”.
Step 7: Set the joystick handle image
Click on the JoystickHandle object in the Hierarchy window, then go to the Inspector window. Click on the “Source Image” dropdown and choose the image you want to use for the joystick handle. You can also adjust the “Color” property to change the color of the handle if you want.
Step 8: Set the joystick handle size and position
Now we need to set the size and position of the joystick handle. Click on the JoystickHandle object in the Hierarchy window, then go to the Inspector window. Under the “RectTransform” component, set the “Width” and “Height” values to the size you want. In this tutorial, we’ll set both values to 100. Next, set the “X” and “Y” values to 0, which will center the handle within the background.
Step 9: Create the joystick script
To make the joystick work, we need
to create a script that will control its behavior. Right-click in the Project window, then go to Create > C# Script. Name it “JoystickController”. Double-click the script to open it in your preferred code editor.
Step 10: Add variables
In the JoystickController script, add the following variables:
public RectTransform background;
public RectTransform handle;
public float handleRange = 50f;
public float deadZone = 0.1f;
private Vector3 inputVector = Vector3.zero;
background
will be used to reference the RectTransform component of the joystick background.handle
will be used to reference the RectTransform component of the joystick handle.handleRange
is the maximum distance the handle can move from the center of the joystick background.deadZone
is the minimum distance the handle must be from the center of the joystick background for input to register.inputVector
is the vector that will be used to control the character or object in the game.
Step 11: Add the OnDrag() method
The OnDrag() method is called when the user drags the joystick handle. Add the following code to the JoystickController script:
public void OnDrag(PointerEventData eventData)
{
Vector2 position;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(background, eventData.position, eventData.pressEventCamera, out position))
{
position.x = (position.x / background.sizeDelta.x);
position.y = (position.y / background.sizeDelta.y);
float x = (background.sizeDelta.x / 2f) * handleRange * position.x;
float y = (background.sizeDelta.y / 2f) * handleRange * position.y;
inputVector = new Vector3(x, y, 0);
inputVector = (inputVector.magnitude > handleRange) ? inputVector.normalized * handleRange : inputVector;
handle.anchoredPosition = new Vector3(inputVector.x, inputVector.y, 0);
}
}
This code uses the PointerEventData
to get the position of the user’s touch or mouse click, then converts it to a local position within the joystick background. It then calculates the input vector based on the position of the touch or click relative to the center of the joystick background. If the magnitude of the input vector is greater than the handleRange
, it normalizes the vector and multiplies it by handleRange
. Finally, it sets the position of the joystick handle to the input vector.
Step 12: Add the OnPointerUp() and OnPointerDown() methods
The OnPointerUp() and OnPointerDown() methods are called when the user lifts their finger or mouse button off the joystick handle, or when they first touch the handle. Add the following code to the JoystickController script:
public void OnPointerDown(PointerEventData eventData)
{
OnDrag(eventData);
}
public void OnPointerUp(PointerEventData eventData)
{
inputVector = Vector3.zero;
handle.anchoredPosition = Vector3.zero;
}
The OnPointerDown() method simply calls the OnDrag() method to update the position of the joystick handle. The OnPointerUp() method resets the input vector to zero and sets the position of the joystick handle back to the center of the joystick background.
Step 13: Add the Update() method
The Update() method is called every frame, so we’ll use it to update the position of the character or object in the game based on the input vector. Add the following code to the JoystickController script:
private void Update()
{
if (inputVector.magnitude > deadZone)
{
// Update position of character or object in the game
// based on inputVector
}
Here, we check if the magnitude of the input vector is greater than the deadZone
. If it is, we update the position of the character or object in the game based on the input vector.
Step 14: Use the JoystickController script
To use the JoystickController script, attach it to the joystick handle game object. Select the joystick handle game object in the Hierarchy window, then drag and drop the JoystickController script onto the inspector panel.
Step 15: Use the input vector in your game
To use the input vector to control the character or object in your game, you’ll need to reference it in your game’s script. In the script that controls the character or object, add the following code:
public JoystickController joystick;
private Vector3 moveVector;
private void Update()
{
moveVector = joystick.InputVector;
// Use moveVector to control the character or object in the game
}
Here, we reference the JoystickController script and create a moveVector
variable to store the input vector for use in a separate class, like a character controller. In the Update() method, we set moveVector
to equal joystick.inputVector
, which gets the input vector from the joystick. We can then use moveVector
to control the character or object in the game.
And that’s it! You’ve created a mobile joystick UI in the Unity engine. With this joystick, players can control characters or objects in your game using touch or mouse input.

Here’s the complete JoystickController script with all of the code we’ve covered:
using UnityEngine;
using UnityEngine.EventSystems;
public class JoystickController : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
public RectTransform background;
public RectTransform handle;
public float handleRange = 50f;
public float deadZone = 0.1f;
private Vector3 inputVector = Vector3.zero;
public Vector3 InputVector => inputVector;
public void OnDrag(PointerEventData eventData)
{
Vector2 position;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(background, eventData.position, eventData.pressEventCamera, out position))
{
position.x = (position.x / background.sizeDelta.x);
position.y = (position.y / background.sizeDelta.y);
float x = (background.sizeDelta.x / 2f) * handleRange * position.x;
float y = (background.sizeDelta.y / 2f) * handleRange * position.y;
inputVector = new Vector3(x, y, 0);
inputVector = (inputVector.magnitude > handleRange) ? inputVector.normalized * handleRange : inputVector;
handle.anchoredPosition = new Vector3(inputVector.x, inputVector.y, 0);
}
}
public void OnPointerDown(PointerEventData eventData)
{
OnDrag(eventData);
}
public void OnPointerUp(PointerEventData eventData)
{
inputVector = Vector3.zero;
handle.anchoredPosition = Vector3.zero;
}
/*
* You can also build your character movement right into the controller.
* Uncomment this block and add your movement code using the supplied
* inputVector. The Y axis represents vertical movement, and the X axis
* represents horizontal movement.
private void Update()
{
if (inputVector.magnitude > deadZone)
{
// Update position of character or object in the game based on inputVector
}
}
*/
}
Make sure to attach this script to the JoystickPanel game object and set the necessary fields to use it in your game.
You must be logged in to post a comment.