Overriding the Unity input system

One of Unity's most compelling features is its ability to be cross-platform across any system, and with that comes several helpful layers of abstraction that we can use to inject our code into. However, the game in question needs to be following the Unity best practices in order to make this injection easy. That isn't to say that we couldn't do it by overriding the game's input system; it just wouldn't be as easy.  

Before we get into describing how the injection works, let's take a step back and look at the best practices for using the Unity input system. Over the years, the Unity input system has evolved from a simple query that the device uses for inputs to the more cross-platform system that it uses now. However, many developers, including Unity itself, still use input methods that query a particular key code, for instance. The best practice is to define a set of axes (input channels) that define the input for the game.

We can easily see how it is currently defined in the game by following this exercise:

  1. From the editor menu, select Edit | Project Settings.
  2. Select the Input tab and then expand Axes | Horizontal and Axes | Vertical, as shown in the following screenshot:

Inspecting the Input Axes settings
      1. The Vertical and Horizontal axes define the input that will be used to control the game. By defining them in this tab, we can control the input across platforms by querying the axes. Notice that the axis input allows us to define both the button and joystick (touch) input. The output of a query to the input system with getAxis returns a value from -1 to +1, or continuous output. This means that we can take any discrete form of input, such as a keystroke, and immediately convert it to a continuous value automatically. For example, if a user presses the W key, the input system coverts that to a positive 1 value on the Vertical Axis, and conversely, a press on the S key generates a negative 1 value, again on the Vertical AxisLikewise, the A and D keys control the Horizontal Axis.
      As you have seen in a few chapters in this book, using the .6 version of ML-Agents, the current discrete action solution is not nearly as good as the continuous action. Therefore, it will be our preference going forward.

      At this point, you may be wondering why we used discrete actions at all; that is a good question. It remains to be seen how Unity will handle this dichotomy in the future. In the next section, we will look at how to inject into the input system.

      ..................Content has been hidden....................

      You can't read the all page of ebook, please click here login for view all page.
      Reset