Camera follow object when it reaches edge

I’m struggling to figure out how to actually implement what I’m wanting to do and was wondering if anyone could help.

I have an Orthoscopic Camera and a Rigidbody. The Rigidbody will get launched to the right (x-axis positive) direction. While it’s possible to launch it to the left, there will be an invisible collider that basically stops it from moving left beyond the camera. On the y-axis, I don’t care if it goes above the camera and the ground prevents it from going down.

So I want the camera to follow it in the positive x-axis direction ONLY. To make things a little more complicated, I’d like it to move with the object once it gets almost to the edge of the screen. Think like 20% left before it gets to the edge. Once the object continues in the positive x-axis position, I want the camera to keep up with it so it never goes outside the camera.

I’m thinking along the lines of mathf.clamp but I’m still unsure how to implement it so maybe I’m way off.

This a problem that is semi-common and there are a couple solutions I’ve seen.

The one I’m partial to the most is defining dead zones in terms of screen coordinates. That is to say, you prevent the camera from panning while your character is within, say, 20% and 80% of the X coordinates of your screen. When the character goes past 80 or 20 then the camera moves to follow the character based on delta movement along the X axis. You can duplicate this to do the same thing on the Y axis. So your camera doesn’t move in world space while your character’s position on screen space is within your dead zone for camera movement.

As far as not allowing the camera to go to the left past a point, just do a check if the camera’s X world position is to the left of the point you want it to stop at. Likewise in preventing the camera moving down, add a check to keep it above.

You can do that with Clamp or just a simple conditional. if(camera.position.y < val) camera.position.y = val; kind of logic.