Search Unity

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

Discussion in 'Cinemachine' started by VEWO, Apr 8, 2018.

  1. VEWO

    VEWO

    Joined:
    Sep 30, 2014
    Posts:
    8
    Hi!

    I'm fairly new to game development so please bare with me...I'm developing a 2D tilemap based game (using Unity's new tilemap features). When I "swap" maps I basically destroy the current map and load the new one. Each map has it's own bounding box (a polygon collider) to bound the virtual camera. I only have one virtual camera that follows the player.

    My problem is: when I swap maps, I can see that the polygon collider from the Cinemachine Confiner component updates correctly. However, the camera goes out of bounds and if I move the player to a certain point the scene turns blue and sometimes it follows the player and some other times it doesnt.

    The camera works well on the first map, this makes me believe that it has something to do with the player's spawning point but i'm not sure. In the attached picture you can see where the player spawns and the virtual camera with the correct collider.

    ***Additional Info**** If I set the starting map to the second map the second map works well but now the first map has the problem, so there's something wrong with the implementation and not the spawning point or colliders. Also if possible, I would like to avoid having one vcam per map to keep things manageable.

    Thanks! Screen Shot 2018-04-08 at 4.51.43 AM.png
     
    Last edited: Apr 8, 2018
  2. VEWO

    VEWO

    Joined:
    Sep 30, 2014
    Posts:
    8
    nvm, all I had to do was to destroy and instantiate the vcam.
     
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    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.
     
  4. treecki

    treecki

    Joined:
    Feb 6, 2017
    Posts:
    29
    I was having the same problem, thanks Gregoryl! Calling InvalidatePathCache() solved it perfectly.
     
  5. NINJATAC0

    NINJATAC0

    Joined:
    May 22, 2017
    Posts:
    10
    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,

    Code (CSharp):
    1. void LoadNewBoundingShape(PolygonCollider2D newBoundingShape) {
    2. cinemachineConfiner.m_BoundingShape2D = newBoundingShape;
    3. }
    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!
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    That sounds like the culprit: it's not being set. You need to debug that.
     
  7. NINJATAC0

    NINJATAC0

    Joined:
    May 22, 2017
    Posts:
    10
    @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
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    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.
     
  9. NINJATAC0

    NINJATAC0

    Joined:
    May 22, 2017
    Posts:
    10
    Alright, I think I got it working. Before I was finding my virtual camera, finding its CinemachineConfiner component and running this code:

    Code (CSharp):
    1. var virtualCamera = GameObject.FindGameObjectWithTag("VirtualCamera");
    2.  
    3. var confiner = virtualCamera.GetComponent<CinemachineConfiner>();
    4.  
    5. confiner.InvalidatePathCache();
    6. 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:
    Code (CSharp):
    1. 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
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    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.
     
  11. MuttlyMartin

    MuttlyMartin

    Joined:
    Apr 13, 2020
    Posts:
    2
    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.
     

    Attached Files:

  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    Are you calling confiner.InvalidatePathCache()?
     
  13. joycatdev

    joycatdev

    Joined:
    Aug 20, 2021
    Posts:
    6
    can i get the full code of the scripts, im facing the same probleme ! thanks in advance !
     
  14. Char_141689

    Char_141689

    Joined:
    Jan 31, 2022
    Posts:
    1
    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?)
     
  15. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    Try putting
    using Cinemachine;
    at the top of the file.
     
  16. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    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".

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