Question by
DarkC0der · Dec 10, 2017 at 03:40 PM ·
unity 5unity multiplayer
Implementing weapon switch in Multiplayer FPS
Hello everyone, I've got a problem that I cannot sync weapon switching in multiplayer. I have Weapon Manager script which has all weapons list and current weapon and it is only switched on for local player for remote it is switched off. So I have to tell somehow to other clients to change weapon on their machine for my player. I can't really find solution cause Im very new to multiplayer un unity. Any help will be appreciated. Here is the code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using Weapons;
public class WeaponManager : NetworkBehaviour{
// Weapon layer name for local
public string localWeaponLayer = "Weapon";
// Weapon place
public Transform weaponHolder;
// Weapons player has
public List<Weapon> weapons = new List<Weapon>();
[SerializeField] private int startingWeaponIndex = 0;
private Weapon _equippedWeapon;
public Weapon equippedWeapon
{
get {
return _equippedWeapon;
}
}
void Start()
{
// Equip default weapon
EquipWeapon (startingWeaponIndex);
}
public void AddWeapon(Weapon weapon)
{
weapons.Add (weapon);
}
public void RemoveWeapon(Weapon weapon)
{
weapons.Remove (weapon);
}
public void RemoveWeapon(int index)
{
for (int i = 0; i < weapons.Count; i++)
{
if (i == index)
{
RemoveWeapon (weapons [i]);
}
}
}
public void EquipWeapon(int index)
{
if (weapons.Count <= 0)
return;
for (int i = 0; i < weapons.Count; i++)
{
if (i == index) {
if (weaponHolder == null)
return;
if (equippedWeapon != null)
Destroy (equippedWeapon.gameObject);
Weapon weaponInstance = Instantiate (weapons[i], weaponHolder) as Weapon;
weaponInstance.gameObject.transform.position = weaponHolder.position;
weaponInstance.gameObject.transform.rotation = weaponHolder.rotation;
_equippedWeapon = weaponInstance;
Utils.SetLayerRecursive (_equippedWeapon.gameObject, LayerMask.NameToLayer(localWeaponLayer));
}
}
}
}
Comment