Quick answer: After changing the bounding shape, call confiner.InvalidateBoundingShapeCache(). The confiner caches the decomposed polygon for performance and won’t detect shape changes automatically.
A side-scroller transitions between rooms. Each room has its own PolygonCollider2D bounds. You swap the confiner’s m_BoundingShape2D reference when entering a new room. The camera position is wrong — it’s clamping to the old room’s shape, allowing visible black areas where the player walks past the new room’s edges.
Why the Cache Exists
Confiner2D’s clamp math is expensive: it decomposes the bounding polygon into convex sub-polygons, inflates them inward by the camera frustum extents, and stores the result. The decomposition is O(N²) where N is polygon vertex count — doing it every frame would burn CPU.
Instead, the confiner does it once and caches. The cache invalidates only if you tell it to.
The Fix
using Cinemachine;
[SerializeField] CinemachineConfiner2D confiner;
[SerializeField] PolygonCollider2D[] roomBounds;
public void EnterRoom(int index)
{
confiner.m_BoundingShape2D = roomBounds[index];
confiner.InvalidateBoundingShapeCache();
}
The InvalidateBoundingShapeCache call forces the confiner to re-decompose on the next LateUpdate. Next frame, the camera clamps correctly.
Updating an Existing Shape
If you modify the same collider’s vertices — e.g., procedurally regenerating bounds — the confiner still doesn’t notice. Same fix:
polyCollider.SetPath(0, newVertices);
confiner.InvalidateBoundingShapeCache();
Don’t skip the invalidation just because you didn’t swap references — the cache is keyed on a decomposition, not on the collider reference.
Multiple Camera Confiners
If your scene has multiple Cinemachine vcams each with their own confiner, invalidate all of them when bounds change:
foreach (var c in Object.FindObjectsByType<CinemachineConfiner2D>(FindObjectsSortMode.None))
{
c.InvalidateBoundingShapeCache();
}
Cheap call; even with several vcams the total cost is microseconds.
CompositeCollider2D vs PolygonCollider2D
For shapes built from tilemaps, use a CompositeCollider2D set to Geometry Type = Polygon. The confiner accepts either type. Composite colliders auto-update their geometry when the source tiles change — but the confiner’s cache still needs invalidation. Wire up tilemap-change events to call invalidate.
Verifying
Move the player to the edge of the new room. Without the fix, the camera stops at the old bounds (you can see the old shape outline if you enable Gizmos). With the fix, the camera goes right up to the new room’s actual edge. Visually inspect both extremes.
“Cinemachine caches expensive geometry for speed. Mutating bounds without invalidation gives you yesterday’s clamp.”
Wrap room-transition code in a single function that handles bounds swap + cache invalidate together — never one without the other.