Restricting Camera movement along the x axis

I’m using an orthographic camera which I control left and right movement like such:

void Update () {

//amount to move	
float amountToMove = Input.GetAxisRaw("Horizontal") * CameraSpeed * Time.deltaTime;

//move the camera
transform.Translate(Vector3.right * amountToMove);

}

I need to restrict the camera so it cannot move past certain point of the x axis. I tried using clampf but I can’t seem to get it to work. Any suggestions?

1 Answer

1

At the end of all that, do something like

transform.position = new Vector3(
Mathf.Clamp(transform.position.x, minimum, maximum), 
transform.position.y, transform.position.z);

where minimum and maxium are floats you defined earlier in the script.

Whoops, I forgot my Mathf.Clamp syntax! Should've checked the documentation first... Fixed!

–

Very long after this post, but.......this worked great for my overhead tracking cam of a golf ball on a fairway - only thing is - its a little harsh when it picks up the ball and starts tracking it. Anyway of smoothing this out? Lerp or something? But how do I implement it in this as an example: Mathf.Clamp(transform.position.x, minimum, maximum), Thanks.

–