For some this code doesn’t work, and I can’t figure out why. I am working on switching weapons in a multiplayer fps game. Thanks!
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class WeaponManager : NetworkBehaviour
{
private string WeaponLayerName = "WeaponLayer";
public int[] equippedWeapons;
[SerializeField]
private Transform weaponHolder;
[SerializeField]
private PlayerWeapon primaryWeapon;
[SerializeField]
private PlayerWeapon secondaryWeapon;
[SerializeField]
private PlayerWeapon specialWeapon;
[SerializeField]
private PlayerWeapon MeleeWeapon;
[SyncVar(hook = "SetWeaponCallBack")]
private int currentWeaponIndex = 0;
private PlayerWeapon currentWeapon;
private WeaponGraphics currentGraphics;
public bool isSwitchingWeapon = true;
bool canSwitchAgain = true;
private PlayerWeapon weaponToLoad;
private bool firstEquip = true;
// Use this for initialization
void Start()
{
//EquipWeapon(equippedWeapons[0]);
isSwitchingWeapon = false;
}
public PlayerWeapon GetCurrentWeapon()
{
return currentWeapon;
}
public WeaponGraphics GetCurrentGraphics()
{
return currentGraphics;
}
public override void OnStartLocalPlayer()
{
gameObject.GetComponent<Player>().SetAnimationParameter("CurrentWeapon", 2, 0f, currentWeaponIndex, false, true);
if (isLocalPlayer)
{
Util.SetLayerRecursively(currentWeapon.graphics, LayerMask.NameToLayer(WeaponLayerName));
}
}
public override void OnStartServer()
{
base.OnStartServer();
firstEquip = true;
currentWeaponIndex = equippedWeapons[0];
}
public override void OnStartClient()
{
SetWeapon(currentWeaponIndex);
}
public void SetWeaponCallBack(int _weapon)
{
currentWeaponIndex = _weapon;
SetWeapon(currentWeaponIndex);
gameObject.GetComponent<Player>().SetAnimationParameter("CurrentWeapon", 2, 0f, currentWeaponIndex, false, true);
}
public void SetWeapon(int _weapon)
{
isSwitchingWeapon = true;
currentWeaponIndex = _weapon;
if (currentWeapon != null)
{
currentWeapon.graphics.SetActive(false);
}
switch (_weapon)
{
case 0:
// no weapon
currentWeapon = null;
break;
case 1:
// primary
currentWeapon = primaryWeapon;
break;
case 2:
// secondary
currentWeapon = secondaryWeapon;
break;
case 3:
// special
currentWeapon = specialWeapon;
break;
case 4:
// Melee
currentWeapon = MeleeWeapon;
break;
}
currentGraphics = currentWeapon.graphics.GetComponent<WeaponGraphics>();
if (isLocalPlayer)
{
Util.SetLayerRecursively(currentWeapon.graphics, LayerMask.NameToLayer(WeaponLayerName));
}
currentWeapon.graphics.SetActive(true);
gameObject.GetComponent<Player>().SetAnimationParameter("CurrentWeapon", 2, 0f, currentWeaponIndex, false, false);
gameObject.GetComponent<PlayerShoot>().fireRateTimer = 0;
gameObject.GetComponent<PlayerShoot>().aiming = false;
if (GetComponent<PlayerSetup>().playerUIInstance != null)
{
GetComponent<PlayerSetup>().playerUIInstance.GetComponent<PlayerUI>().crossHair.SetActive(true);
}
isSwitchingWeapon = false;
}
// Update is called once per frame
void Update()
{
if (!isLocalPlayer)
return;
if (Input.GetKeyDown("1"))
{
SetWeapon(equippedWeapons[0]);
}
if (Input.GetKeyDown("2"))
{
SetWeapon(equippedWeapons[1]);
}
if (Input.GetKeyDown("3"))
{
SetWeapon(equippedWeapons[2]);
}
if (Input.GetKeyDown("4"))
{
SetWeapon(equippedWeapons[3]);
}
}
public void loadAmmoStart()
{
for (int i = 0; i < equippedWeapons.Length; i++)
{
SetAmmo(i);
}
}
public void SetAmmo(int _weapon)
{
switch (_weapon)
{
case 0:
// no weapon
weaponToLoad = null;
break;
case 1:
// primary
weaponToLoad = primaryWeapon;
break;
case 2:
// secondary
weaponToLoad = secondaryWeapon;
break;
case 3:
// special
weaponToLoad = specialWeapon;
break;
case 4:
// Melee
weaponToLoad = MeleeWeapon;
break;
}
if (weaponToLoad != null)
weaponToLoad.currentAmmo = weaponToLoad.magazineSize;
}
}