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
10
Question by G_Sacristan · Jul 14, 2011 at 12:27 PM · mathfclamp

mathf.clamp

Could someone explain what Mathf.Clamp does with an example in his own words(cant get the meaning out of scripting reference)?

Thanks in advance!

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
35
Best Answer

Answer by Peter G · Jul 14, 2011 at 12:42 PM

There are two overloaded versions so its slightly different but very similar.

  1. You insert your value.

  2. Give it a minimum

  3. Give it a maximum.

  4. Unity will make sure that your value is never smaller than the min and never larger than the max.

You could write something like this for the same effect.

 function Clamp (value : float , min : float , max : float) : float {
     if(value < min)
          value = min;
     if(value > max)
          value = max;
     return value;
 }

so if you clamp between 0 and 5, your value can be any number between those two. So these points are all valid:

 value = 3.0
         2.3
         1.0
         0.4
         4.9
        .000000000001
        .02
        .49999

but if you try to go above 5 or below 0, then this function will clamp the value at the appropriate min or max.

And the integer version is the same except that the values are all restricted to integers.

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 Skjalg · Jul 14, 2011 at 12:43 PM 0
Share

Very nice answer!

avatar image
6

Answer by Skjalg · Jul 14, 2011 at 12:38 PM

it basically means that it constraints the value between the min and max.

 float value = 10f;
 value = Mathf.Clamp(value, 0f, 5f);
 
 print("the value is clamped to: " + value);
 //prints 5
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 Bunny83 · Jul 14, 2011 at 12:50 PM 0
Share

You forgot the value parameter x)

avatar image
3

Answer by save · Jul 14, 2011 at 12:46 PM

With Mathf.Clamp you set a floor and a roof for a float- or int value. The value can never exceed the negative or positive clamped values.

Example:

 //Clamp the Y-value of the Mouse Input to not exceed certain angle when tilting camera
 private var ytargetRotation : float = 10;
 private var min=-80;
 private var max=80;
 function Update () {
     var yAxisMove : float = Input.GetAxis("Mouse Y")*5;
     ytargetRotation+=-yAxisMove;
     ytargetRotation=ytargetRotation % 360;
     ytargetRotation=Mathf.Clamp(ytargetRotation,min,max);
 }

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
1

Answer by 123_umer · Oct 10, 2018 at 04:35 PM

the one thing everyone missed was that the value parameter is set in which min and max are defined. so the returning value can range between min and max

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 urii_baba · Oct 10, 2018 at 04:40 PM 0
Share

yup that exactly what i was looking for ! you made my life easier bro ! (Y)

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

13 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

Related Questions

Mathf clamp movement to Camera space in js 0 Answers

Mathf clamp movement to Camera space in js 1 Answer

Performance of Mathf.Clamp vs if-else statement? 1 Answer

Clamp Rotation Problem 1 Answer

Help clamping a Rotation 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