- Home /
How to use if statements with New Input System?
Hi, I'm working on a game, where the user picks up trash in Trash cans and dumps them into a Trash Truck. I've decided to convert over to the New Input System, for multiple controller support, and whatnot. However, I've run into a problem with an asset I'm using, called First Person All-in-One. I can't seem to be able to get it to work so that it moves the character when the player presses a button. It used to use Input.GetButtonDown for this, I'm using (in this case, the name of the action is jumpKey,), jumpKey == 1. It will detect these inputs in the script, but it doesn't seem to do anything with it.
If anyone has any ideas, please, let me know! Thanks.
https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/QuickStartGuide.html
I suggest reading the documentation because (whilst I have very little experience with the new input system) it seem immediately apparent that it is event based now which is far better/ more efficient than polling essentially on each frame for input events.
Is there a reason you want to use an if statement in your update method? If so is there a reason you don't just use the legacy input system?
Note, if you still want to poll the input: https://forum.unity.com/threads/update-polling.698543/
Answer by MT714 · Jan 09, 2021 at 09:03 PM
Supposing you know how to work with the input action asset, you need to create a new action called however you want, lets say "Walk", set the action type to Button and adding a binding to that action corresponding to your jumpkey. Add the "press" interaction to the "Walk" Action and set the trigger behaviour to "press and release".
Next you need to get that input to your script. The easiest way to do so (but not the only) is to add the "Player Input" component to the gameobject you need to be moving (which i guess it's the Player), set the right InputActionAsset and then decide the behaviour.
I would suggest using "Send Messages", but the choice is yours. If you use "Send Messages" or "Broadcast Messages", you can directly receive the input in any other script attached to that same gameobject.
In this case, in your script use this function:
bool canWalk = false; //you need a variable to store the input
void OnWalk(InputValue val) {
canWalk = val.isPressed;
}
Remember to write using UnityEngine.InputSystem;
at the beginning of your script
now you have a variable called canWalk
, wich is basically equivalent to the GetKeyDown(...)
method of the old Input System, so you can simply use if(canWalk)
to check if the key is pressed
Note that if you decide to name the action differently, you need to change the function name from OnWalk(...) to On[whatever name you gave to the action](...)
Ok, thanks. I had actually gotten it to work earlier yesterday, I only just saw this now, today, sorry! Thanks for the response though. I am stuck actually on trying to get running working, for this Asset Script, I'm trying to make it so that it uses the $$anonymous$$ulti-Tap Feature, I don't know how to get it to work though. It appears as if it's just only running / sprinting for a short amount of time, like, a split second. Do you know how to get $$anonymous$$ulti-Tap to work properly? If you need any more clarification just let me know, and, again, thanks!
Does the player control the character movement when walking/running or is it automated such that you only need to press the button to walk (and double tap it to run)? Also, does the player need to hol it or just press it once?
I asked you the first question because maybe the problem derives from the fact that, in the case the player also needs to input the movement direction, you're not providing an InputAction that allows the player to tell the system where the character has to go, so all you would need to do is to add another InputAction to provide that information (maybe as Vector2?).
If that's not the problem at all, because you've already figured it out/the movement is automated, then the problem obviously derives from the misuse of the $$anonymous$$ultiTap Interaction.
Hi, again, sorry for the late response, I've been caught up with other things. The Player would just use WASD for the $$anonymous$$ovement, or the Left Joystick for alternate controls. The Player can use Left Shift for Running with Keyboard Controls, or, I want it to be so that, with alternate controls, I want it to be so that the Player has to tap once, then keep the Left Joystick Up in order to Run, and then they can move around the Left Joystick to sprint in that direction, and as soon as they release it, they stop running / moving.
$$anonymous$$ultiTap works the best with InputActions of type Button has three foundamental parameters:
tap count: the number of taps to trigger the action; let's assume you keep the value to 2
max tap spacing: the max amount of time that can pass between each tap, starting from when a tap is released to the moment the following tap is pressed
max tap duration: the max amount of time a tap can last till the Action is canceled, in other words, how much time you can hold the tap. Note that in order for the Action to be triggered the last tap (the second one) must be released
I'll ignore press point parameter, which is for trigger buttons like the ones on the gamepads
The default behaviour of an Action of type Button is to only trigger once, when the action is performed; in the Walk example i told you to add the "press" interaction and set it to "press and release" to change this behaviour, otherwise it would have only be triggered when you pressed the button down, making the "walk" variable always true.
In this case, if you used the same code I provided you in with Walk, (this time using a method called OnRun i guess), with the default behaviour the method is called only when the action is triggered with the "multi tap" interactions, hence it only triggers when you release the second tap, meaning that val.isPressed will always result in false
.
You can change this behaviour by again adding the "press" interaction ("press and release"). This not only makes sure to trigger the action twice, when you press and release the double tap, but also makes sure to provide a true
value if you hold the button down after the max tap duration of the last tap, and false when you finally release it.
Please note two things:
-the order of the interacions matters: you should put "multi tap" before "press", otherwise the action will trigger two mroe times for each individual tap.
-if you bind Run and Walk to the same key, triggering one will not exclude the other, so make sure to consider that in your game's logic (this means that while your're running, the canWalk
variable will still be true)
Hopefullly this helped, if not make sure to ask me again and I'll do what i can
Sorry I had to split the answers or it wouldn't have let me post it. Btw I've never used $$anonymous$$ulti tap before, so I tested it out when i read the comment an hour ago. With this I want to say that not everything I said could be right, because I just told you what i got from my testing, but hopefully I made no mistakes