- Home /
Handling input in one script or multiple?
So I currently have 2 scripts that require input my movement and camera and I wanted to make a new thing that also requires input and it got me thinking if I should continue having my input scattered around or if I should move all of it into one script and then use events like this?
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
useItemInput?.Invoke();
if (Input.GetKeyDown(KeyCode.E))
interactInput?.Invoke();
if (Input.GetKeyDown(KeyCode.Q))
dropItemInput?.Invoke();
}
Would either method be fine or is one better? Or is there a better way that I don't know about?
Answer by Koyemsi · Jun 01 at 05:59 PM
Hi, I think scattering your inputs won't be a problem.
However, I'm not sure that invoking events is the best way ; I would rather target the required scripts and call their methods, like this :
if (Input.GetKeyDown(Keycode.LeftArrow))
MyPlayer.WalkLeft();
if (Input.GetKeyDown(Keycode.Space))
MyGun.Shoot();
if (Input.GetKeyDown(Keycode.Escape))
MyMenu.Show();
Generally I prefer to have them all in one script (usually my GameManager), but sometimes not : I'm currently making a game where my camera inputs are in my CamController script, for better readability.
Just be sure to not have the same inputs in different scripts, of course ;)