# Stopping camera panning beyond object

I have a video here showing the issue: - YouTube

All I want to do is stop the user being able to pan the orthographic camera outside of the scene. Outside = black.

I’ve already asked about 10 questions and haven’t gotten any sensible responses, so I thought I’d make a new question. I’ve been trying to solve this for a whole week, working all day. It’s about time I got the camera sorted so I can actually start making the game. It’s getting really tedious.

You can attach colliders at the boundary of the screen and at the boundary of the dragging object to not allow camera to be moved outside that boundary.

Finally solved it. Was useful to project the screen size onto the scene.

I added a wall along the top and right edge, and used the relevant coordinate and the raycast hit point to calculate the distance.

``````private void calculateCameraBounds()
{
RaycastHit hit1;
RaycastHit hit2;
RaycastHit hit3;
RaycastHit hit4;

Ray ray1 = Camera.main.ViewportPointToRay(new Vector3(0, 0, 0));
Physics.Raycast(ray1, out hit1);
Debug.DrawRay(ray1.origin, ray1.direction * 5000, Color.red);

Ray ray2 = Camera.main.ViewportPointToRay(new Vector3(0, 1, 0));
Physics.Raycast(ray2, out hit2);
Debug.DrawRay(ray2.origin, ray2.direction * 5000, Color.green);

Ray ray3 = Camera.main.ViewportPointToRay(new Vector3(1, 0, 0));
Physics.Raycast(ray3, out hit3);
Debug.DrawRay(ray3.origin, ray3.direction * 5000, Color.yellow);

Ray ray4 = Camera.main.ViewportPointToRay(new Vector3(1, 1, 0));
Physics.Raycast(ray4, out hit4);
Debug.DrawRay(ray4.origin, ray4.direction * 5000, Color.blue);

Debug.DrawLine(hit1.point, hit2.point, Color.red);
Debug.DrawLine(hit2.point, hit4.point, Color.green);
Debug.DrawLine(hit3.point, hit1.point, Color.yellow);
Debug.DrawLine(hit4.point, hit3.point, Color.blue);

topBound = (topWall.transform.position.z - hit4.point.z) + cameraHolder.transform.position.z;
bottomBound = -topBound;
rightBound = (rightWall.transform.position.x - hit4.point.x) + cameraHolder.transform.position.x;
leftBound = -rightBound;

}
``````