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

HACKED BY SudoX — HACK A NICE DAY.

Share via Social Media

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

30 Comments