Drone Controls w/ New Unity Input System
Let’s take a look at how we can build drone controls within this little test. To start, we will want to create input commands for the movement of the drone. With creating drone functionality, we need to consider the different ways a drone moves. As it isn’t as simple as just WASD to move around, we have to consider the roll, yaw and pitch of the drone’s movements. As for what these movements are, the yaw is the spin that occurs within the air. The pitch is the tilt up and when the drone moves forwards and back. The roll is with it tilts on the sides:
So, for this, we will create both some vector2 along with a couple vector 1 commands:
Now that we have this all set up, let’s take a look at how we can create the horizontal movements for the drone. To start, we can take a look at how it is built with the old code, and take that information and build our new code out of it:
As we can see with the old code, we have a lot of input.getkey commands written with just minor changes in the values on each part. We also originally have it written as 2 separate voids, but with a bit of work on it, we can shrink the amount of code that we need to use and get the same results:
What I have done here is replaced the need to state every key input, and it is now dependent on which key we press. As the value of the key’s are set to 1 or -1, we just need to multiply it by the value we want it adjusted by and we will get our drone to move in the desired direction. As for the yaw of our drone, we can add it within our movement code by simply adding in the value of the rotation. Again, we can skip worrying about if we are getting a positive or negative value as we have it being determined by which button is pressed.
As for the roll, we have it being multiplied by a negative. We could just switch the inputs within our action map, but I went with this way instead. Now, let’s take a look at how it appears when we play:
Now that we have our yaw, roll and pitch all set up, we can get to work on the lift and descent of our drone. This one is a simple switch over, but let’s take a look at the difference in the legacy code and the new code:
With this, we are doing the same as we did with that of our yawn, using the float value of the vector1 inputs and applying it to the force being applied to the drone. From here, we can see how it goes within the game:
There we have it. We have successfully changed a drones input from a legacy input system to the new input unity input system.