Keeping it Clean with New Input System

Now that we have looked over a bunch of ways that we can utilize the new input system in Unity, let’s see how building it the current way can cause issues with scripting. As it stands now, enabling all of these input actions and then creating methods for it all can quickly add up to making our scripts long. For example, let’s take a look at building a player that can move around, fire, reload, and sprint.
If we decided to just keep all of the action inputs within our player script, we get it starting to look a bit like this:

Now, this is just for 4 separate actions within our test. Imagine if the player had another 10 actions built in. For every action, we are looking at having a lot of built up code just for the input and then for the actual action itself. Let’s say we come up with an issue later in the game and need to look for something in relation to movement. With this, we would have to scroll through hundreds of line of script to find all the relevant code that we have related to movement and see what is causing the issue.

Now, to get this looking a bit cleaner, we will want to create an empty object and make it our Player Manager. This will be a parent object to our player and will hold the scripting for all of the Input commands of the player:

Once we have our Player manager in place, we can start on splitting up the code between the scripts:

Within our Player Manager script, you can see that all that is being done is performing our actions and calling the method that is set up in the player script. We don’t need to make any values to decide how fast the sprint is, or how much ammo we need. All that we do within the manager is call the action and connect it to the method that we want it to be connected with:

Now within the Player script, it is a lot cleaner. We can focus on just making sure that the methods that relate to the player are proper. We can give the values that we want for those actions, and not have it get cluttered up by having all the additional Input commands. If there was an issue with part of the script we were working with, we can troubleshoot that issue a lot easier, as we don’t need to scroll through the inputs along with the actual methods we are using.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store