2
\$\begingroup\$

I have found a few posts and blogs here and there. But all of the solutions they provide seem to be global.

Is there a way for let's say a multiplayer scenario where maybe an enemy has a cloaking device. Player 1 can't see said enemy but because Player 2 has thermo-goggles he can see the enemy just fine.

Is there an example of that scenario somewhere? I assume that it would have to be done at the camera level instead of the object instantiation. Maybe a state machine to designate these objects are invisible and one on the camera to state that you can see said invisible objects?

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

You can specify layers that a camera should and should not render: https://docs.unity3d.com/Manual/Layers.html. Anything that should be rendered by one camera but not the other should be assigned to a different layer. You can do this through code by changing the gameObject.layer. Then, in a camera's Inspector, deselect that layer from the "Culling Mask" drop-down to make that layer invisible to that camera.

If the goggles are temporary, when you want to activate the goggles, modify the player camera's culling mask from code. Each bit in the culling mask is a one if that layer should be visible. So if you are revealing the nth layer, toggle the nth bit by XORing it with a mask:

cam.cullingMask = cam.cullingMask ^ (1 << n);

When the goggles are removed, do the same XORing to toggle it again, making the layer invisible again.

\$\endgroup\$
7
  • \$\begingroup\$ So let me see if I have this right. Layer 1 is normal vision and P1 and P2 and E1 are all on that layer. E1 cloaks and gets moved to Layer 2 (invisibility) then P1 and P2 no longer see E1. P2 then dons his goggles allowing him to see Layer 2. If this was done with 3rd person view the equipping of the goggles could trigger the change in layer accessibility I assume? \$\endgroup\$
    – Slagmoth
    Commented Mar 22, 2018 at 17:05
  • \$\begingroup\$ I updated my answer to describe how to do the change from code, and yes. \$\endgroup\$
    – tyjkenn
    Commented Mar 22, 2018 at 17:24
  • \$\begingroup\$ Excellent, thank you. I wasn't sure on whether I should even begin if this was not a possibility. Pretty sure Unity supports everything else I want to do, but this was a clincher. Now just have to research a couple more things and hopefully get started. \$\endgroup\$
    – Slagmoth
    Commented Mar 22, 2018 at 17:29
  • \$\begingroup\$ Just realized my bit shifting was wrong. I'll fix it in a second. \$\endgroup\$
    – tyjkenn
    Commented Mar 22, 2018 at 17:33
  • 1
    \$\begingroup\$ Modifying an existing camera will be one line of code, as given above. With swapping out cameras, you'd have to disable one and enable another, and you'd have an extra camera in your scene. Also, if you end up having different types of goggles, it would be one camera per goggles per player, which could get messy. But if you are doing lots of other effects with the goggles, such as post-processing, it might be worth having the multiple cameras, each with their own configuration. \$\endgroup\$
    – tyjkenn
    Commented Mar 23, 2018 at 15:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .