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 getyour411 · Sep 05, 2013 at 05:09 AM · booleanoperator

C# please explain this line of code

 moveDirection = new Vector3((Input.GetMouseButton(1) 
 ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));

Found the MSDN ? operator page but I don't get this one. Input.GetMouseButton(1) is the boolean test clause and ?/if it's true then do Input.GetAxis("Hori... but if it's false (:) then do other stuff. It's the 2nd part I don't get. What's the 0),0 right after the : doing?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by whydoidoit · Sep 05, 2013 at 05:13 AM

So that says:

Create a movement vector that uses the Horizontal axis only if the right mouse button is down and is 0 otherwise, and the Vertical axis always.

The x component of the Vector3 is the GetAxis("Horizontal") if the GetMouseButton(1) is held and is 0 otherwise. The other two components of the Vector3 are the same in all cases.

Comment
Add comment · Show 2 · 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 getyour411 · Sep 05, 2013 at 06:02 AM 0
Share

If right.mouse is held, result is:

 new Vector3((Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));

or if right.mouse is not/otherwise, result is:

 new Vector3((0,0,Input.GetAxis("Vertical"));

Do I have that right? I've seen a few examples of the ?: (thanks PeterG did not know what to call that) but usually to do straight if/then/else logic, this seemed different and I didn't understand. I think I do now. It's elegant, but unfamiliar at first.

avatar image lukad · Sep 05, 2013 at 06:26 AM 0
Share

Yes, that's correct. The ?: notation is called ternary operator by the way.

condition ? true : false

avatar image
0

Answer by Peter G · Sep 05, 2013 at 05:17 AM

It's because whoever wrote the code is writing it into a Vector3. The ?: (It's called the conditional/ternary operator) is setting the x value of the Vector3. Then the y component is 0, and the z is the other axis.

It would probably make more sense written like this:

 moveDirection = new Vector3(  Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0 //x
      ,0 //y
      ,Input.GetAxis("Vertical") ); //z

There was also an extra set of ()'s.

Comment
Add comment · 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

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

19 People are following this question.

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

Related Questions

Script throwing error Operator '==' cannot be used with a left hand side of type System.Object and right hand side of type Controller 1 Answer

How to handle different bools in 1 statement 1 Answer

In Javascript, can I reverse a boolean as so: boolean = !boolean 1 Answer

Boolean Toggle Cross-Script No Worky? 1 Answer

Player movement isn't working 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