What's the best way to manage/handle input in a game?

How would you do it? Do you have like, a ‘InputManager’ object that controls all input? or you just throw input detection all over the place? - Normally, I would have controls separated in different areas where they’re used. For example player controls belong in the player script, inventory controls belong to the inventory script, etc.

So what’s the best way to organize/manage all the inputs in your game? and why.

1 Like

Unity does have an Input Manager built into it. If you go to “Edit” → “Project Settings” → Input, the Inspector panel will show the Input Manager.

For example, you can use it as follows:

// the following line uses Translate(x, y, z)
transform.Translate(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical");

By default, the “Horizontal” access is mapped to A and D for left-and-right movement, respectively, while “Vertical” is mapped to W and S. This is particularly useful as you can create a placeholder for input instead of hard-coding it in, and it allows the player to tweak the individual keys to suit themselves.

Thanks for your reply but that’s not what I meant. I actually meant where you would put/organize your input handling, I mean how would you organize your input checks (if Input.GetKeyDown(something) // do something; etc) - Do have a main input manager object that in its update you check for all your controls? or your input checks are scattered in separate scripts, like in your player, etc

1 Like

Ah, I didn’t understand the question first time around.

I tend to have mine scattered around depending on their purpose. I’d add firing/attacking commands to a script that is attached to a weapon, and movement controls to a script attached to the player. My reasoning is that keeping them separate means less hassle with sending messages and stuff: for instance, if you had a single object to check for input, it would have to send the “Fire” command to whatever weapon was equipped, while sending the movement commands to the player’s transform.

In short, I think having controls in separate scripts according to their purpose means less hassle.

1 Like

I prefer to build an ‘InputManager’ and I usually attach the script to an empty gameobject that holds other “GameMaster” scripts. This way I have all the input functions in one place.

Your example of having player controls with the player script and inventory controls with the inventory script is a good example why I prefer to concentrate all input related things in one file: Where would you leave the function to open the player inventory? Is that with the player script or the inventory script?