I am trying to make a camera that starts 0 on the X axis. When you the arrows it moves on the X. I am trying to make it only be able to move 5 in either direction.
This code lets it go as far is it wants and the clamp does nothing
void Update () {
translation = Input.GetAxis ("Horizontal") * moveSpeed;
myTransform.Translate(Mathf.Clamp(translation, -5, 5), 0, 0);
}
Thanks for any help
The clamp function limits your value between two others. But, you are calling the translation function anyway, thus, your object translates. In fact, you should “clamp” the x position of your transform, not the translation vector.
void Update () {
Vector3 position = myTransform.position ;
translation = Input.GetAxis ("Horizontal") * moveSpeed;
if( myTransform.position.x + translation < -5 )
position.x = -5 ;
else if( myTransform.position.x + translation > 5 )
position.x = 5 ;
else
position.x += translation ;
myTransform.position = position ;
}
You need to clamp the output mytransform coords, not the input translation.