Search Unity

Resolved Cinemachine Confiner 2D conflicts with Lookahead

Discussion in 'Cinemachine' started by _eternal, Jul 19, 2021.

  1. _eternal

    _eternal

    Joined:
    Nov 25, 2014
    Posts:
    304
    Hey,

    I'm new to Cinemachine. Currently trying to integrate it into my existing project (2D platformer). I love the lookahead feature, but it seems to contradict the confiner at times.

    What I mean is, let's say you're falling quickly, and lookahead y is enabled because we want the player to see where they're falling. But, we also have a confiner surrounding the pit. When you land at the bottom of the pit, the confiner would adjust the camera so that only the nearest ground tile is visible (otherwise the whole bottom half of your screen would be ground).

    In this situation, the lookahead seems to overshoot the constraints, and then the constraints kick in to pull the camera back to normal.

    This probably shouldn't happen, right? I understand that the lookahead is predicting where you'll be in a few frames, but it seems to be making this prediction without accounting for the confiner.

    Also, I know that we can make the confiner kick in instantly by setting damping to 0, but that doesn't work in my situation. If we walk from one room to another, I don't want the camera to instantly jump to the new room; rather, I change the confiner to the new room, and I allow the camera to catch up at its own pace, based on the damping setting.

    Here's a visual example of what I mean :

    You can see how the camera reveals the secret passage at the bottom-left of the cave when constraint damping is greater than 0.
     
    gaborkb likes this.
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Hey,

    Thank you for the detailed bug report! I could reproduce the bug.

    To fix this bug, use CinemachineConfiner2D instead of CinemachineConfiner with 2D mode. CinemachineConfiner2D extension, in general, is an improved and faster version of CinemachineConfiner - mode: 2D. CinemachineConfiner2D is available from Cinemachine 2.7.2.
     
    Last edited: Jul 20, 2021
    Gregoryl and _eternal like this.
  3. _eternal

    _eternal

    Joined:
    Nov 25, 2014
    Posts:
    304
    Oh! Thank you, I didn't know about that. I was still on the latest Verified version (2.6.5).

    I just upgraded, and the issue mentioned in this thread is now fixed. But now I encountered a couple of issues with CinemachineConfiner2D that I wasn't having with the original CinemachineConfiner.

    The problem is related to camera bounds that are roughly the same size (or smaller) than the camera's frustum. The camera jumps straight to the center of the new bounds, even with damping set to > 0 and Oversize Window turned on.

    Strangely, the opposite seems to happen when I increase the size of the confiner. It jumps when transitioning from the smaller room to the bigger room, rather than the opposite.

    I tried recording this while frame stepping in scene view, so you can see the CM gizmos, if that helps.

    Small confiner:


    Bigger confiner:
     
  4. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    This is caused because the Bounding Shape 2D parameter is changed at runtime. When this parameter is changed, then the confiner needs to recalculate some internal states (you can see this happening in your recordings when the blue bar changes). Consequently, it won't have a previous state for damping, thus the jump. In addition, it can be costly to change the Bounding Shape 2D parameter at runtime.


    You can probably combine all your confining areas (probably Polygon Collider 2Ds) into one using Composite Collider 2D, and use this Composite Collider 2D as the Bounding Shape 2D parameter for your vcam's confiner. This will simplify your game, because you won't need trigger volumes (I guess) to change the Bounding Shape 2D parameter, because it stays the same.

    Note, don't forget to tick the "Used by Composite" flag on your Polygon Collider 2Ds, and that they need to be children of the gameObject that holds the Composite Collider 2D. Also, the Composite Collider 2D needs to have Geometry Type set to Polygons. Here are a couple of images to help you set it up: ;)
    Screen Shot 2021-07-20 at 6.06.04 PM.png Screen Shot 2021-07-20 at 6.06.35 PM.png Screen Shot 2021-07-20 at 6.07.05 PM.png


    If the above, doesn't work, then I propose a different approach:
    Create a vcam for each of these confiners. Instead of changing the Bounding Shape 2D parameter, switch between the vcams when the player enters these areas. In this case, the damping between the vcam switches will be controlled by the Blend settings (you can fine-tune this on CinemachineBrain, which can be found on the Main Camera).
     
    Last edited: Jul 20, 2021
    Gregoryl likes this.
  5. _eternal

    _eternal

    Joined:
    Nov 25, 2014
    Posts:
    304
    Hm, okay. I'll try a few things out.

    Is there a performance cost associated with having multiple vcams? This approach would require a new vcam for every single room in the game, so there might be 20+ in a single scene.
     
  6. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    No, because they will be disabled. You'll only have one or two enabled at the same time at most.
     
  7. gyx920820

    gyx920820

    Joined:
    Oct 8, 2015
    Posts:
    35
    In my case, Confiner 2d doesn't work. But Confiner, selecting 2d option, works.
    cinimachine version 2.7.4

    ---------------
    I figure out that in 2d, collider ranges view. in 3d confiner, collider ranges camera. NO bug
     
    Last edited: Jul 23, 2021
  8. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Could you describe your case? I'd like to understand in what case is Confiner 2D mode better than Confiner2D. :)