- Home /
Trouble using OR operator with two inputs
Hello.
I'm trying to check if either of two inputs are pressed before moving along to the next dialogue. I've tested each input separately and they both work without the Or operator. Not sure why the || operator isn't working here. Thanks.
while (!Input.GetButtonDown("Submit") || !Input.GetKeyDown("space"))
{
yield return 0;
}
Answer by AfterMath24 · Mar 09, 2018 at 08:14 PM
@duckquark The issue with your original code was that you were attempting an or (||), when you should have used an and (&&).
As long as you are not pressing both Input keys in the same frame then the while loop will keep going
If you press Submit your statement would look like
While(False || True)
{
//Code
}
As long as there is one True the while loop will continue. Changing the code to use an and would make it look more like this when you press Submit.
While(False && True)
{
//Code
}
This would break out of the while loop as both must be True for the while loop to continue.
Hopefully that made sense.
Exactly. This is basic boolean algebra.
!(A || B) == !A && !B
If you have an "AND" gate and invert the inputs as well as the output it will turn into an OR gate. The same is true for the reverse. So an OR gate will turn into an AND gate when you invert the input and the output. In this specific case we actually want a "NOR" gate (and or with negated output). When you invert the inputs as well as the output and you replace the OR with an AND you get the same result.
If you have $$anonymous$$ecraft you can practise those simple things as the only things you have available in game are an OR and a NOT gate. They can be used to create any other logical gate.
So the actual solutions are either
while (!Input.GetButtonDown("Submit") && !Input.Get$$anonymous$$eyDown("space"))
or
while (!(Input.GetButtonDown("Submit") || Input.Get$$anonymous$$eyDown("space")))
Answer by duckquark · Mar 09, 2018 at 07:50 PM
I found a solution. I added space as a alternate positive button in the InputManager. So I only have the one condition. And it'll check for either of the buttons.
while (!Input.GetButtonDown("Submit"))
{
yield return 0;
}
Your answer
Follow this Question
Related Questions
Inconsistency with For Loop 2 Answers
while loop issue 1 Answer
How to stop infinite loops in turn based game. 3 Answers
Waiting for a mouse click in a Coroutine 1 Answer
apply something on enum 1 Answer