Network restrictive rendering

Hi, now I have an additional problem. I would like to set up a player prefab with an animated playermesh and a weaponmesh. The prefab contains a camera that should render only the weapon but not the player's body. Now let's look what happens if someone plays the game:

  • He starts the game
  • He chooses "start server" in the GUI implemented by a pretty much standard script like "connect.js"
  • A player-prefab is network.instantiated on his machine
  • He can now walk, jump, shoot and interact with the scene
  • A second person starts the game and connects to this server
  • Now the camera of our first person should render the enemies body but not its own. Additionally it would be nice, if the camera of player 1 wouldn't render the weapon of player 2

The problem:

Can I change the layer of a GameObject during runtime ? If this is possible I could make a check for networkView.isMine and change the layer individually on each machine.

Is there something like camera.Ignore(GameObject) ? If there's something like this, how can I still render the children of the ignored GameObject ?

You can most definitely change the layer during runtime... From the scripting reference:

// Put the game object in the ignore raycast layer
gameObject.layer = 2;

If you want to continue with your way, you can setup the layers (for example) as

MyWeapon, MyPlayer, OtherWeapons, OtherPlayers.

Then, you can use Camera.cullingMask on your camera to decide what it renders, and what not. You can either edit your prefab of the player (change the camera's cullingMask in the inspector) Or you can have a variable in your spawn script:

public var localPlayerSees: LayerMask;

If it's public you can edit it in the inspector, just like you edit the cullingMask.

When you instantiate your local player, just assign:

remotePlayerCamera.cullingMask = localPlayerSees;

When you instantiate a remote player, move the relevant game objects on the instantiated player to the appropriate layers (OtherPlayers, OtherWeapons). The camera will do the rest.

Alternatively, you can also create two prefabs - one for a local player, one for a remote one.

BTW, just out of curiosity - why would you not want to display other player's weapons? (Unless the weapon is a heavy object and you want to replace it with some low poly object)