- Home /
Using layers to show/hide different players
Hi. Most games are made so other people in a multiplayer game sees the whole person and you only see your own hands + gun, and the same goes for the other way around.
I need to Check if the NetworkView is mine and do this: Set other player's layer to "OtherPlayer" and show them. Set other player's weapon layer to "OtherWeapon" and hide them. Set own player's layer to "OwnPlayer" and hide that layer. Set own player's weapon layer to "OwnWeapon" and show it.
Can someone explain to me how to change the culling mask via code? because the script reference isn't that helping :/
Sorry if this thread was confusing, I wasn't sure on how to describe my question.
Thanks if you're able to help.
Answer by Tomer-Barkan · Oct 22, 2013 at 09:16 PM
I'd think most games would actually have the same model, and the difference would be the camera placement, if you place the camera in a certain location you would only see your arms/weapons while you would see the full body of other players.
Still, if you want to go this way, to set the culling mask of a camera programatically use the following command:
camera.cullingMask = LayerMask.NameToLayer("Layer1") | LayerMask.NameToLayer("Layer2");
where "Layer1" and "Layer2" are names of layers you want the camera to show (any number of layers can be BITWISE ORed). To set the layer of an object is the same, except you can only provide one:
gameObject.layer = LayerMask.NameToLayer("Layer1");
Answers.unity3d.com died yesterday so I posted a forum post about it ins$$anonymous$$d. now this site is back again.
I set the Culling mask on my camera to hide "OtherWeapon" and "OwnPlayer" and attached this script to my player prefab that get's spawned.
using UnityEngine;
using System.Collections;
/// <summary>
/// Just a test script. Not important.
/// </summary>
public class LayerChange : $$anonymous$$onoBehaviour {
private Transform myTransform;
private Transform cameraHeadTransform;
private Transform soldier$$anonymous$$odelTransform;
private Transform weaponTransform;
// Use this for initialization
void Start ()
{
myTransform = transform; // set myTransform to the transform of the scripts parent.
cameraHeadTransform = myTransform.FindChild("CameraHead"); // Find the cameraHead
soldier$$anonymous$$odelTransform = myTransform.FindChild("Soldier"); // Find the soldier $$anonymous$$odel
weaponTransform = cameraHeadTransform.FindChild("Weapons"); // Find the weapons under CameraHead
//if the script is running on my player, set my player's layer to OwnPlayer
//and my weapon to OwnWeapon
if(networkView.is$$anonymous$$ine == true)
{
soldier$$anonymous$$odelTransform.gameObject.layer = Layer$$anonymous$$ask.NameToLayer("OwnPlayer");
weaponTransform.gameObject.layer = Layer$$anonymous$$ask.NameToLayer("OwnWeapon");
}
//If the script isn't running on my player, set the player's layer to Other Player
//and the player's weapon's tag to OtherWeapon
if(networkView.is$$anonymous$$ine == false)
{
soldier$$anonymous$$odelTransform.gameObject.layer = Layer$$anonymous$$ask.NameToLayer("OtherPlayer");
weaponTransform.gameObject.layer = Layer$$anonymous$$ask.NameToLayer("OtherWeapon");
}
}
// Update is called once per frame
void Update () {
}
}
For some reason the player can't move and the GameObject get's called "BlueTeam(copy)" ins$$anonymous$$d of the name the player writes when they enter the server. Everything works fine if I remove the script except that everyone sees each others player and weapon+arms model.
The code seems fine to me (don't know what networkView.is$$anonymous$$ine does though). Are you getting any exceptions at all? If you disable this code, does it not call "BlueTeam(copy)"? the "(copy)" part is added when you instantiate a prefab programatically.
I got it working, Thanks for the help ;) If you attach a Network View to a GameObject, then you can check if the script is attached to your player(networkView.is$$anonymous$$ine == true) or if it's attached to another persons player.
so you can disable the script on other players if you want to.