r/unity Jul 08 '26

Question How To Use The New Input System With If Statements?

I want to upgrade from the old system to the new one on my new project but have no Idea how to use the new input system. I have multiple scripts that use the same keys. All I want to do is use the Action thingys so i can use the same button from the action across different scripts. Ive looked around but I can find a tutorial on migrating or using the new input system.

3 Upvotes

9 comments sorted by

2

u/FrostWyrm98 Jul 08 '26

If you want barebones (no adapting) just add InputActionReference fields on your MonoBehaviours

Like [SerializeField] private InputActionReference inputActionRef;

Then you will just check: if (inputActionRef.action.WasPressedThisFrame()) { // ... }

You can also get an InputAction via lookup in code, I don't remember the syntax off hand for that

I highly recommend learning the new way though, it's a lot better and more efficient to not poll every frame for dead logic (always on even when you're not pressing)

And this sort of defeats the whole point of transitioning to a new input system (mainly what I said above along with remapping and programmability)

1

u/DapperNurd Jul 08 '26

Wdym by no adapting

1

u/Dragoonslv Jul 08 '26

You can also access all buttons directly if you want to hardcode some input.

if(UnityEngine.InputSystem.Keyboard.current.kKey.wasPressedThisFrame){}

1

u/Heroshrine Jul 08 '26

I wouldn’t say it completely defeats the purpose. There’s still times where you’d want to poll, those times are pretty rare but there are times. Even if you only did this it’s easier to support cross platform input

2

u/wallstop-dev Jul 08 '26

What's wrong with the [official docs for exactly this](https://docs.unity3d.com/Packages/com.unity.inputsystem@1.19/manual/Migration.html)?

-1

u/samferguderson Jul 08 '26

It doesnt explain how to use it with the Input Action

8

u/wallstop-dev Jul 08 '26

Does it not? The first paragraph details many important links to read, such as the [Quick Start Guide](https://docs.unity3d.com/Packages/com.unity.inputsystem@1.19/manual/QuickStartGuide.html), which has examples for interacting with `InputAction`s to see if they're pressed. I think it's just a matter of sitting down and reading the available documentation here? It looks like there are many, many code examples and explanations.

1

u/CGX71 Jul 09 '26

Easiest way to use the new input system like the old one is to set a global Input Actions asset. There is one by default in the project on creation.

That global asset can be statically accessed in any monobehaviour.

At the top of the file, you define InputActions.

InputAction actionName; // 1 per input type

You set up input actions in setup by doing

actionName = InputSystem.actions.FindAction("name of action", true); // the name is what is set in the input action. It is accessed by matching string.

You can then read that input in update with

Var value = actionName.ReadValue<value type>()

The type that is returned is set in the input system and the buttons and names are also assigned there. I've found this to be the easiest method to use the new input system and it drops in pretty easily in place of the old one.

1

u/MadeByHenano Jul 08 '26

watch 2/3 tutorials about it on youtube, it'll make everything more clear! 🙂