Cinemachine Confiner for 2D

I’m using Cinemachine Confiner extension to prevent virtual camera from reaching out 2D boundary. But it seems not work as expected. Here is my step:

  1. create a plane with box collider as large as the scene
  2. drag the plane to Bounding Volume of Cinemachine Confiner

Please have a look. Thanks.

It’s working for me. I’m using CM 2.1 RC4.
What behaviour are you expecting, and what are you getting?

Note that even in a 2D scene, the box collider needs to be a 3D object, whose position is such that the camera will be inside it. So, if the camera is 10m in front of the game plane, then the box collider needs to include the plane where the camera is: either position it at z = -10, or have its Z size big enough to include the z=-10 plane.

Here is my vcam setting:

3255307--250770--upload_2017-10-16_9-34-25.png

And here is my collider plane:

3255307--250771--upload_2017-10-16_9-35-38.png

2 Likes

Worked! I guess the reason why the camera beyond the bound is I did not check “Confine to screen edges”…Is it be good if it was by default?

Yes, good idea.

Just found this. It was exactly what I was looking for. :slight_smile:

I just want to share my thoughts on the way the Confiner 2D works, or should works more specifically. I struggled for some time before I realized something : Each time I assign a new PolygonShape2D (for example) to the confiner, I had to call “InvalidatePathCache” method. To me it is not intuitive because the confiner let us know (the client of the API) of its internal “poutine” and it should not work that way. Of course, when I assign a new Shape to the confier, it should invalidate automatically its internal cache, that’s the contract I have with the confiner. Suggestion, instead of having a public member m_BoundingShape2D, give us a method or a property, in that case you could invalidate the cache automatically each time a user set a new BoundingShape… That my 2 cents… :slight_smile:

3 Likes

hey, cinimachine 2.5 does not have an check box option of confining screen edge, and I am having problems to confine the camera in a 2D polygon collier. Have any idea what may went wrong? I was following what this tutorial said and It did not work out. It is step 5 and 6.

Confine screen edges option only exists if camera is orthographic

2 Likes

Thanks for your 2 cents :)! We added a fix to our next release.

1 Like

Hi! Is this already fixed? I dont know if that is the problem I am having but everytime I set new bounds (at runtime) they dont work properly, But if I reload the scene with the new bounds set before hand they do work!
Edit: Yes it was exactly that. I just added the InvalidatePathCache() before changing the bounds and now it works. At least I only wasted like 4 hours on this!

Yes, this is fixed in 2.6

Something else to add to the list of fixes with Confiner2D: When editing a Polygon Collider 2D’s points while the editor is running the game, it will completely break the cinemachine camera. I say completely, but essentially when the screen edge hits the updated collider, it will–what it appears to do anyway–is immediately teleport(or move quickly with enough dampening) the camera to a location that is within the polygoncollider2d’s original boundaries before being changed. If I double the size of my polygon2d collider’s extents–this is a very significant unity space jump. Took me several hours to figure out this bug. Was honestly ready to throw cinemachine in the trash and just code my own solution, but lucked out and saw the above mentioned bug and tested it out myself in the editor’s game window while playing to see if it was a similar issue. It’s more than assigning a new collider–even changing the collider’s extents seems to cause mayhem.

@robcbryant For performance reasons, the confiner will not check every frame if the polygon points changed, and will just use cached values. If you change the polygon, you must call confiner.InvalidatePathCache().

1 Like

When I use orthographic camera, confinder works. But I need perspective camera. How fix it?

6732103--774982--Screenshot_26.png

You can use the new Confiner2D extension, which ships with CM 2.7.1. It can confine screen edges for perspective cameras also.

2 Likes

thanks, I’ll try

Hi, just a quick question, my project use 2020.3.7f1 version of unity and the last version available for cinemachine seems to be the 2.6.4 where i cannot find the new confiner 2d extension.

You need to enable preview packages.
Edit → Project Settings → Package Manager → Enable Preview packages or Enable Pre-release packages.

1 Like

Oh thanks, that solved everything :slight_smile:

Good morning!
Sorry for the distraction, but I need help.
I have not found any clues on either google or youtube.
I have several locations on the same stage.
And for each location, I need to make a CameraConfiner so that the camera does not go beyond the edges of the location.
I know how to do it in one location, but I don’t know how to do it in all locations.
And if you teleport, the player teleports, and the camera stays at the first location, because the confiner blocks any exit from the camera.
I have a 2D Top-Down game.