Unity use right stick to aim in 2D with new input system
Objective: Use the right stick of a controller to allow the player to aim a weapon in 2D, while moving with the left.
If you have followed my previous articles about 2D movement with the new input system or you’re familiar with it then just carry on. If not I do recommend reading them.
I am in a different project to my previous articles but the left movement is very similar. There is just 1 small change, instead of using the up, down, left and right of the joystick as with the button presses, I am using just the leftstick of a gamepad.
In the code there is no difference to how it is implemented. You still read the same value. So, the next thing to do was set up the action for the right stick and it is set up in the exact same way as the left stick.
I will say that I will be using a small trick to allow for the aiming. Instead of getting the mouse position which is what happens with mouse and keyboard movement. I will be creating an object that will have a sprite renderer component but the sprite will be turned off.
Doing it this way, I can just move the object around as I am with the player, by using transform.Translate().
The bottom line above is the most important line.
- I have set up a _move variable that is global so that I can use it in the update method.
- I am subscribing to the performed event with +=
- the => is not equal to or more than it is a quick way to run code in one line
- the => separates the input parameters in this case obj from the lambda body on the right
- This means that I am storing the value of the obj inside of the _move variable
Below you can see the long way of doing the same thing. I subscribe to the event, create the method I want to call and then assign the value inside the method. I do agree that the below way is a lot easier to read and understand but I wanted to show two ways to do the same thing.
Moving on, I then use the updated _move variable inside of the update function.
I am first creating a magnitude that I am using for my movement, you do not need this but I like to add this.
I am then moving the _aimCursor gameobject using the _move variable, along with speed, magnitude and Time. That is it for aiming with the right stick.
One last thing you can see above, is the rotation of my turret, which is what is firing. I rotate it to wards where the invisible gameobject is in the game world.
The last thing that I do for this to work, is to limit the functionality of the mouse. If a gamepad is connected then the player will need to use that, if not then they can use the mouse and keyboard.
In the OnEnable method I check for a gamepad.
To get the Gamepad namespace you will need to add the following using statement, which you should already have if you want the input system to work correctly.
And that is it for aiming with the right stick in 2D. Get moving those fake objects people.
If you found this helpful, don’t forget to clap and follow for more articles on game development in Unity.