Clamping camera based on map and variable screen aspect ratio.

I’m working on a 2D Android-based game, using a orthographic camera. I know this question has been asked before, but I’m trying to find a specific solution.

My problem is factoring in the various aspect ratios available to Android into the clamp, which I have thus far not been successful at doing. I know it’s possible - games like Bad Piggies achieve it under Unity, with maps larger then the view port but keeping the camera clamped to an area within the map.

	leftOffset = 0 + (camera.pixelWidth / 2);
	rightOffset = 215;


	void LateUpdate()
	{
		Vector3 pos = transform.position;
		
		pos.x = Mathf.Clamp(pos.x, leftOffset, rightOffset);

		transform.position = pos;
	}

This is what I have so far… needless to say, whenever I factor in the screen-based offset and not just one number… it freaks out. What am I doing wrong? I’ve tried asking in Answers, and no one seemed to understand what I was attempting to do.

Small Update: Figured out PART of the problem. Clamping by width or pixelWidth produces too large of a value.
The stage holds an x-width of 0 - 215

Clamping a basic Landscape view of 800… produces 400, which forces the camera far out past the stage.

Solved it! This little gem works perfectly for a single x-axis clamp

	leftOffset = 0 + ((Screen.width/Mathf.abs(transform.position.z))*100)/215;
	rightOffset = 215 - ((Screen.width/Mathf.abs(transform.position.z))*100)/215;