Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. We're excited to be bringing you the newest Unity features, news, demos, and we've even got some special guests from some of the industry’s most storied franchises. Set a reminder to tune in on YouTube here
    Dismiss Notice
  3. Unity 2018.3 is now released.
    Dismiss Notice

Perspective vcams with 2D confiners.

Discussion in 'Cinemachine' started by justin_short, Dec 14, 2017.

  1. justin_short

    justin_short

    Joined:
    Feb 18, 2013
    Posts:
    1
    I'd like to use cinemachine on my next project. It will be a side scroller with 3D level geometry. The problem is that when using a Virtual Camera that is set to perspective you can't use a Cinemachine Confiner that is set to Confine 2D mode. Is there a way to use 2D confinement with a perspective camera that I'm not seeing? If not, is there a more intuitive way of setting this up outside of creating box colliders for you vcams and messing with the z size and the camera offset?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,130
    I take it you'd like to use the "Confine Screen Edges" feature with a perspective camera? That's only implemented for ortho cameras at the moment. It's a little tricky to implement in a general way for perspective cameras, but if you make some assumptions about how your world is set up, you can probably modify the code to handle the perspective case (i.e. scale the camera bounding box based on the FOV and camera distance from 2D collider).
     
  3. sregnault

    sregnault

    Joined:
    Jan 15, 2018
    Posts:
    1
    You can try the plugin Pro camera 2D. It's compatible with perspective camera.
     
  4. Zillustrator

    Zillustrator

    Joined:
    Dec 17, 2017
    Posts:
    2
    Can you give an example of the algorithm for this please? For example, if the aspect ratio is 9:16 and the camera distance is 40 and the FOV is 40
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,130
    Height of the frame at depth d in scene units is h = d * 2f * Mathf.Tan(fov* Mathf.Deg2Rad / 2f);
    and width = aspect * h.
     
  6. Zillustrator

    Zillustrator

    Joined:
    Dec 17, 2017
    Posts:
    2
    Great thanks! Wish I'd seen this sooner!

    I did however find a reliable workaround that requires zero lines of code...

    1) Parent a perspective camera to the orthographic camera
    2) Tweak the values of the perspective camera so their views match
    3) Set the orthographic camera to render at a depth below the perspective camera so we only see the perspective view.
    4) Make cinemachine use the orthographic camera

    Note: The orthographic camera has to render something or cinemachine does not do anything to it. You can set the orthographic camera to only render a certain layer and just add a massive quad/sprite to the scene of that layer, then just set the orthographic camera not to render that layer if you are worried about performance.
     
  7. meebou

    meebou

    Joined:
    Mar 6, 2017
    Posts:
    3
    Hey, sounds interesting, but i do not really understand. I tried it and it did nothing unfortunately. So Orthographic is the parent and perspective is the child? The Cinemachine is attached to the parent orthographic? ... If i do so, the cinemachine is not moving anymore...Could you explain that again? :)