Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. 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?
     
    tbriz likes this.
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    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).
     
    tbriz likes this.
  3. sregnault

    sregnault

    Joined:
    Jan 15, 2018
    Posts:
    6
    You can try the plugin Pro camera 2D. It's compatible with perspective camera.
     
    WildWolvesGames likes this.
  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:
    7,233
    Height of the frame at depth d in scene units is h = d * 2f * Mathf.Tan(fov* Mathf.Deg2Rad / 2f);
    and width = aspect * h.
     
    MrJeanJean and gaborkb like this.
  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:
    40
    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? :)
     
  8. hyagogow

    hyagogow

    Joined:
    Apr 25, 2014
    Posts:
    26
    If you still need this feature, I recently developed a package which transites the Cinemachine Camera between placed areas on your level. It works fine for a perspective camera as well for a 3D side scroller game.

    As a main feature, you can create/edit regions inside the Scene view:




    Check out the repo for more information.
     
    Dark_Seth likes this.
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    @hyagogow Thanks for posting this really cool idea! I like it.

    I had a quick look at CinemachineRegionsConfiner.cs and would like to suggest a couple of things:
    1. For best integration with CM, take out all references to Camera.main. Instead, work only with the virtual camera, and get the main camera from there using CinemachineCore.FindPotentialTargetBrain(). Why? because that way you can support split-screen, where there is more than one main camera.
    2. In PostPipelineStageCallback, you seem to be performing the update twice per frame: once in Stage.Finalize and once in CinemachineCore.Stage.Body. Do it only once (why not just in Body?).
    3. Why not use a CinemachineBlendDefinition for your transition? Then it's configurable by the user.
     
    hyagogow likes this.
  10. Berringer

    Berringer

    Joined:
    Aug 31, 2020
    Posts:
    3
    There is no such thing as ActionCode-Cinemachine Regions in the package manager, there's other stuff but not this thing.
     
  11. hyagogow

    hyagogow

    Joined:
    Apr 25, 2014
    Posts:
    26
    I re-upload the package. Try it now please.
     
  12. hyagogow

    hyagogow

    Joined:
    Apr 25, 2014
    Posts:
    26
    Thanks! :)

    I'll implement those improvements. Maybe we can add it to the official package?
     
  13. hyagogow

    hyagogow

    Joined:
    Apr 25, 2014
    Posts:
    26
    About your third suggestion, I'd like to use the default/custom blend from the CinemachineBrain component.
    Would be great to have a function which takes 2 positions and blend between them using this blend definition.

    Do you have any thoughts on how to do that?
     
  14. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    The CinemachineBlendDefinition defines a blend time, and a mapping curve to compute the blend weight as a function of time. You just need to read the blend weight (which will be between 0 and 1) and lerp the position using that weight.

    See the CinemachineBlend.BlendWeight function for an example of how to get the blend weight.
     
    hyagogow likes this.
  15. rubendariohh

    rubendariohh

    Joined:
    Oct 14, 2017
    Posts:
    32
    Hi, can you please further explain this approach? Thank You!
     
  16. rubendariohh

    rubendariohh

    Joined:
    Oct 14, 2017
    Posts:
    32
    What I did was to add a Regular Perspective Camera as child of the Orthographic Camera with the Cineachine Brain. Both Cameras are rendering all layers. Now I have a Perspective based Parallax with an Orthopgraphic based Confiner.
     
    DrVanillaCookie likes this.