I’m building a 2D game, where the entire play field is randomly generated. Thus, I do not know the dimensions and center-location of the playable area.
I’m using Cinemachine and a virtual camera to enable pan and zoom functionality.
Now, I need the camera to keep within the randomly generated playfield and I’m not entirely sure how to achieve that. Ideally, the camera would allow panning until the last pieces of the playable area are just about visible and then just stop panning at all, until the user changes panning direction.
I’ve tried using a Polygon Collider 2D as a camera boundary, but since the map might be very slim and tall, this boundary in some cases does not fit the view and thus spectacularly stops working at all.
My current approach is checking the “pan-drag” direction and distance on each frame, applying the transform to the cam, checking if any GameObject centers are in view and if not, restoring the previous camera position.
This, however, leads to some very nasty flickering, camera jumping back and forth all the time, camera getting “stuck” outside the playable area without being able to be moved back…
This is my current try, a script on the virtual camera object:
public class CamControl : MonoBehaviour {
[Header("Attributes")]
public float panMagnitudeThreshold = 1f; // this is used to allow clicks without detecting tiny movements
public int panBoundaryOvershoot = 2; // margin to allow the camera to move a bit outside the bounds, currently unused
private Vector3 touchStart; // cached position of last frame's touch position
private Camera camMain;
Vector3 viewPos; // position of object in viewport, used for visibility checking
Vector3 currentCamPos; // cached position of camera
private bool isCalculationPanning; // a flag, an attempt to stop the jumping and jittering behaviour
private void Start() {
camMain = Camera.main;
}
private void Update() {
// cache position of first touch location to calculate direction and amount
if (Input.GetMouseButtonDown(0)) touchStart = camMain.ScreenToWorldPoint(Input.mousePosition);
if (Input.GetMouseButton(0)) {
if (isCalculationPanning) return; // see flag above
Vector3 direction = touchStart - camMain.ScreenToWorldPoint(Input.mousePosition);
touchStart = camMain.ScreenToWorldPoint(Input.mousePosition); // cache new touch position
if (direction.magnitude < panMagnitudeThreshold) return; // see field comment
isCalculationPanning = true; // see flag above
currentCamPos = transform.position; // save current camera position in case it needs to be restored
transform.position += direction; // apply pan
// check if at least anything of the play field is still visible
bool anyVisible = false;
foreach (GameObject tile in MapManager.Manager.plotTiles) {
viewPos = camMain.WorldToViewportPoint(tile.transform.position);
if (viewPos.x >= 0 && viewPos.x <= 1 && viewPos.y >= 0 && viewPos.y <= 1) {
anyVisible = true;
break;
}
}
// if not, restore former camera position
if (!anyVisible) transform.position = currentCamPos;
isCalculationPanning = false; // lift flag
}
}
}

