[SOLVED] Cinemachine 2D when warping to a new tilemap (bounding box)

nvm, all I had to do was to destroy and instantiate the vcam.

Yes, that would work. But FYI to explain what was happening: the confiner caches the level boundaries for performance. When you changed levels, probably the old cache was still in effect. There is an API call in the confiner to invalidate the cache and force it to rebuild. Instead of destroying the vcam you could just call that.

I was having the same problem, thanks Gregoryl! Calling InvalidatePathCache() solved it perfectly.

I still don’t understand what the “answer” is. I want to set a new “Bounding Shape 2D” reference for my Cinemachine Confiner component. At the start of my scene, I already have one referenced (dragged and dropped from the editor). In my game, when level 1 is complete, I load level 2 which includes an object with a polygon 2D collider. At that point, this is my function,

void LoadNewBoundingShape(PolygonCollider2D newBoundingShape) {
cinemachineConfiner.m_BoundingShape2D = newBoundingShape;
}

I have tried calling “cinemachineConfiner.InvalidatePathCache()” both before and after this code. Though I see in my inspector that the reference for Bounding Shape 2D is missing - making me think it’s not being set at all.

What am I missing?

Also, I’d rather not load a brand new virtual camera every time I load a level. I would prefer to just change the bounding shape.

Thanks!

That sounds like the culprit: it’s not being set. You need to debug that.

@Gregoryl How do you recommend I debug that?

After I set “m_BoundingShape2D” to my PolygonCollider2D, I printed the value of “m_BoundingShape2D” to console to see if it took that value or was still null. In the console, the value of “m_BoundingShape2D” showed my collider, so it’s not null. However, in the inspector I see the “Bounding Shape 2D” field is still “Missing”

What more / else could I debug? Do you have any alternative suggestions on setting this value?

My alternative approach (which I don’t want to do quite yet…) is to have a virtual camera per level already preloaded with the bounding shape collider. That way - when I switch levels, I have that camera there, but I find it odd I cannot just set the bounding shape to a new collider

Are you sure that you’re setting it in the object that you think you are? Setting from script should to the same thing as setting it in the inspector.

Alright, I think I got it working. Before I was finding my virtual camera, finding its CinemachineConfiner component and running this code:

var virtualCamera = GameObject.FindGameObjectWithTag("VirtualCamera");

var confiner = virtualCamera.GetComponent<CinemachineConfiner>();

confiner.InvalidatePathCache();
confiner.m_BoundingShape2D = GameObject.FindGameObjectWithTag("MyNewBoundary").GetComponent<Collider2D>();

So yes - I’m sure I was working with the right object. The one thing I did not do was run this line of code:

confiner.m_BoundingShape2D = null;

The confiner would not be set to a new reference unless it was first null. That wasn’t obvious to me, so I was just trying to overwrite the existing reference. I set it to null after I ran the InvalidatePathCache() function and before I assigned the new reference/collider

hmmm… glad it’s working, but I’m not buying that explanation. Setting it to null before immediately setting it to something else will make no difference.

so i made this fix for my project on a 2d scroller but i have a issue still. it did fix the initial problem but when i change zones the Vcam still seems to miss place the bounding area(the polygon position stays the same). iv no idea why thius is happening. its not as bad from when Vewo and i had it.

6992447--825878--bounding.PNG

Are you calling confiner.InvalidatePathCache()?

can i get the full code of the scripts, im facing the same probleme ! thanks in advance !

Trying do the same thing, but I don’t know why got this error:
error CS0246: The type or namespace name ‘CinemachineConfiner’ could not be found (are you missing a using directive or an assembly reference?)

Try putting using Cinemachine; at the top of the file.

You’re going to have to give a little more context.

I assume that somewhere in your project some code is changing the shape of the bunding box used in a confiner extension on a vcam. That’s where you need to insert this code. In its current form, it will only work if you tag the vcam in question “VirtualCamera”.

8676630--1169490--upload_2022-12-20_17-7-34.png