Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Mrroundtree22 · Mar 05, 2021 at 01:26 PM · intrangemathf.clamp

Mathf.Clamp an int value

I want to clamp an int so that its value never goes above 1000 (I want it so that it never goes above this number if I have 999 + 20 the result should still be 1000). I've read documentation but I still can't seem to get it to work. Could somebody explain it to me?

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

4 Replies

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

Answer by Mrroundtree22 · Mar 05, 2021 at 03:44 PM

I just put

 if (myNum > 1000)
         {
             myNum = 1000;
         }


At the end of my method and that worked fine.

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 Bunny83 · Mar 05, 2021 at 06:51 PM 0
Share

Well, if this works, using Clamp or Min does work exactly the same. Mathf.Min is literally defined like this.

If your manual if statement works, the Clamp or Min call would work exactly the same. You haven't posted your original code in your question, so we don't know how you actually used it in your case so we can not tell what you did wrong. However your answer implies that Clamp or Min does not work which is not the case.

avatar image Mrroundtree22 Bunny83 · Mar 06, 2021 at 02:11 AM 0
Share

I'm not disagreeing with you. I genuinely don't know why clamp isn't working for me.

avatar image
0

Answer by Bunny83 · Mar 05, 2021 at 01:54 PM

I think the documentation is already as clear as possible. The Clamp method returns the clamped value and ensures that it's between min and max.

 yourValue = Mathf.Clamp(yourValue, 0, 1000);

Though If you just want to "clamp" the max value but you don't need a min value, you could simply use "Mathf.Min" instead:

 yourValue = Mathf.Min(yourValue, 1000);


Just for completeness, if you only need a min value but no max value you can use "Mathf.Max" instead:

 yourValue = Mathf.Max(yourValue, 0);

Mathf.Clamp can be emulated with a Min and Max call chained together. Those two lines to exactly the same thing:

 yourValue = Mathf.Min(Mathf.Max(yourValue, minVal), maxVal);
 yourValue = Mathf.Clamp(yourValue, minVal, maxVal);

The Min and Max functions are simply defined as

 public static int Min(int a, int b)
 {
     if (a > b)
         return b;
     return a;
 }
 public static int Max(int a, int b)
 {
     if (a > b)
         return a;
     return b;
 }

This is all rather trivial math / logic. Hopefully this clears up the last confustion about the usage ^^.

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 Mrroundtree22 · Mar 05, 2021 at 02:12 PM 0
Share

The documentation is clear but that doesn't solve my problem unfortunately. I've done exactly as you and the documentation described but the number still goes over 1000. I don't understand why.

avatar image
0

Answer by TKDHayk · Mar 06 at 02:34 AM

Clamping the value does not prevent it from ever rising above the max.

Clamping a value simply returns the value, constrained within the min and max.

You can use Mathf. Clamp when retrieving the value to constrain it, but it is not a permanent constraint. It simply returns the number within the min/max range.

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

Answer by DaoGear · Mar 06 at 06:45 AM

myNum = Mathf.Min(newValue, 1000);

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

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

Number Range 3 Answers

Does anyone know why this is wrong? 2 Answers

Making a plain number range 2 Answers

detect if a variable has stop increasing 4 Answers

Increasing or decreasing int values quickly? 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