Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Essential · Feb 01, 2012 at 08:48 AM · booleanintnegative

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Feb 01, 2012 at 08:55 AM 1
Share

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.

avatar image Eric5h5 · Feb 01, 2012 at 10:10 AM 0
Share

Regarding your edit...again, just compare against 0. It doesn't make any sense to be using booleans in this context.

avatar image Essential · Feb 01, 2012 at 10:28 AM 0
Share

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?

avatar image Eric5h5 · Feb 01, 2012 at 10:44 AM 1
Share

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.)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Essential · Feb 01, 2012 at 10:00 AM 0
Share

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!

avatar image BiG · Feb 01, 2012 at 10:12 AM 0
Share

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.

avatar image asafsitner · Feb 01, 2012 at 10:18 AM 0
Share

Or you could try a XOR operator: if(direction.x ^ userSettings)

avatar image Essential · Feb 01, 2012 at 10:33 AM 0
Share

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.

avatar image
0

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)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Essential · Feb 01, 2012 at 09:13 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Convert positive float to negative? 5 Answers

How can I move back to Element 0 in an array to reset a loop? 1 Answer

GameObject variable doesn't get filled in 0 Answers

Get the number of false bools in GameObject[] (array) 3 Answers

Turn Off int using Boolean 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges