Cinemachine Confiner for 2D - to block at the borders of the map

To start with I am beginner game dev, and new to using Cinemachine. I am trying to restrict the camera exposing the edge of the screen. I know that i have to add a Cinemachine Confiner and to have a collider. But my map is generating procedural and so I will have to add the collider programmatically and also somehow tie the collider to the Cinemachine Confiner and I don’t know how, or couldn’t find the necessary documentation…

What I’ve done until now:

  • I have created a collider
  • I have created a Cinemachine.Cinemachine confiner
  • I have created a CM vcam1 in the hierarchy

How to tie them together?

Thanks in advance

1 Like

Your collider is a PolygonCollider2D, right?
If you’re using the TileMap, you can have it generate a CompositeCollider2D programmatically, and you can use that.
Once you have the collider, just populate the fields of the CinemachineConfiner component:

3305259--256484--upload_2017-11-29_15-14-7.png

From script, you can do it as follows:

using Cinemachine;
confiner.m_ConfineMode = CinemachineConfiner.Mode.Confine2D;
confiner.m_BoundingShape2D = myPolygonCollider;
confiner.m_ConfineScreenEdges = true;
1 Like

I forgot to ask you: how did you create the vcam and the CinemachineConfiner? Can you show me a picture of the inspector for your vcam?

It should look something like this:
3305262--256485--upload_2017-11-29_15-20-0.png

Thanks for the quick answer, I am now looking into ways of creating a polygon collider 2D, cause mine was a Box Collider 2D. The camera looks exactly like that.

1 Like

I made a mistake in my earlier post: the TileMap can generate a CompositeCollider2D, not a PolygonCollider2D. This generated shape can be used as the bounding shape.

Thanks a lot! If anyone is intrested I made it by creating a Composite Collider, enabling isTrigger, then adding my boxCollider2D as a child of the said Composite Collider, with a tick at “Used by Composite” and then editing the boxCollider2D size and offset from the script.

4 Likes