Handling Input

At some point in your development journey, you may find yourself wanting to let players of your project utilize fancier input methods, rather than simply having them hit specific, default keys on their computer keyboard.

TOOLS

Unity does include some basic gamepad support; you can go to Edit->Project Settings->Input in the editor to map keys and joystick buttons to what Unity calls “Input Axes” (which you can refer to via script), but it is difficult to know what each input on your device of choice is named by the engine, so Unity’ inbuilt system does not come highly recommended if you want to provide maximum ease-of-use to you, the developer, not to mention your players.

Enter: InControl. There is a free,open-source version that can be found here, and a paid version on the Unity Asset Store.

The free version of InControl provides standardized control mnemonics for easy input referencing, some basic device detection and device profiles, and let’s you create custom input profiles of your own. If you’re just starting out and want to see what its all about, I recommend giving this a try.

The paid version includes more advanced features, such as run-time rebinding of controls, and more and better device support, and I recommend it if you are intent on shipping your project.

SYSTEM DESIGN

At some point during development of a sufficiently complex project, Input checking will start to become a tangled mess split across multiple scripts and it can be easy to lose track of it all. To make things easier and not accrue too much ‘technical debt’, I recommend creating a InputManager script.

public class InputManager : MonoBehaviour {

  public Player player;
  public SomeComponent component1;
  public AnotherComponent component2;

  //set these to 'false' if you don't want input handled on them
  public bool playerEnabled, comp1Enabled, comp2enabled;

  void Update(){
    //for each component, check if it is enabled
    //if it is, do its input handling routine
    if(playerEnabled)
        player.HandleInput();
    if(comp1Enabled)
        component1.HandleInput();
    if(comp2Enabled)
        component2.HandleInput();
    //check more things here
  }

}

Using a construction like this, you can toggle on/off all of your individual components (i.e. scripts attached to gameobjects) that need input whenever you want, usually upon various game events being triggered. Just make sure each component has a HandleInput() method which does the desired input checking and you are all good to go. This should help keep your input checking logic very easy to follow and organized!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s