Using Mathf.Clamp function to restrict values in your game mechanics to within a specified range.
Code Used (Javascript)
function Update () {
-
var xMove : float = Input.GetAxis("Horizontal") * Time.deltaTime * 20;
-
transform.Translate(Vector3(xMove,0,0));
-
transform.position.x = Mathf.Clamp(transform.position.x, -10, 10);
-
}