Camera view bounded by mesh map area?

I have created a map made of meshes sorted like a puzzle all correctly lined up and each mesh fits perfect next to each other. All together it forms a perfect rectangular map, each mesh is a scale of 100. I have a camera set to Ortho with a size of 300, I have set up camera controls to pan across the map and zoom controls to change size from 300-200. It all works perfectly. However… I can pan way past the map end, I want to prevent this.

What is the relationship between camera coordinates and its size?
Can I determine the total map size by measuring the border mesh lengths?
Can I use those measurements to clamp the camera coordinates in relation to its zoom?

I done some testing… for instance camera of size 300 needs to be clamped between 600 and -600 on x. If the camera is a size 200 (zoomed in) it needs to be clamped between 768 and -768 on x. I need to know some equation that can clamp the camera on any size between 300 and 200. Is there some relationship value I am not aware of?

1 Answer

1

Yes: an InverseLerp inside a Lerp.

var maxX : float = 768;
var minX : float = 600;
var maxCamSize : float = 300;
var minCamSize : float = 200;
var clampX : float;
var camSize : float;

clampX = Mathf.Lerp (minX, maxX, Mathf.InverseLerp (minCamSize, maxCamSize, camSize) );

if clampX is used for the x coordidate of the camera position it just stays fxd at 768, besides, the camera needs to move between -600 and 600 at 300 size, and -768 and 768 at 200 size. There needs to be a way to find the correct clamp values for any size between 300 and 200....

That's probably because you never changed the value of camSize. Did you? The Lerp and InverseLerp approach will work, but GIGO. I didn't say what to do with the clampX variable after you get a value for it because I thought you already had that part figured out. But it goes something like this: if (Mathf.Abs (camT.position.x) > clampX) { camT.position.x = Mathf.Clamp (camT.position.x, (-1*clampX), clampX); }