unity 3d student

Beginner B26 – Using Mathf.Clamp to restrict values

Using Mathf.Clamp function to restrict values in your game mechanics to within a specified range.

Code Used (Javascript)

function Update () {
  1.  var xMove : float = Input.GetAxis("Horizontal") * Time.deltaTime * 20;
  2.  transform.Translate(Vector3(xMove,0,0));
  3.  transform.position.x = Mathf.Clamp(transform.position.x, -10, 10);
  4. }

Further Reading

Share via Social Media

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites

15 Comments

    This looks like a simple and great way to limit both the player and objects interacting with each other in a side-scrolling type of game. Thanks for the tutorial! :)

    • Thanks for the comment Ragnarock, there are plenty of uses of Clamp, from animation to screen restriction right down to very simple script elements, perhaps restricting randomisation for example. Happy Devving!

    You’r doing a great work.
    congrats :)

  • Отличные уроки для новичков, спасибо )
    Great lessons for beginners, thx )

  • Good stuff, but the “Next” button links to C01 instead of B27. Thought you should know :-)

    Pe-ads

  • Does this function also work if you use it on things such as a timer or an ammunition value?

    • Certainly, all it does it limit floating point (decimal placed) number values to what you specify, so there are a myriad of explanations.

    Hi great site! I decided to take challenge 3 and step it up a bit. Making a small rotating cannon in a fixed location. However I noticed that whenever I rotate the turret in the X axis it sticks at 90 degrees. Why is this?

  • Just to add to the above, the sticking has nothing to do with the limiting that is going on in the script. It seems that I just cannot rotate any object via script past 90 degrees on the X axis.

  • Hello !

    Thank you for the tutorial. However, what should I do if I want to restrict my object’s movement within the screen’s left and right border instead of fixed coordinates ?

  • I think this MathF.Clamp is really useful for Lerp Intervals ;)

  • 감사합니다

  • These tutorials are totally amazing. Thank you for such a well put together set of tut’s.

    I’ve come from using various sdk’s in the past the last being UDK and I’ll be honest in the fact that scripting has always been beyond my programming skills.

    That is, until I started watching these and going through them on my own afterwards. Everything is very clear and concise and leaves me with something to refer back to later on.

    I can’t believe this website isn’t flooded with comments of thanks tbh.

    Keep up the good work Will and Thank you again ;)

    • Cheers Tyborg, good luck with your development. Scripting is something that makes more sense the more you try out different solutions to problems. What helps me is making small prototypes of ideas and finding solutions / asking for help with them, you’ll learn a lot that way.

    transform.position.x = Mathf.Clamp(transform.position.x, -10, 10);

    doing that on c# doesnt work, this is the solution i found:

    transform.position = new Vector3(Mathf.Clamp(transform.position.x, -4, 4), transform.position.y, transform.position.z);

Leave a Comment