Layer Mask Issues When Not Using Default Layer in Unity 2021
Notice how the cockpit appears about halfway into this clip?
The cockpit should be shown at all times.
This is more what it should look like (rough first draft, fine-tuning yet to be done).
The point is, the cockpit should be visible the entire time.
So, why isn’t it?
You’re going to need a little background on what it is that I’m doing here.
Essentially, I have a Virtual Camera for the starship’s cockpit point-of-view that is being used during an intro cinematic sequence.
The player’s starship is flying into the scene, through a wormhole contained in a gate which plays out before the player is able to take control of the ship.
I also have a script that changes the Main Cameras culling mask.
For more details on setting up your own custom layer masks for virtual cameras, see my earlier article:
Using culling layer masks for Virtual Cameras in Unity 2021
In the scene shown above, we can see the result of switching from one Cinemachine virtual camera to another.
As you can see above, each culling mask has a specific use-case.
The follow mask will be used when the follow virtual camera is in use for a third person view of the ship as the player flies around.
The cockpit mask is generally used when the player is flying around but with a first-person point-of-view inside the ship.
The Intro Cinematic mask is only used during the first 3 seconds of this Timeline sequence.
Why do I even need a separate mask for the first 3 seconds of the cinematic?
In the future, I intend to add effects for the ship to start off as being mid “warp” and then coming out of warp into the scene as shown.
So, while in warp the virtual camera should not be able to see all of the asteroids, ships, stations, etc that are in my scene!
Initially the Intro culling mask is used, then a signal emitter on the Timeline calls a method in my script to set the culling mask back to the normal cockpit culling mask.
Now, my start() method sets the Cockpit VCam to be active and then sets the culling mask for introCinematicMask.
The only things I do with the VCams and Main camera in script is to set the culling mask on main camera or change the priorities of the VCams to switch between them.
Yet, when I ran the scene, the Main Camera’s CinemachineBrain would inexplicably lose the assigned virtual camera.
The key factor in solving the case was that enabling default layer on the Intro mask would make it work again.
The MainCamera is on the Default layer and all of the virtual cameras are on the default layer.
In effect, by culling the layers in which the virtual cameras themselves exist, I’ve culled the current camera view!
For some reason, this does not seem apply to the MainCamera, however.
It still renders, but it is missing a virtual camera assignment on its cinemachine brain.
Creating a new layer I’ve named “Camera” and setting it as the layer assignment of the virtual cameras solved the issue.
You see, if I used “Default” on the layer mask, well that shows everything.
It is literally the default layer…
You might have noticed that Camera layer in this gif earlier from the finished version of the culling masks.
I leave Default layer on for both Follow and Cockpit layer masks just to be clear, but I also have to add the Camera layer to those as well.
That’s it, this is the work-around that worked for me and I hope it works for you!
ADDENDA: Check out the finished (or nearly finished?) intro cinematic:
UPDATE: Post Processing effects added to intro sequence!