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 /
  • Help Room /
avatar image
0
Question by DevMerlin · Feb 23, 2016 at 02:51 PM · c#mathf.clamp

Is there any way to reduce this bit of Mathf.Clamp code for spawn positions down to one line?

I have a fairly basic stage set up where the players are spawned on the opposite side around natural zero of the world - they end up in positive / negative positions. I would like to clamp them so that neither can leave the 2D playing field. At current, I have that accomplished by using this snippet:

     Vector3 SpawnClamp = _transform.position;
     // Positive
     if (PlayerIDNumber == 0)
     {
         SpawnClamp.x = Mathf.Clamp(SpawnClamp.x, SpawnPosition.x, -SpawnPosition.x);
     }

     // Negative 
     if (PlayerIDNumber == 1)
     {
         SpawnClamp.x = Mathf.Clamp(SpawnClamp.x, -SpawnPosition.x, SpawnPosition.x);
     }

However, it feels like there must be a simpler way to do this without repeating the line twice. I would rather reduce it down to one or two lines of code - if it is at all possible. Can that be done?

Comment
Add comment · Show 2
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 Salmjak · Feb 23, 2016 at 03:17 PM 0
Share

If you make your playerID number 1 and -1 you can just multiply your positions with *PlayerID and *-PlayerID

avatar image mkondor · Feb 23, 2016 at 03:22 PM 0
Share

exactly my answer from 10 $$anonymous$$s ago but I need to be moderated before my answer is published, it seems :)

1 Reply

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

Answer by flashframe · Feb 23, 2016 at 03:26 PM

If you only have two player IDs you could use a ternary operator

 Vector3 SpawnClamp = transform.position;
 
 float _spawnPos = (PlayerIDNumber == 0)? SpawnPosition.x : -SpawnPosition.x;
         
 SpawnClamp.x = Mathf.Clamp(SpawnClamp.x, _spawnPos, -_spawnPos);
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 DevMerlin · Feb 23, 2016 at 03:42 PM 0
Share

flashframe's comment worked best! I only have two players - it's a simple combat game on a 1D/2D playing field. If it was an Answer, I could accept it!

avatar image flashframe DevMerlin · Feb 23, 2016 at 04:11 PM 0
Share

@Teonnyn I made it an answer for you. Although you should also +1 mkdondor's answer, which is largely the same :-)

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

102 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Please help me! Correct script) , 0 Answers

Raycast instantiation only sometimes working. 1 Answer

How can I make my turret also rotate on the X-axis? 0 Answers

Mathf.Clamp does not work at all. 1 Answer

Save object position after drag? 0 Answers


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