Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Cinemachine 2D confiner makes camera snap with polygon collider

Discussion in 'Cinemachine' started by Nalaria, Aug 3, 2020.

  1. Nalaria

    Nalaria

    Joined:
    Jan 25, 2018
    Posts:
    7
    I have been trying to use the Cinemachine confiner to set the boundaries of a level.

    For that I made a polygon collider that follows the shape of where I want the camera to be allowed to be.

    The problem is that at various points the camera snaps randomly and it seems like it's using a different part of the polygon.

    I have to use this complicated shape because I want to control how far the camera sees depending on the room etc, rather than having one giant rectangle around my entire level.

    upload_2020-8-3_21-54-33.png

    This is what my collider polygon looks like. The inside area is big enough for the camera, but the camera still snaps position strangely at various points.

    I have tried using a composite collider with multiple box colliders on one gameobject and giving that to the camera, but the behaviour it gives me seems broken.

    Is the confiner only supposed to work with rectangular boxes? If so why does it accept polygons?

    I also made a gif of what the camera does, you can see that the camera box snaps when it reaches some edges of the collider
     
    Last edited: Aug 3, 2020
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Hi,
    Could you send me the collider that you use? Created something similar :)
    We are working on a better confiner solution, and I would like to test that it works with this case too. In our new experimental confiner, we had no problems in your example. ;)
     
    Last edited: Aug 5, 2020
  3. DoctorShinobi

    DoctorShinobi

    Joined:
    Oct 5, 2012
    Posts:
    219
    @gaborkb Can you share any info on that experimental confiner? Like what does it solve, and estimated time it will be available for testing/production. I'm currently struggling with problems similar to OP and I'm desperate for a fix
     
  4. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    We're targeting the next CM release. It will have more features than the current confiner. One of them will solve the problem mentioned in this thread.
     
    DoctorShinobi likes this.
  5. DoctorShinobi

    DoctorShinobi

    Joined:
    Oct 5, 2012
    Posts:
    219
    Thank you! It's good to know
     
  6. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    2.7.2 (2.7.1+) should have the full version. @DoctorShinobi Though depending on your use case, the version in 2.7.1 may suffice.
    Look for Cinemachine Confiner 2D extension. You can add it to your vcam through the Add Extension drop down.
     
    Last edited: Nov 27, 2020
    DoctorShinobi likes this.
  7. DoctorShinobi

    DoctorShinobi

    Joined:
    Oct 5, 2012
    Posts:
    219
    Thanks, I'll check it out.
     
  8. johanolofsson

    johanolofsson

    Joined:
    Nov 21, 2018
    Posts:
    94
    I have more or less the same issue using 2.7.3 and CinemachineConfiner2D. I found this thread on reddit which shows the same issue.


    I pretty sure this is an issue given any convex polygon as bounding shape. For example imagine a level in the shape of an L where the width of the | part is as wide as the camera and the height of the _ part is as high as the camera. Now if I move from the end of the _ part to the corner the camera follows perfectly confined by the polygon. As I start to ascend the | part the camera is still stuck to bottom (unless I move enough to the left) as I move above the diagonal of the corner the camera instantly snaps to a new position where it is confined by the | part instead. The instant snapping is not pleasant. If I add damping it works better but now it also starts cutting the inner corner and displaying a part of the inner corner that is not inside the given polygon.

    I find no way of configuring this to make it even close to useful. Any suggestions?
     
  9. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Thanks for bringing this issue to our attention.

    What would be an ideal solution in this case for you? The problem, when we are transitioning through a concave corner, is that the confined camera path is not continuous, so the jump is expected. Damping solves this naively, but this naive solution can also show things outside the confined area.
     
    Last edited: May 17, 2021
  10. unity_hugstvLvFJrRjg

    unity_hugstvLvFJrRjg

    Joined:
    Jun 15, 2020
    Posts:
    1
    I just ran into this same problem. I found that if there's some way to "nudge" the camera towards the corner when it gets close (probably just that it moves faster in that direction when it's close) then it should solve the problem in theory. Obviously this would need to take place in a fairly large area near the corner to be gradual, probably something the dev sets themself. My only other idea is to have 2 confiners, one with no damping to hide the outside and one with high damping for smoothness, but that has its own problems.
     
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    You can make a NavMesh and attach the vcam to an invisible "cameraman" object that navigates on the navmesh to the desired position.
     
  12. SaharaBob

    SaharaBob

    Joined:
    Jul 30, 2018
    Posts:
    15
    But, speaking personally, a NavMesh is neither appropriate nor practical. It seems wrong to me that we should be expected to add this extra layer of complexity just to try to work round a shortcoming in another Unity component.

    I have three issues, two of which are mentioned above, namely:
    1. With damping == 0 the camera "snaps" into its new position when transitioning.
    2. With damping > 0 the camera reveals parts of the scene that I don't want revealed. I've set the bounds for the level and I expect the camera to respect them unless - as in the case of the bounding shape being smaller than the vcam's ortho size - it is impossible to do so. Irrespective of the damping value the camera should stop at the boundary without overshooting.
    3. (Not mentioned by the previous posters) When damping is > 0 and the sizes of the bounding shape and camera are similar it can cause the camera to "bob" in a way that I find nauseating. I appreciate that this is an issue that some may have and others won't.
    In my case I'm using Cinemachine 2.7.8 and I have a composite collider 2D comprising a number of overlapping (2D) box colliders. The composite is used as the bounding shape for the Cinemachine Confiner.

    The ideal solution for me in this case would be:
    1. When the vcam is at the edge of but wholly within the bounds of one of the constituent box colliders the camera does not overshoot the bounds even if damping is > 0.
    2. When the vcam spans two or more of the box colliders, is at the edge of at least one, but is not [yet] transitioning the camera does not overshoot the bounds even if damping is > 0.
    3. When the vcam is transitioning between box colliders it should apply the damping I've defined.
    (Note: I assume the vcam sees the composite collider rather than the individual box colliders, I'm referring to them just to make the problem easier to describe.)

    I accept that in cases (2) and (3), depending on the collider's shape, the camera may have no option but to display parts of the scene that are outside the boundary but it should nevertheless try to stay within as best it can.
     
  13. isbasher

    isbasher

    Joined:
    Mar 19, 2014
    Posts:
    8
    shouldn't the camera follow the line of the confiner up until it reaches the new closest point? instead of jumping to it...