- Home /
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?
Answer by syclamoth · Sep 28, 2011 at 04:49 PM
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.
Thanks works perfectly I just needed to put the $$anonymous$$ and max the other side of transform.position.x.
Whoops, I forgot my $$anonymous$$athf.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:
$$anonymous$$athf.Clamp(transform.position.x, $$anonymous$$imum, maximum),
Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220612230240im_/https://answers.unity.com/themes/thub/images/avi.jpg)