Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Closevalue | The floating point value to restrict inside the range defined by the minimum and maximum values. |
min | The minimum floating point value to compare against. |
max | The maximum floating point value to compare against. |
float The float result between the minimum and maximum values.
Clamps the given value between the given minimum float and maximum float values. Returns the given value if it is within the minimum and maximum range.
Returns the minimum value if the given float value is less than the minimum. Returns the maximum value if the given value is greater than the maximum value. Use Clamp to restrict a value to a range that is defined by the minimum and maximum values.
Returns an undefined value if the minimum value is greater than the maximum value.
using UnityEngine;
// Mathf.Clamp example. // // Animate a cube along the x-axis using a sine wave. // Let the minimum and maximum positions on the x-axis // be changed. The cube will be visible inside the // minimum and maximum values.
public class ExampleScript : MonoBehaviour { private float xMin = -0.5f, xMax = 0.5f; private float timeValue = 0.0f;
void Update() { // Compute the sin position. float xValue = Mathf.Sin(timeValue * 5.0f);
// Now compute the Clamp value. float xPos = Mathf.Clamp(xValue, xMin, xMax);
// Update the position of the cube. transform.position = new Vector3(xPos, 0.0f, 0.0f);
// Increase animation time. timeValue = timeValue + Time.deltaTime;
// Reset the animation time if it is greater than the planned time. if (xValue > Mathf.PI * 2.0f) { timeValue = 0.0f; } }
void OnGUI() { // Let the minimum and maximum values be changed xMin = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), xMin, -1.0f, +1.0f); xMax = GUI.HorizontalSlider(new Rect(25, 60, 100, 30), xMax, -1.0f, +1.0f);
// xMin is kept less-than or equal to xMax. if (xMin > xMax) { xMin = xMax; }
// Display the xMin and xMax value with better size labels. GUIStyle fontSize = new GUIStyle(GUI.skin.GetStyle("label")); fontSize.fontSize = 24;
GUI.Label(new Rect(135, 10, 150, 30), "xMin: " + xMin.ToString("f2"), fontSize); GUI.Label(new Rect(135, 45, 150, 30), "xMax: " + xMax.ToString("f2"), fontSize); } }
value | The integer point value to restrict inside the min-to-max range. |
min | The minimum integer point value to compare against. |
max | The maximum integer point value to compare against. |
int The int result between min and max values.
Clamps the given value between a range defined by the given minimum integer and maximum integer values. Returns the given value if it is within min and max.
Returns the min value if the given value is less than the min value. Returns the max value if the given value is greater than the max value. The min and max parameters are inclusive. For example, Clamp(10, 0, 5) will return a maximum argument of 5 and not 4.
using UnityEngine;
// Mathf.Clamp integer example. // // Add or subtract values from health. // Keep health between 1 and 100. Start at 17.
public class ExampleScript : MonoBehaviour { public int health = 17; private int[] healthUp = new int[] {25, 10, 5, 1}; private int[] healthDown = new int[] {-10, -5, -2, -1};
// Width and height for the buttons. private int xButton = 75; private int yButton = 50;
// Place of the top left button. private int xPos1 = 50, yPos1 = 100; private int xPos2 = 125, yPos2 = 100;
void OnGUI() { GUI.skin.label.fontSize = 20; GUI.skin.button.fontSize = 20;
// Generate and show positive buttons. for (int i = 0; i < healthUp.Length; i++) { if (GUI.Button(new Rect(xPos1, yPos1 + i * yButton, xButton, yButton), healthUp[i].ToString())) { health += healthUp[i]; } }
// Generate and show negative buttons. for (int i = 0; i < healthDown.Length; i++) { if (GUI.Button(new Rect(xPos2, yPos2 + i * yButton, xButton, yButton), healthDown[i].ToString())) { health += healthDown[i]; } }
// Show health between 1 and 100. health = Mathf.Clamp(health, 1, 100); GUI.Label(new Rect(xPos1, xPos1, 2 * xButton, yButton), "Health: " + health.ToString("D3")); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.