Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 airbagnr1 · Oct 05, 2018 at 01:40 PM · transform.positionif-statementscleaning

Tidying up IF statements with '<' '>' in them

Hi there,

Im trying to tidy up a bunch of IF statements and I cant think of a way passed it due to the greater than and less than symbols. If anyone could offer any suggestions would be great! Here is my code:

   if (transform.position.x > groundSizeX)
         {
             transform.position = updateX(-groundSizeX);
         }
         if (transform.position.x < -groundSizeX)
         {
             transform.position = updateX(groundSizeX);
         }
 
         if (transform.position.z > groundSizeZ)
         {
             transform.position = updateZ(-groundSizeZ);
         }
         if (transform.position.z < -groundSizeZ)
         {
             transform.position = updateZ(groundSizeZ);
         }
Comment
Add comment · Show 1
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 airbagnr1 · Oct 05, 2018 at 01:52 PM 0
Share

Hi there, as a side note - the updateX and update Z code is as follow: Vector3 updateX(float x) { return new Vector3(x, transform.position.y, transform.position.z); }

Was thinking about maybe using ?? but not sure how that works...

1 Reply

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

Answer by Hellium · Oct 05, 2018 at 02:05 PM

 void YourFunction()
 {
     Vector3 position = transform.position;
     position.x = LoopValue( position.x, -groundSizeX, groundSizeX ) ;
     position.z = LoopValue( position.z, -groundSizeZ, groundSizeZ) ;
     transform.position = position ;
 }
 
 private float LoopValue( float input, float min, float max )
 {
     return input < min ? max : input > max ? min : input ;
 }

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 airbagnr1 · Oct 05, 2018 at 04:03 PM 0
Share

Ah thanks so much man! Really trying to improve my code, but finding it hard to find alternative ways to do things!

I know the single question marks mean if but im not sure what the colon ':' means? Any chance you could describe that for us?

Thanks again!

avatar image TreyH airbagnr1 · Oct 05, 2018 at 04:24 PM 0
Share
 var a = [what you are checking] ? [value if true] : [value if false];
avatar image TreyH TreyH · Oct 05, 2018 at 04:25 PM 0
Share

In that function, the value if false is another embedded comparison.

avatar image Hellium airbagnr1 · Oct 05, 2018 at 08:09 PM 0
Share

The conditional operator (?:), commonly known as the ternary conditional operator, returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.

 condition ? first_expression : second_expression;  

The code return input < $$anonymous$$ ? max : input > max ? $$anonymous$$ : input ; could have been translated as follow:

  if( input < $$anonymous$$ ) return max;
  else if( input > max ) return $$anonymous$$;
  else return input ;

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

95 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 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 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 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 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

if player transform position x do something 1 Answer

How does this conditional evaluate? -- Repeating Background Script 1 Answer

C# 2D platformer: How to have player always face center? 0 Answers

Object doesn't move 2 Answers

Transform.Position when Play changes 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