Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Question Camera snaps to bounding edges with Confiner2D

Discussion in 'Cinemachine' started by mikeomni, May 3, 2022.

  1. mikeomni

    mikeomni

    Joined:
    Aug 23, 2015
    Posts:
    5
    We're using a Cinemachine Confiner set to "Confine 2D", using a poly collider2d as the bounding area, with Confine Screen Edges on and no damping in our 2d platformer.

    When the camera gets to screen edges it snaps harshly to the final position, and when you walk away from the map edge it snaps back off, both ways skipping about a world unit or so before handing to or from the camera's regular behavior. I cannot seem to pin down what causes this behavior, thought my only suspicion is that perhaps because we're using a framing transposer, the damping and lookahead and what-have-you when met with a hard collider snaps to the final position, skipping over everything it's procedurally doing in the framing transposer to offset the camera movement from the actual under-the-hood hard target, once the collider is encountered.

    I'd imagine that's something though that you guys would've considered, being as these are all Cinemachine components written to play well together and that it's more likely we're doing something wrong.
     
  2. mikeomni

    mikeomni

    Joined:
    Aug 23, 2015
    Posts:
    5
    @Gregoryl
    On further experimenting, we may have uncovered something you might want to know about if our theory on how this is working under the hood is accurate as our use-case may just be an edge case the confiner script doesn't account for the possibility of. I'd have to dig a lot more through your source to confirm, but perhaps it's best to just explain.

    In our game, we have a level editor which allows you to re-set the camera bounds at runtime. It's our theory that somewhere under the hood, you aren't really using the actual polycollider2d, but just the points array, caching them and working off those initial points. That would be an efficient thought, assuming there is no expectation of dynamically changing the collider at runtime, but we may've just found that edge-case for you. Whenever we edit the bounding collider's points positions [at runtime], the confiner breaks again, and seems to click to the original points when first loaded. If you [while in play mode] dupe the poly collider object and drop the dupe into the confiner's bounding shape 2d field, suddenly it's fixed again and working beautifully- until you edit the shape of the poly again.

    Now this probably isn't fantastic, as if we're fixing that off of dropping a new collider into the inspector at runtime, I imagine you're using in the CinemachineConfinerEditor script some kind of onValueChanged on the inspector field to update it, but I imagine in an actual build that would not work as there is no editor script to update the confiner's point array, which I imagine I'll have to dig into your source and see if there is even a way to that while built from code without the editor script.

    Is this how this is all working under the hood? I haven't had time to comb through the source yet to confirm, but it seems likely this must be working something like this for our accidental 'fix' to work and then rebreak on editing the poly points, the way it does.
     
  3. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    You need to call InvalidatePathCache() on CinemachineConfiner, when the bounding shape's points change at runtime.

    If you are using CinemachineConfiner2D, then call InvalidateCache().
     
    Gregoryl and mikeomni like this.
  4. mikeomni

    mikeomni

    Joined:
    Aug 23, 2015
    Posts:
    5
    @gaborkb
    Chef's Kiss, thank you very much.
    We've been constantly impressed with how well written Cinemachine has been. Keep smashing it out of the park boys.
     
    Gregoryl and gaborkb like this.