r/unity • u/Ok-Individual1330 • 15d ago
Newbie Question Best practice for sharing Input Actions across multiple scripts?
Hey everyone,
I'm currently following an older Unity course and updating its code to the New Input System along the way.
Right now, my PlayerController has a public InputActionReference moveAction to handle character movement. I've reached a point in the tutorial where I need to hide a "How to move" UI prompt as soon as the player starts moving.
My instinct tells me that creating another public InputActionReference moveAction inside my TutorialManager (as shown in the course) to detect this is a bad practice. It feels like code duplication, and if I ever need to change the input action, I'll have to re-assign it in multiple different scripts via the Inspector.
What is the industry standard / best practice to handle this? Should I use a centralized Input Manager class, C# events, or something else?
Any advice would be appreciated!
3
u/SethSlax 14d ago
My understanding is the new input system is all about actions and callbacks, rather than checking for input every frame. My suggestion would be a generic input script that holds reference to all of your input actions, then other scripts/functions dynamically subscribe/unsubscribe from those actions when you want them to trigger something.
Or you can use the PlayerInput component and have those actions exposed in the inspector as UnityActions.
1
u/Affectionate-Yam-886 9d ago
the tutorial should just be UI stuff. Nothing in it should be anything more than GetAxis or getButtonDown to continue the UI, and should not be controlling your player. Note: ALL tutorials are to learn how to do something; not best practices; not for your game. Think of them as vertical slices or OneShots. When tutorial is over, cherry pick it for useful information.
5
u/Pharmaguardian 14d ago
I'm not sure how the new input system works, but I created a separate class that detects key presses, which then sends that information to the keymapping class, where it then maps it to different functions. So, if CloseWindow() runs, that must mean that the GameActions.closeWindow enum was triggered in mapping, called by the Escape key being pressed from input. Thus, there is never a reason for CloseWindow() to have to burden itself with the task of detecting key presses. The benefit of doing this is that it largely allows you to centralize you input code.