How to change layer on only other clients' objects.

Currently I am using a separate camera with a culling mask, to avoid gun clipping into the wall.

But when I build the game I see the other players gun through the wall.

My script looks like this:


public PhotonView PV;

public GameObject weapon;

private void Update()

  {

    if (!PV.IsMine)

    {

      weapon.layer = LayerMask.NameToLayer("Default");

    }

  }

I appreciate any replies.

Best Answer

  • Dibbie
    Dibbie ✭✭
    Answer ✓

    You probably dont need to constantly change the layer in Update and could do it only once when the object is created, either with IPunInstantiateMagic (https://doc.photonengine.com/en-us/pun/current/gameplay/instantiation#photonnetwork_instantiate) or Start/OnEnable - also if you plan to set the layer to "Default" specifically, you should be able to just set the layer to 0, if you need a specific layer, then using a layermask like that shouldnt have any issues, one other thing to keep in mind is that layers only apply to the object and none of the children, so if you have a weapon with child meshes/objects, then you will also need to apply the layer change to the child objects as well

Answers

  • Dibbie
    Dibbie ✭✭
    Answer ✓

    You probably dont need to constantly change the layer in Update and could do it only once when the object is created, either with IPunInstantiateMagic (https://doc.photonengine.com/en-us/pun/current/gameplay/instantiation#photonnetwork_instantiate) or Start/OnEnable - also if you plan to set the layer to "Default" specifically, you should be able to just set the layer to 0, if you need a specific layer, then using a layermask like that shouldnt have any issues, one other thing to keep in mind is that layers only apply to the object and none of the children, so if you have a weapon with child meshes/objects, then you will also need to apply the layer change to the child objects as well

  • Thank you @Dibbie for answering, this fixed it.