- Home /
Multiplayer weapon switching not syncing
Hey there,
I’m trying to make a super simple multiplayer FPS, following along with a “Merry Fragmas 3.0” video posted on Unity’s site.
The feature I’m trying to implement now is weapon switching. Currently, the client can see the host switch weapons, but the host can’t see the client switching weapons. After implementing this feature I think I’m calling it quits on this, to return to some simpler projects.
Here’s a screenshot of the Switching Weapon script along with the inspector of the Player prefab. https://imgur.com/a/qMAdk
using UnityEngine;
using UnityEngine.Networking;
public class CurrentlyEquipedWeapon : NetworkBehaviour
{
[SerializeField] GameObject weaponHolder;
[SerializeField] GameObject [] Weapons;
public int selectedWeapon= 0;
public int weaponDamage;
public int weaponClipSize;
public float weaponReloadTime;
public float weaponFireRate;
public int weaponRange;
private void Start ()
{
selectedWeapon = 0;
localSwapWeapon ();
if (isServer)
{
RpcSwapWeapon();
}
else
{
CmdSwapWeapon();
}
}
#region
private void Update ()
{
if (!isLocalPlayer)
return;
if (Input.GetKeyDown(KeyCode.Alpha1))
{
localSwapWeapon();
if (isServer)
{
RpcSwapWeapon();
}
else
{
CmdSwapWeapon();
}
}
}
#endregion
void localSwapWeapon()
{
selectedWeapon++;
//Disable previous weapon
if (selectedWeapon > 0)
Weapons[selectedWeapon - 1].SetActive(false);
//If we are greater than all weapons in inventory, set it to 0
if (selectedWeapon >= Weapons.Length)
{
selectedWeapon = 0;
}
//Set current weapon active
Weapons[selectedWeapon].SetActive(true);
}
[Command]
void CmdSwapWeapon()
{
//Apply it to all other clients
localSwapWeapon();
RpcSwapWeapon();
}
[ClientRpc]
void RpcSwapWeapon()
{
if (isLocalPlayer)
return;
localSwapWeapon();
}
}
Cheers! Ryan
Your answer
Follow this Question
Related Questions
Photon Networking - What function(s) are called when a player (not me) is spawned? 2 Answers
How to call [Command] on Client in UNet 1 Answer
How to Sync Non-Players Actions in Multiplayer Games 1 Answer
Help| Sync' boolean variable (Friendly Fire) 0 Answers
RTS Multiplayer, Lock-step simulations and fixed game timings 1 Answer