- Home /
Checking number against boolean
So this works fine…
var direction.x = Input.GetAxis("Horizontal");
if (direction.x)
// direction is a positive value
else
// direction is a negative value
.
But the following doesn't validate. Is it not possible to compare a number with a bool in this manner?
var userSetting : bool;
if (direction.x == userSetting)
// Both values are either negative or positive
.
// Edit: What I'm trying to do is check whether player is facing the correct direction to grab onto a ledge (Designer sets the ledge side manually) so player cannot grab a ledge with his back.
Just compare against 0 ins$$anonymous$$d of trying to use booleans. Your first example doesn't actually work, since there are 3 possibilities: greater than zero, less than zero, or zero.
Regarding your edit...again, just compare against 0. It doesn't make any sense to be using booleans in this context.
Thanks Eric, but I can't figure a way how to check against zero that's simpler than this…
if (direction.x && userSetting || !direction.x && !userSetting)
How would you be doing it?
A boolean is true or false, direction.x is a float, so they aren't related. You can use System.$$anonymous$$ath.Sign if you're just concerned about < 0, == 0, or > 0. ($$anonymous$$athf.Sign doesn't really work right, since it returns 1 if the input is 0.)
Answer by BiG · Feb 01, 2012 at 09:34 AM
To check if direction.x is positive and userSetting is TRUE in the same test, just do this:
if (direction.x && userSetting)
instead that:
if (direction.x == userSetting)
, that you've used. That instruction couldn't work because the domains of the two variables are different.
Thanks but it's not that I want to check if they're both true, I want to check if they're both the same. I guess I could use…
if (direction.x && userSetting || !direction.x && !userSetting)
…but it felt a bit odd. I thought I might be able to just ask if they're the same but I guess it can't be done. Thanks though!
O$$anonymous$$...So, "check if they are the same" is nonsense, for the reason that I told you above. It's right to say "check if their truth value is the same, for the compiler": that means, as you rightly said, check if they constitute the pair or the pair .
For short, the code that you've provided in the comment above is all you can do.
Or you could try a XOR operator: if(direction.x ^ userSettings)
cool idea but apparently unity doesn't let you compare a float against a bool, even using that operator. I guess it's not treating the float as a boolean at all within the if statement, but that simply a positive will trigger the if statement. At least that's my understanding.
Answer by asafsitner · Feb 01, 2012 at 08:54 AM
It's not working because GetAxis
returns a float, not a bool.
You can instead make userSettings an int (or short, or byte or what have you) and then check if it's greater than 0 (true) or equal to 0 (false).
EDIT: Remember to NOT compare a float against an int. So in your example the if statement should be more like if(direction.x && userSettings > 0)
Yeah, I know the value is a float but it can be checked within an if statement as if it was a boolean. Was just wondering if it can be checked against another boolean within the same if statement. Guess not.