I have just started making my first multiplayer game. I am using Unity Networking (not Photon) and JavaScript.
I started adding guns to the player prefabs today but I noticed that. I can’t see other players guns and they can’t see mine. This is strange to me because I have a Santa Hat model on the player prefabs and I can see that.
I don’t understand why I can see their Santa Hat but not their gun…
(Also they can see my Santa Hat)
you probably have multiple weapons on your character, only active the main weapon, other are disabled, then use a function like EquipWeapon(int index) where index is you weapon List. you turn this EquipWeapon function into a RPC and it should work.
Here is my script that might help you understand the situation
The gun is a child to the players camera I found that when I take the gun out of the camera other players see it. But it needs to be in the camera for the gun to point the right direction.
//**********VARIABLES
private var gameName : String = "Lazys_FPS_Warfare";
private var customServerName : String;
private var hostData : HostData[];
var myPlayer;
var playerPrefabBLU : GameObject;
var playerPrefabGREEN : GameObject;
var spawnObjectBLU : Transform;
var spawnObjectGREEN : Transform;
private var status : String;
var statusStyle : GUIStyle;
private var chooseTeam : boolean;
var teamStyle : GUIStyle;
var labelStyle : GUIStyle;
private var isPlayerCreated : boolean;
private var paused : boolean;
private var startGame : boolean;
//**********FUNCTIONS
function Start() {
InvokeRepeating("clearStatus", 0, 30);
customServerName = "Default Server";
status = ("");
isPlayerCreated = false;
paused = false;
startGame = false;
chooseTeam = false;
}
function startServer(serverName) {
status = status + " > Creating Server...";
Network.InitializeServer(9, 25001, !Network.HavePublicAddress);
MasterServer.RegisterHost(gameName, serverName, "This is the description");
}
function refreshHostList() {
MasterServer.ClearHostList();
status = status + " > Refreshing Hosts...";
MasterServer.RequestHostList(gameName);
yield WaitForSeconds(2);
status = status + " > Found " + MasterServer.PollHostList().Length + " Servers";
hostData = MasterServer.PollHostList();
}
function spawnPlayer(team) {
status = status + " > Creating Player...";
if(team == "0") {
myPlayer = Network.Instantiate(playerPrefabBLU, spawnObjectBLU.position, spawnObjectBLU.rotation, 0);
myPlayer.GetComponent("FPSInputController").enabled = true;
myPlayer.GetComponent("MouseLook").enabled = true;
myPlayer.GetComponent("CharacterMotor").enabled = true;
myPlayer.transform.Find("Main Camera").gameObject.SetActive(true);
myPlayer.transform.Find("Main Camera").GetComponent("MouseLook").enabled = true;
}
else {
myPlayer = Network.Instantiate(playerPrefabGREEN, spawnObjectGREEN.position, spawnObjectGREEN.rotation, 0);
myPlayer.GetComponent("FPSInputController").enabled = true;
myPlayer.GetComponent("MouseLook").enabled = true;
myPlayer.GetComponent("CharacterMotor").enabled = true;
myPlayer.transform.Find("Main Camera").gameObject.SetActive(true);
myPlayer.transform.Find("Main Camera").GetComponent("MouseLook").enabled = true;
}
status = status + " > Player Created";
isPlayerCreated = true;
chooseTeam = false;
}
function Update() {
if(isPlayerCreated && Input.GetKeyDown(KeyCode.Escape)) {
paused = !paused;
myPlayer.GetComponent("MouseLook").enabled = !myPlayer.GetComponent("MouseLook").enabled;
myPlayer.transform.FindChild("Main Camera").GetComponent("MouseLook").enabled = !myPlayer.transform.FindChild("Main Camera").GetComponent("MouseLook").enabled;
myPlayer.GetComponent("FPSInputController").enabled = !myPlayer.GetComponent("FPSInputController").enabled;
}
if(!isPlayerCreated || paused){
Screen.showCursor = true;
} else {
Screen.showCursor = false;
Screen.lockCursor = true;
}
}
function clearStatus() {
status = "";
}
function OnServerInitialized() {
status = status + " > Server Created";
chooseTeam = true;
}
function OnDisconnectedFromServer(info : NetworkDisconnection) {
if (Network.isServer) {
status = status + " > Local server connection disconnected";
Destroy (myPlayer);
isPlayerCreated = false;
paused = false;
startGame = false;
}
else {
if (info == NetworkDisconnection.LostConnection) {
status = status + " > Lost connection to the server";
Destroy (myPlayer);
isPlayerCreated = false;
paused = false;
startGame = false;
}
else {
status = status + " > Successfully diconnected from the server";
Destroy (myPlayer);
isPlayerCreated = false;
paused = false;
startGame = false;
}
}
}
function OnConnectedToServer() {
status = status + " > Connected to Server";
chooseTeam = true;
}
function OnPlayerConnected(player: NetworkPlayer) {
status = status + " > Player has joined Server";
}
//**********GUI
var native_width : float = 1920;
var native_height : float = 1080;
function OnGUI () {
var rx : float = Screen.width / native_width;
var ry : float = Screen.height / native_height;
GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (rx, ry, 1));
GUI.Label(Rect(0, 0, 192, 192), status, statusStyle);
if(paused) {
GUI.Box(Rect(0,20,9999,9999), "");
GUI.Box(Rect(0,20,9999,9999), "");
if(GUI.Button(Rect(810, 30, 300, 30), "Disconnect")) {
Network.Disconnect();
Destroy (myPlayer);
paused = false;
}
}
if(!isPlayerCreated) {
GUI.Box(Rect(0,20,9999,9999), "");
GUI.Box(Rect(0,20,9999,9999), "");
}
if(!Network.isClient && !Network.isServer && !startGame) {
if(GUI.Button(Rect(10, 10, 150, 50), "Start Server")) {
startGame = true;
startServer(customServerName);
}
GUI.Label(Rect(10, 70, 80, 30), "Server Name: ", labelStyle);
customServerName = GUI.TextField(Rect(110, 70, 200, 20), customServerName, 15);
if(GUI.Button (Rect(10, 120, 150, 50), "Refresh Servers")) {
refreshHostList();
}
GUI.Label (Rect(10, 175, 300, 30), "Servers: ", labelStyle);
if(hostData) {
for(var i : int = 0; i<hostData.Length; i++) {
if(GUI.Button(Rect(810, 200, 300, 30), hostData[i].gameName)) {
Network.Connect(hostData[i]);
}
}
}
}
if(chooseTeam) {
paused = false;
GUI.Label(Rect(745, 30, 300, 30), "Choose Your Team!", teamStyle);
GUILayout.BeginHorizontal();
if(GUI.Button(Rect(745, 150, 150, 50), "BLU")) {
spawnPlayer("0");
}
if(GUI.Button(Rect(1058, 150, 150, 50), "GREEN")) {
spawnPlayer("1");
}
GUILayout.EndHorizontal();
}
}