Virtual Joystick Pack
User Guide


Last updated: 24 November 2024

Foreword

If you encounter any problems in the package, have anything you would like to clarify, or would like to report a bug, please contact us at terresquall.com/contact.

Need help with setting up your Virtual Joystick, or want to report a bug? Please make a post on our dedicated forum for this asset pack.

Table of Contents

  1. Version Changelogs
  2. Setting up
    1. Adding a Joystick
    2. Unity Input system settings
    3. Assigning unique IDs
  3. Reading joystick input
    1. Using GetAxis()
    2. Using GetAxisRaw()
    3. Using GetAxis() or GetAxisRaw() without arguments
    4. Reading multiple joysticks
    5. Using GetAxisDelta()
  4. Settings
  5. FAQs
  6. Credits

1. Version Changelogs

Version 1.1.0 (24 November 2024)

Version 1.0.8 (8 October 2024)

Version 1.0.7 (27 September 2024)

Version 1.0.6 (27 September 2024)

Version 1.0.5 (11 July 2024)

Version 1.0.4 (16 June 2024)

Version 1.0.3 (2 June 2024)

Version 1.0.2 (3 April 2024)

Version 1.0.1 (22 March 2024)

Version 1.0.0 (3 December 2023)


2. Setting up

Import the asset into your project. The asset should be unpacked into a folder called VirtualJoystick in your Assets folder.

a. Adding a Joystick

To add and use a virtual joystick, drag any of the joystick prefabs from VirtualJoystick/Prefabs onto any Canvas GameObject in your Scene, and it should be ready to use.

Available joystick designs
Available joystick designs to choose from.

b. Unity Input system settings

Since v1.1.0, the Virtual Joystick works with both the old Input Manager system and the new Input System. It will also automatically default to using the old Input system, unless you explicitly set the Active Input Handling setting under Player Settings > Player to use the new Input System Package.

When both input handling systems are enabled, Virtual Joystick defaults to the old Input Manager system to maximise compatibility with Unity Remote. Hence, if you are using Unity Remote to test your projects, be sure to set the Active Input Handling to Both.

How to enable both the old and new Input systems.
The Active Input Handling field is in Edit > Project Settings, under Player > Other Settings.

If you are using an older version of the Virtual Joystick (before v1.1.0), you will receive an error message on your Virtual Joystick if the old Input System is not enabled.

How to enable the old Input system
Follow the instructions above to rectify this.

c. Assigning unique IDs

As of Version 1.0.6, every joystick comes with an ID attribute. This attribute will need to be unique for every joystick in the Scene, as it is the ID that you will have to use to read the joystick's input.

Virtual Joystick ID
Every joystick has an ID attribute.

If your ID is not unique, a warning will be shown on your joystick. Click on the button below the warning, and a unique ID will automatically be assigned to your Virtual Joystick.

Virtual Joystick ID
Every ID must be unique.

If the ID attribute is not on your joystick, please update your asset to the latest version, as versions before 1.0.6 are unable to reliably retrieve input from the same joystick across different devices.


3. Reading joystick input

Once the joystick is set up, in every script where you want to read input from any of your virtual joysticks, you will need to add the following namespace to the top of your scripts:

using Terresquall;

Once that is done, you will be able to access the VirtualJoystick class.

a. Using GetAxis()

To read input from the joystick, use VirtualJoystick.GetAxis("Horizontal") to read horizontal offset, and VirtualJoystick.GetAxis("Vertical") to read vertical offset. For example, the following code moves the character in the horizontal direction the joystick is pushed:

// The value of x is between -1 and 1.
float x = VirtualJoystick.GetAxis("Horizontal");
transform.position += x * Time.deltaTime;

The function works similarly to Unity's own Input.GetAxis() method. Do note, however, that the "Horizontal" and "Vertical" prompts are hardcoded into the joystick and are unrelated to the values in Unity's Input Manager.

From Version 1.0.3 onwards, you can also call GetAxis() on an instance of a joystick.

b. Using GetAxisRaw()

If you want to snap the values to -1, 0 or 1, you can also use VirtualJoystick.GetAxisRaw("Horizontal") or VirtualJoystick.GetAxisRaw("Vertical"), which functions like Unity's own Input.GetAxisRaw().

c. Using GetAxis() or GetAxisRaw() without arguments

If you don't like to retrieve each axis separately, you can also call VirtualJoystick.GetAxis() without any arguments to retrieve a Vector2 containing the horizontal and vertical inputs.

Vector2 joyInput = VirtualJoystick.GetAxis();
			
// Moves the character with the joystick.
transform.position += joyInput.x * Time.deltaTime;

From Version 1.0.3 onwards, you can also call GetAxisRaw() on an instance of a joystick.

d. Reading multiple joysticks

If you have multiple virtual joysticks on the Scene, you will need to make sure every Joystick has a unique ID assigned to them.

Virtual Joystick ID
Make sure ID is unique.

Then, to access input from the joystick, use the assigned ID in your GetAxis() or GetAxisRaw() calls to access input from that specific joystick.

// Reads the horizontal input of the joystick with an ID of 2.
VirtualJoystick.GetAxis("Horizontal", 2);
// Reads the vertical input of the joystick with an ID of 3.
VirtualJoystick.GetAxis("Vertical", 3);

If you want to retrieve input data for both axes, just pass the integer value of the joystick, like so:

// Gets the input data of the joystick with an ID of 3.
VirtualJoystick.GetAxis(3);

If the ID attribute is missing, please update the asset pack and ensure that it is above Version 1.0.7. Versions before 1.0.7 are unable to reliably retrieve input from the same joystick across different devices.

You are also recommended to avoid using the GetAxis() and GetAxisRaw() methods without specifying an ID when you have multiple joysticks, to avoid having potential input issues.

VirtualJoystick.GetAxis("Horizontal"); // Avoid doing this.

e. Using GetAxisDelta()

If you want to find the change in the axis of the joystick from the last frame to the current frame, you can use the following code:

Vector2 delta = VirtualJoystick.GetAxisDelta();

If you have multiple joysticks on the screen, you can also pass an integer to the function to get your nth joystick. The first joystick has an index of 0, so if you want the delta of the 2nd joystick:

Vector2 delta = VirtualJoystick.GetAxisDelta(1);

You can also call the function on an instance of your joystick:

VirtualJoystick vj = FindObjectOfType<VirtualJoystick>();
Vector2 delta = vj.GetAxisDelta();

Do note that, since the magnitude of GetAxisDelta() is affected by frame rate, if you want use it to judge how fast the joystick is moving, you should be dividing the resulting vector by Time.deltaTime first.

Vector2 delta = VirtualJoystick.GetAxisDelta() / Time.deltaTime;

4. Settings

On top of this, each virtual joystick also comes with a Virtual Joystick component, which has a variety of settings you can toggle.

Virtual Joystick component
The Virtual Joystick component.

To adjust how the Virtual Joystick works, you will want to adjust the attributes under the Settings section. Below are a list of the properties, and what they do:

PropertyDescription
Only On Mobile Check this box if you want to hide the Virtual Joystick when the game is not being played on a mobile device. Only works on Unity 2020 and above. Works with the Device Simulator in Unity.
Drag Color What the color of the joystick turns into when you are tapping on it. Used to provide feedback when using the joystick.
Sensitivity This controls how responsive the joystick is.
Radius This controls how far you can pull the control stick on the joystick away from the joystick base at the centre. When adjusting this, a red circle will be shown on the joystick, showing you how big this radius is.
Deadzone A value between 0 and 1, representing a percentage of the maximum distance the joystick can travel. For example, if this value is 0.3, you will need to pull the joystick at least 30% away from the centre for the input to register.
Edge Snap When active, the joystick automatically snaps to the edge when it is outside of the Deadzone.
Directions If more than 0, the joystick will only be able to move in specfic directions. For example, if set to a value of 4, the joystick can only move in . Set it to 8, and it can only move in .
Angle Offset Only available when Directions is more than 0. Allows you to set the angle that the Directions are aligned to, so that with 4 directions, the joystick can be configured to move in instead of the default directions.
Snaps To Touch This works together with the Boundaries attribute. When checked, the joystick will teleport to wherever your finger is, as long as they are within the boundaries set.
Boundaries Only available when Snaps To Touch is activated. If a finger is tapped within the bounds denoted (in the Editor, this is a yellow box around the joystick), Snaps To Touch will occur.
Size Adjustments For adjusting the size of the joystick, use the buttons here to make your life easier, as there is a child element inside the joystick that you have to scale up as well.

5. FAQs

Before you read the rest of this FAQ, make sure you update your Virtual Joystick package on your project first.


My project can't export after I import this asset. What do I do?

Please update your Virtual Joystick package. If you are using Version 1.0.1 or below, there is a bug that prevents you from being able to export your project.


When I use GetAxisRaw(), when I move towards the left or right directions, the joystick still detects upward movement!

If this happens, please update your Virtual Joystick package and make sure you are using Version 1.0.5 and above.

Also, make sure that your Deadzone is set above 0.


The Joystick is not showing up when I drag it onto a Scene! What do I do?

If it is not showing up, that's because it wasn't parented to a Canvas. If you put it onto the Scene without parenting it to a Canvas, it won't work! You will also get the following warning on your Joystick component:

Joystick is not parented to a Canvas.
A friendly warning from the Joystick component.

To fix this, make sure you always parent your Virtual Joystick GameObjects under your Canvas.


Now the Joystick is showing up, but it is not responding when I tap or drag it!

If your Joystick is not responding to your interaction, check to see if there are any warning or error messages on the component in the Inspector. If you see the following error message:

How to enable the old Input system
Follow the instructions above to rectify this.

That means you have Unity's new Input system installed on your project, and you have configured it to disable the original Input system. To get the joystick working again, you will need to either:

  1. Update your Virtual Joystick Pack to the latest version (any version above v1.1.0), or;
  2. Turn on the old Input system as well, by heading to Edit > Project Settings. Under the Window that pops up, go to the Player tab, open the Other Settings subwindow, and find the Active Input Handling dropdown. Then, set its value to Both
How to enable both the old and new Input systems.
How to enable both the old and new Input systems.

The Joystick is still not responding to my input.

After trying all the tips above, if your joystick is still unresponsive, try resetting your Virtual Joystick component.

How to reset the Virtual Joystick.
How to reset the Virtual Joystick.

You might have gotten some of the configurations wrong (e.g. if you set Sensitivity to 0, the joystick will also stop responding to player input).

Alternatively, you can also visit our dedicated forums for this asset pack and ask us for more help.


6. Credits

The following people made this asset pack possible: