Search Unity

Dynamically change Camera Culling Mask

Discussion in 'Scripting' started by eco_bach, Aug 31, 2019.

  1. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Anyone know of a way to dynamically change your Camera's culling mask so that it equals 2 layers? As usual the documentation is pretty sparse in this matter https://docs.unity3d.com/ScriptReference/Camera-cullingMask.html
    I've created 2 new layers, ActiveView and IdleView and assigned my Scene gameObjects accordingly. When I transition from IdleView to ActiveView I want my Camera to see BOTH layers and then when the transition is complete switch to have only a single layer in the culling mask. Possible?
     
    lopvet_rangers likes this.
  2. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    The layer indices are bitshifted when set to the culling mask. Try combining them with bitwise OR:
    Code (CSharp):
    1. int layer1 = LayerMask.NameToLayer("MyLayer1");
    2. int layer2 = LayerMask.NameToLayer("MyLayer2");
    3.  
    4. cam.cullingMask = (1 << layer1) | (1 << layer2);
     
  3. Pavel_D

    Pavel_D

    Joined:
    Nov 17, 2013
    Posts:
    14
    Code (CSharp):
    1. this.camera.cullingMask = LayerMask.GetMask("Default", "TransparentFX", "Ignore Raycast", "Water", "UI", "Rig");
    Or event simplier:
    Code (CSharp):
    1. [SerializeField]
    2. private LayerMask openMask;
    3. ...
    4. this.camera.cullingMask = openMask;
    And Some guys has bit of add and disable variant: https://answers.unity.com/questions/348974/edit-camera-culling-mask.html