- Home /
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 ?
Answer by Cyb3rManiak · May 08, 2011 at 02:14 PM
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)
I don't know how to turn my weapon object correctly. So I have two objects: The weapon from which shots are fired. It's parented to the camera and moves just like in any other fps. $$anonymous$$oreover I have the player oject. It's animated to walk and jump. However I don't know how I can correctly turn the player's hands in the direction the camera is facing. Therefore I render just the playermesh and not the weapon for other machines and the other way round on the local machine. To the player it looks like the shots are instantiated in mid-air but that's better than a weapon not connected to the player.
If you know how to turn the player's upper body up and down with a script, I would appreciate it very much, if you could help me. Up to know everybody is fighting unarmed persons that shoot from nowhere and can't look up and down.
I could start and explain exactly how to do it, but I think it's much more productive if you just change the "Head Look Controller" that Unity have on their resources section (http://unity3d.com/support/resources/unity-extensions/head-look-controller). It's meant to turn the head towards something interactively, but I think it can easily be changed to effect the torso if necessary :)
Your answer
Follow this Question
Related Questions
Camera rendering help. 1 Answer
How to apply target, while created at runtime? 1 Answer
camera layer issue, multiplayer. 1 Answer
Post processing with multiple camera 1 Answer
Camera with background hides game 1 Answer