Weapon Pickup in Multiplayer/Networking
I couldn't find a solution anywhere for this issue on Google or on these forums after searching so I am going to ask it here.
I am trying to make my game a melee fighter, where someone can pick up a weapon laying on the ground. So far it works for the local player picking up the weapon. On the local player camera it shows the weapon is attached to the character. However, all of the other players on the server cannot see the weapon equipped.
There is a WeaponManager as a child under the FPScamera on the player prefab, of course I have the camera disabled for all non-local players, and all the weapons are objects under the WeaponManager object. Is there anyway for me to tell all the other players that I am holding the weapon when I setActive the weapon object under WeaponManager?
[SerializeField]
public Camera FPSCamera;
[SerializeField]
public float interactRange = 3f;
private void Update () {
Ray ray = FPSCamera.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
RaycastHit hit;
if (Input.GetKeyDown("e")) {
if (Physics.Raycast (ray, out hit, interactRange)) {
if (hit.collider.tag == "Weapon") {
CmdPickupObject (hit.transform.gameObject);
}
}
}
}
[Command]
void CmdPickupObject (GameObject hit) {
GetComponent<Player_Network> ().RpcPickupWeapon (hit.name);
NetworkServer.Destroy (hit.transform.gameObject);
}
[ClientRpc]
public void RpcPickupWeapon (string hit) {
if (isLocalPlayer) {
Debug.Log ("Player equipped " + hit + " weapon.");
if (hit == "Sword") {
weaponManager.transform.FindChild ("Unarmed").gameObject.SetActive (false);
GameObject swordEquip = weaponManager.transform.FindChild (hit).gameObject;
_player.EquipWeapon (hit, swordEquip);
}
}
}
Any help would be greatly appreciated! Thank you for your time!