#Help with If Statments

1 messages · Page 1 of 1 (latest)

crude maple
#

i have been working on my own little 2d platforming game where you have to unlock your movement abilities. (if you don't understand what i mean this is my game: https://vicodk.itch.io/moveunlock)
so my question is if i want to use W, Space and up-arrow for jumping is this the right way or can i do something smarter?
if (Input.GetButtonDown("Jump") && IsGrounded() && JumpUnlock == true | | Input.GetKeyDown(KeyCode.W) && IsGrounded() && JumpUnlock == true | | Input.GetKeyDown(KeyCode.UpArrow) && IsGrounded() && JumpUnlock == true)

itch.io

Unlock your Movement

raven kernel
#

I cant really think of any better way to get input from different buttons other then that.
However, the ==true is not needed.
If(JumpUnlock) is If(JumpUnlock==true) (think of it as writing If(true==true) rather then just if(true))

west ocean
#

I may not fully understand your game, though you could try moving your conditions (parts like && JumpUnlock == true) into functions, that way your logic just handles checking for input and the action you want to do (jumping in this case), and everything that action needs (being grounded, and being unlocked in your case) can be handled by the function

You could also look into the "new" input system to register functions to input which can help manage your logic a bit, I also personally like using states to separate actions like abilities, they are commonly just virtual or abstract base classes, but can also be serialized or a scriptable object, though there is usually many ways to approach a problem

I would suggest looking up "samyam" on YouTube to learn more about the "new" input system, and the Unity docs "how do I..." page for common use case code examples with that system and "C# state patterns" or "abstract and virtual classes in C#" if it sounds like a viable approach for your use case

barren yacht
#

Go for Dibbie’s suggestion and use the newer input system, you’ll find your code to be cleaner, by getting rid of redundant if statements :)

If not, an alternative is to go to settings and add your own axis and have it respond ti the keys you want to (I think you can add several keys, the same way ‘Vertical’ responds to both ‘W & S’ and ‘UpArrow & downarrow’