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

Can I use 2 camera with Cinemachine Brain?

Discussion in 'Cinemachine' started by fmacro, Jul 14, 2017.

  1. fmacro

    fmacro

    Joined:
    Mar 24, 2017
    Posts:
    4
    When I use cinemachine in two player game, I did not know how to use two camera with cinemachine brain.
    Do you have a way to use two camera ?
     
    Last edited: Jul 14, 2017
  2. JakubSmaga

    JakubSmaga

    Joined:
    Aug 5, 2015
    Posts:
    417
    Just create second camera and add it's own Cinemachine Brain.
     
  3. fmacro

    fmacro

    Joined:
    Mar 24, 2017
    Posts:
    4
    I'm sorry I didn't make it clear enough.
    I want to use two camera with differ virtual camera.
    I added second Cinemachine Brain, both camera followed same virtual camera.
     
    Last edited: Jul 15, 2017
    Marc-Saubion likes this.
  4. JakubSmaga

    JakubSmaga

    Joined:
    Aug 5, 2015
    Posts:
    417
    @Adam_Myhill Any way to set it up?
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    From the user docs:

    You can set up a multi-camera split-screen with Cinemachine 2.0:
    1. Make 2 Unity Cameras, let's call them Camera A and Camera B. Give each one its own CinemachineBrain, and set up their viewports.
    2. Now make 2 virtual cameras to follow the players. Assign those virtual cameras to different layers. We’ll call them layer A and layer B.
    3. Go back to the two Unity cameras, and set their culling masks so that the Camera A excludes layer B and the Camera B excludes layer A.
    4. That’s it! Camera A will be driven by virtual cameras on layer A, and camera B will be driven by virtual cameras on layer B. They will do their blending etc independently.
    5. Extend this idea to as many layers and cameras as you like.
     
    Last edited: Nov 29, 2017
  6. fmacro

    fmacro

    Joined:
    Mar 24, 2017
    Posts:
    4
    This problem was resolved thanks to your support!
    Thank you for your help!
     
    ModLunar likes this.
  7. username132323232

    username132323232

    Joined:
    Dec 9, 2014
    Posts:
    476
    Does this work for showing the same scene from different angles on a split-screen?
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
  9. username132323232

    username132323232

    Joined:
    Dec 9, 2014
    Posts:
    476
    Tried that, but the cameras only show whatever exists in their assigned layer. I must be missing something, but don't know what it is :)
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Make new layers for the vcams. Have your Cameras see everything except the other guy's vcam layer.
     
    zzxiang, joshnaro and Ramlock like this.
  11. username132323232

    username132323232

    Joined:
    Dec 9, 2014
    Posts:
    476
    Wow! Thanks. I can't believe how good Cinemachine is and I'm probably using less than 10% of what it can do.

    PS. Do you think
    "Go back to the two Unity cameras, and set their culling masks so that one camera sees layer A but not layer B... "

    should say

    "Go back to the two Unity cameras, and set their culling masks so that one camera excludes layer B ..."
     
    Ramlock, Chiefkat, ModLunar and 2 others like this.
  12. username132323232

    username132323232

    Joined:
    Dec 9, 2014
    Posts:
    476
  13. JakubSmaga

    JakubSmaga

    Joined:
    Aug 5, 2015
    Posts:
    417
  14. username132323232

    username132323232

    Joined:
    Dec 9, 2014
    Posts:
    476
  15. iwaldrop

    iwaldrop

    Joined:
    Sep 3, 2012
    Posts:
    9
    @Gregoryl Wow, that is unexpectedly easy. It sure would be great though if a single camera could be used on several layers. Perhaps moving the check to a layer mask field on either/both the virtual camera/brain would allow a single virtual camera to be used by brains on separate layers.

    Example:
    * vcams 1 and 2 where 1 applies to all layers, and where 2 applies to only one; vcam 2 is inactive
    * two unity cameras assigned to corresponding layers, each with a brain component attached
    * vcam 2 is activated and brain on layer 2 transitions to vcam 2 while brain on layer 1 remains unchanged

    As of now it seems the only way to achieve this effect is to duplicate one vcam between two layers, and reducing this duplication seems like it would facilitate a smoother experience when tweaking and maintaining camera angles.
     
    narengenet likes this.
  16. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    @iwaldrop I think you can get what you're looking for by using a third vcam layer:
    • Brain 1 sees vcam layers 1 and 3
    • Brain 2 sees vcam layers 2 and 3
    • vcam 1 is on layer 3, vcam 2 is on layer 2
    • Initially vcam 1 is active, both brains see it
    • When vcam 2 is activated, only Brain 2 will transition
     
  17. Cleverlie

    Cleverlie

    Joined:
    Dec 23, 2013
    Posts:
    219
    hi guys, hi @Gregoryl, I have a kind of similar setup to what is asked here, I have a "security room" where you have multiple screens, each one shows the renderTexture that different cameras have assigned, basically you can see multiple cameras rendering at the same time, which is similar to split screen rendering.

    I have two problems with the "set opposite cameras in different non intersecting layers" approach:

    the first one is the obvious, once you get more than 4-5 cameras managing the layers exclusions and inclusions starts to be a mess, cam A should exlcude B,C,D,E, but not exclude other important layers, cam B should exclude A,C,D... etc, you get what I mean.

    The second problem is that this approach is very limited to the small amount of layers Unity has, while we have less than 32 layers, some of them are already taken for physics exclusions, lighting, rendering, etc. so this is not really scalable.

    would it be too hard to have an option to switch from the current way the Brains compute their affecting virtualCams to an approach where you explicitly set a List<CinemachineVirtualCam> in the inspector or through code in each brain (note that I imagine this as an optional boolean that switches between the two algorithms)?.
     
    Marc-Saubion and PlayCreatively like this.
  18. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Hmmm... no immediate plans for this. However, I think there is a way with the current code.

    The CM brain has an API to override its vcam selection mechanism. This is what timeline uses. You can write a script that has its own vcam selection logic (possibly based on a list of vcams), and uses the brain's override API to drive it.
     
    Cleverlie likes this.
  19. Cleverlie

    Cleverlie

    Joined:
    Dec 23, 2013
    Posts:
    219
    thanks, I think thats a good starting point for me, is this API documented or is it internal? I had a look at the docs but I didn't found anything at first sight, maybe you can point me in the direction of this feature? thanks again
     
  20. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Yeah it's marked internal and not documented, although it does have XML doc in the sources.
    Will look at promoting it to public for next release.

    You need to check out CinemachineBrain.SetCameraOverride() and CinemachineBrain.ReleaseCameraOverride().
    For a usage example see CinemachineMixer.cs.

    If you go this route, you'll have to manage your own blending, the way timeline does.
     
  21. Cleverlie

    Cleverlie

    Joined:
    Dec 23, 2013
    Posts:
    219
    roger, will check that out, thanks!
     
  22. Remolino

    Remolino

    Joined:
    Oct 21, 2012
    Posts:
    5
    Thank you. Save my a lot of time
     
  23. Kolyasisan

    Kolyasisan

    Joined:
    Feb 2, 2015
    Posts:
    397
    Yep, minus the fact that this API is now internal. I do wonder what was the reasoning behind gating it off? Relying fully on layers to drive the camera behaviour is, IMO, a very flimsy tactic.
     

    Attached Files:

  24. m506

    m506

    Joined:
    Dec 21, 2015
    Posts:
    93
    Will this layer technique work for the same camera viewport? I have a FP arms that i want to prevent culling inside walls and enemies. To achieve this with regular cameras I just set one of them to skybox and the other one to clearflags, assigning my arms to a specific layer and it works great, but apparently cinemachine wont work the same way. Is this achievable somehow?
     
  25. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Cinemachine just pushes the camera around. Layering techniques will continue to work.
     
  26. shreyamathur651

    shreyamathur651

    Joined:
    Dec 13, 2019
    Posts:
    8
    Create Second Camera
     
  27. totsboy

    totsboy

    Joined:
    Jul 12, 2013
    Posts:
    253
    I tried this on Unity 2018.4 and it doesn't word.
    I'm using the LWRP, not sure if it affects this.
     
  28. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Should work. You're probably doing it wrong.
     
    gwelkind likes this.
  29. Adam_Myhill

    Adam_Myhill

    Joined:
    Dec 22, 2016
    Posts:
    342
    @totsboy This video might help, let us know how it goes

     
  30. totsboy

    totsboy

    Joined:
    Jul 12, 2013
    Posts:
    253
    Thank you for the helpful reply.

    I had already seen the video. I tried doing the same but it's not working, not sure what I am missing. I captured some screenshots of the setup, so maybe you can spot what's wrong. In my case, one of the cameras is supposed to render a rain FX, while the normal camera show gameplay.
     

    Attached Files:

    • SS1.jpg
      SS1.jpg
      File size:
      415.2 KB
      Views:
      608
    • SS2.jpg
      SS2.jpg
      File size:
      387.7 KB
      Views:
      609
    • SS3.jpg
      SS3.jpg
      File size:
      405.1 KB
      Views:
      610
    • SS4.jpg
      SS4.jpg
      File size:
      398 KB
      Views:
      588
  31. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Happy to be more helpful now that you've provided a little more info than "it doesn't work" ;)

    It looks as though your vcam layers and camera culling masks are set up correctly for CM to drive one camera with one set of vcams, and the other camera with the other set.
    Are the viewports of the cameras also set up correctly?
    If you disable the CM brains and position the cameras manually, do they render correctly? If not, then you probably have the rendering incorrectly set up, and should perhaps ask on the graphics forum.
     
  32. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Looking again at your setup, it strikes me that you don't need to use CM for your effects layer. Just use a single CM brain on the Camera that you want to drive with CM, and have a simple camera, with no vcams, rendering the effects. That camera never needs to move, so you don't need CM for it.
     
  33. totsboy

    totsboy

    Joined:
    Jul 12, 2013
    Posts:
    253
    That was the first thing I tried, but since it did not work I thought I had to use CM.
    I think it has something to do with the LWRP. I'll try the graphics forum, thanks for the help!
     
  34. Northmaen

    Northmaen

    Joined:
    Jun 23, 2015
    Posts:
    17
    Hey, I'm trying to use the new Input Manager with Cinemachine and i can't get it to work properly. In a Two player environment, the brain follows only the second Virtual Cam (Player 2). Since the entire rig (player and camera) are prefabs, instantiated by the Input Manager itself, I cannot assign different layers. How can I make those two systems works together?
     
  35. kimobm

    kimobm

    Joined:
    Dec 20, 2019
    Posts:
    26
    After several years, your answer still works perfectly !
    One issue : it doesn't work when i run the game on my android device. It shows only one Camera, the other portion of the screen is Black.
    But when i run it on Unity, it works perfectly.
    What am i missing ?
     
  36. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    It should work on Android too. Does it work if you remove Cinemachine? Can you make a non-cinemachine splitscreen on Android?
     
  37. kimobm

    kimobm

    Joined:
    Dec 20, 2019
    Posts:
    26
    Hello Gregoryl and thanks for your quick reply :)

    Just tried to make it without the use of Cinemachine, and the result is still the same : the left part of my splitted screen render all black ( like if there is no cam rendering there ) . But on Runtime in Unity, it works perfectly.

    For information, i use LWRP and my Unity version is 2018.4.14f1.

    This issue is going to made me mad..
     
  38. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
  39. kimobm

    kimobm

    Joined:
    Dec 20, 2019
    Posts:
    26
  40. kimobm

    kimobm

    Joined:
    Dec 20, 2019
    Posts:
    26
    Problem solved ! It was effectively LWRP problem.

    For some reason, it didn't allow me to display both camera. Downgraded to Standard, and no more problem :)
     
    Gregoryl likes this.
  41. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    837
    I'm also struggling with this, i have one camera that i instantiate and change the camera rect in order to have a splitscreen game. I dont want to have seperate layers, i dont understand why the virtual cam gets overwritten at all?

    I feel like if it just wouldnt do that my case is completely supported.
     
  42. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    I don't understand what you mean. Can you elaborate on the issue you're having?
     
  43. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    837
    upload_2020-6-26_15-52-36.png

    This is my multi-cam setup. The 'Main Camera' is present in the scene, when the game starts i instantiate it once more for the second player (if there is one). Then i correct the follow target with the spawned players, this is all set correctly.

    However one of the cinemachine brain's live camera property points to the wrong virtual camera. You say i have to use layers to solve this (which is not really what i want in my case, i just want it to not change ever and use the child), but that doesnt seem to work.

    The first camera's Live Camera points to the second virtual cam:
    upload_2020-6-26_15-55-8.png
    upload_2020-6-26_15-55-16.png
    Which is on another layer
     
  44. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    It's not a great idea to make vcams children of the cameras they control, because it creates a circular transform dependency (vcam follows target, then camera follows vcam which moves the vcam because of parent/child). You won't see problems in the case where vcam is 100% tracking something, but it's poor form. Better to have the camera and vcam siblings under a common parent, if you want to do the clone thing.

    The Brain does not consider parent/child relationships when choosing which vcam to follow. The algorithm is to use the highest-priority vcam which is on a layer not filtered out by the camera's culling mask. If you don't want the brain to consider a vcam, then put that vcam on a designated layer, and exclude that layer from the camera's culling mask. This should not impact your visuals, since vcams are not themselves rendered.

    Is there a specific reason why this strategy is not viable for you?
     
  45. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    837
    Mostly because i am migrating from my custom system, that has a solution that is exactly what i want. Is there any way to override this behaviour?
     
  46. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Overriding this would be significantly more work than just creating per-player layers, moving the vcams onto them, and adjusting the culling masks. Why would you want to do that?
     
    Last edited: Jun 26, 2020
  47. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    837
    Because i might have up to four players, and i dont like the idea of making layers and setting culling masks for things that arent visual. For me that is unintuitive, because culling mask determine what is visible. I do want all players to be visible, i just want the selection of virtual cameras to be different.

    I dont understand why it automatically selects, instead of having an serialized array, and an API to add and remove virtual camera's to a brain.
     
    gg_michael, ModLunar and stonstad like this.
  48. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,623
    Well, that's the way it is. Unless you want to write and maintain a custom version of CinemachineBrain, you'll have to make 4 dedicated vcam layers, even though the vcams are not themselves visible.
     
    TJHeuvel-net likes this.
  49. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    837
    I understand, thanks for the fast support!
     
  50. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    837
    Im trying to do just that, but sadly a lot of the CinemachineCore methods that are used are not accessable.