Unity new Input System : Note on UI Mobile Controls (Part IX)
In this lecture we see how to use new Input System in mobile devices.
What we talk about is On Screen Control
Here’s a reference
https://docs.unity3d.com/Packages/com.unity.inputsystem@0.9/manual/OnScreen.html
Let’s set up a scene for example.
We have a “plane” and a cube, the cube is the player and we want to move it with the joystick.
Super easy example.
Now, in order to create a joystick without have it, we need OnScreenGui. In few words, a UI image.
After create it, change source image to “knob”, so we have a circle
To change image just click here
And position it in the lower left corner
Now rename it to Joystick and add a new component :
On Screen Stick
Before moving on, the Movement Range determine how much we can pull the stick in any direction, and the Control Path can be map to in Assets that we have in our game.
In this case we choose joystick and stick.
If we try to use it in Play mode it seems ok, obviously nothing can be move yet but the stick seems fine.
But to use it correctly, we need to go i EventSystem, and click on Replace With InputSystemUI
Now that we have the right component, we need to implement it so can we move our Player!
And it works very similar to how we’ve used it so far :
I create an Input Actions, open it and create a Player, and a Movement.
Movement is not a button but it’s a Vector2 because in the documentation we know this is a Vector2
Now, watch out to this passage :
when we use a Vector2 we do this step and everything works fine
But in this case, we have to use another option.
Because the movement is determine by the Joystick, we pass in directly the stick thanks to “usages” option
If now we generate the C# Class and we create a Player Class, and attach it to the Player (Cube)
This is my Player Class
(just for testing, the cube can be moved only on X axis)
If we press Play
It works, but as you can see the cube is moved at the opposite direction.
If we press left the cube go right and vice versa.
The way to fix it is to go inside PlayerInputActions,
processor -> + -> invert Vector2
And now everything works perfectly fine