transform.position limits problem

hey, I don’t understand why the mathf.clamp doesn’t work. I followed the example from unity’s manual. Thanks for your support. Have a good day!

var move : float = 0.1;
var translation : float;
var stop : Transform;

function Start () {
stop = transform;
function Update () {
if(Input.GetKey(KeyCode.A)) {
translation = Time.deltaTime * move;
transform.position += transform.up * translation;
stop.position.x = Mathf.Clamp (stop.position.x, 1, 3);


Your code is a bit convoluted. You have a public variable ‘stop’ which is overwritten by ‘transform’, but you use both ‘stop’ and ‘transform’ (which are the same thing) in the code. Likely the problem you are facing is that this:

  transform.position += transform.up * translation;

…moves your object on it’s local ‘y’ axis, and the clamp stops movement on the world ‘x’ axis. So unless the object is rotated 90 degrees on it’s ‘z’ axis so that the local ‘y’ is aligned with the world ‘x’, this code will not stop the movement.