Search Unity

Feedback Add Confine Screen Edges To Perspective Cameras

Discussion in 'Cinemachine' started by Meatloaf4, Feb 21, 2020.

  1. Meatloaf4

    Meatloaf4

    Joined:
    Jul 30, 2013
    Posts:
    183
    It would be really helpful to have the ability to utilize
    Confine Screen Edges
    for a perspective camera.

    I had a look at this post https://forum.unity.com/threads/perspective-vcams-with-2d-confiners.508804/ and it detailed how it might be difficult to make this work in a general sense.

    I wen't ahead and did some brainstorming and thought up a way that I think would work fairly well and wanted to get some feedback / input if it might be possible.

    A
    Confine Mode
    could be added named
    Confine 2.5
    this mode would allow a
    BoxCollider2D
    to be added as the
    Bounding Volume
    . Essentially with this mode checked it would be equivalent to the
    Confine 2D
    with
    Confine Screen Edges
    checked but for a perspective camera.

    We could then take the position in world space that the
    BoxCollider2D
    is at and use that section as the point in our perspective camera to check for camera edge collisions.

    This is pretty hard to describe so I drew something to describe what i'm talking about and attached it below. Would love to get thoughts here.
     

    Attached Files:

  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Not sure how this is different from the approach mentioned in the quoted thread.
    Also not too sure about what's being illustrated in the picture.
    Maybe you can elaborate a little?
     
  3. Meatloaf4

    Meatloaf4

    Joined:
    Jul 30, 2013
    Posts:
    183
    Ah sorry I think I finally had a better understanding of what you meant in the original post.

    I went ahead and made some modifications to the Confiner script. I was wondering, would you be interested in me making these modifications a bit more full fledged and pull requesting?

    The way I have it working is essentially you utilize a BoxCollider2D along with Confine Screen Edges and a variable (float) for the perspective camera depth. I grab the orthographic size at the perspective camera depth specified in the variable and confine the camera edges at that depth to the box collider specified.
     
  4. Meatloaf4

    Meatloaf4

    Joined:
    Jul 30, 2013
    Posts:
    183
    Another quick note here is i'm having a hard time finding how i might grab the gate fit property from a cinemachine extension / cinemachine in general.

    Any advice as to how i might grab this?
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Yes, it's interesting.
    Maybe an even better approach would be to take the depth from the Z coord of the BoxCollider2D? Since it has a position in space maybe we can just see where the camera frustum intersects it. Just a thought.

    When "link fov to physical camera" is selected in the HDRP camera component, the Lens Settings in the vcam expands to include a bunch of physical parameters. Is that what you're looking for?
     
  6. Meatloaf4

    Meatloaf4

    Joined:
    Jul 30, 2013
    Posts:
    183
    I was thinking the same thinking about the BoxCollider2D z coord. However I was thinking that having it as an extra parameter would allow for a greater degree of control.

    Now that I think about it though it might make more sense from a usability stand point.

    As for the gate fit property ultimately I ended up grabbing it utilizing this bit
    CinemachineCore.Instance.FindPotentialTargetBrain(VirtualCamera).OutputCamera.gateFit;
     
    Gregoryl likes this.
  7. SuperPoopsy

    SuperPoopsy

    Joined:
    Jan 20, 2020
    Posts:
    17
    Hello @Gregoryl , I apologize for necroing this thread, but having an option to confine perspective cameras to screen edges would be a really useful feature to have in the core Cinemachine package. My current project is a 2D platformer metroidvania (ofc it is...) and I would like to have a nice 'natural' z-based parallax effect on my maps, without resorting to dodgy tricks and scripts that end up constantly moving the position of gameobjects to account for camera position. The ONLY reason why I ended up not implementing it, is because Cinemachine won't stop at the map screen edges like an orthographic camera would, which is unfortunate cause otherwise the parallax effect works beautifully (most importantly, is very 'controllable') and it's something that could 100% be managed by the map designer.

    Are there any plans (if any at all) to implement such feature? Cinemachine is such an amazing product in general, that It seems really odd having third party assets providing this feature out of the box, while Cinemachine still can't (or won't).

    Sorry to drag @Meatloaf4 in this, but did you have any success with your implementation?
     
    RemDust likes this.
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    @SuperTopen Thanks for the input. We are working on an improved version of the 2D confiner, and yes we plan to include this feature.
     
    SuperPoopsy likes this.
  9. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    432
    Hello there,
    Sorry for diggin out this old thread but I thought it would make sense to ask if there is any news about screen edges confinement for 2d perspective camera :)

    Also, if @Meatloaf4 worked some magic, it would be greatly appreciate if you can give us any insights !
    =)
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Yes, the new CinemachineConfiner2D will confine screen edges for both ortho and perspective cameras.
     
    RemDust likes this.
  11. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    432
    Good to hear Gregoryl ! Thank you as always for your quick reply ;)
    Just to be sure, do you mean it will in the next upgrade, or is it already out ?

    I'm running Cinemachine 2.7.1 on unity 2019.3.9, do I need to upgrade any of those ?
     
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    It's there now. CinemachineConfiner2D (not the same as CinemachineConfiner).