[Fixed It] Equip secondary weapon

Updated on May 17, 2017 in [A] Multiplayer
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
6 on July 7, 2016

Hi bassed off the Multiplayer FPS tutorial. [Fixed It]

public class MyWeaponManager : NetworkBehaviour {

[SerializeField]
private string weaponLayerName = “Weapon”;

[SerializeField]
private Transform weaponHolder;
[SerializeField]
private GameObject weaponHolderG;

[SerializeField]
private MyPlayerWeapon primaryWeapon;

[SerializeField]
private Transform sideWepHolder;
[SerializeField]
private GameObject sideWepHolderG;

[SerializeField]
private MyPlayerWeapon sideWeapon;

private MyPlayerWeapon currentWeapon;
private MyWeaponGraphics currentGraphics;

public bool isMainInst;
public bool isSideInst;
public bool main;
public bool side;

void Start ()
{
EquipWeapon(primaryWeapon);
}

void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
if (main == true)
{
Debug.Log(“Main is all redy at”);
return;
}
else if (main == false)
{
Debug.Log(“EquipWeapon”);
EquipWeapon(primaryWeapon);
side = false;
}
}

if (Input.GetKeyDown(KeyCode.Alpha2))
{
if (side == true)
{
Debug.Log(“side is all redy at”);
return;
}
else if (side == false)
{
Debug.Log(“EquipSideWeapon”);
EquipSideWeapon(sideWeapon);
main = false;
}
}
}

public MyPlayerWeapon GetCurrentWeapon ()
{
return currentWeapon;
}

public MyWeaponGraphics GetCurrentGraphics()
{
return currentGraphics;
}

void EquipWeapon (MyPlayerWeapon _weapon)
{
if (isMainInst == true)
{

sideWepHolderG.SetActive(false);
weaponHolderG.SetActive(true);
side = false;
main = true;
}
else if (isMainInst == false)
{
currentWeapon = _weapon;
sideWepHolderG.SetActive(false);
weaponHolderG.SetActive(true);
isMainInst = true;
main = true;
side = false;

GameObject _weaponIns = (GameObject)Instantiate(_weapon.graphics, weaponHolder.position, weaponHolder.rotation);
_weaponIns.transform.SetParent(weaponHolder);

currentGraphics = _weaponIns.GetComponent<MyWeaponGraphics>();
if (currentGraphics == null)
Debug.LogError(“No WeaponGraphics component on the weapon object: ” + _weaponIns.name);

if (isLocalPlayer)
Util.SetLayerRecursively(_weaponIns, LayerMask.NameToLayer(weaponLayerName));
}
}

void EquipSideWeapon(MyPlayerWeapon _weaponS)
{
if (isSideInst == true)
{
sideWepHolderG.SetActive(true);
weaponHolderG.SetActive(false);
side = true;
main = false;
}
else if (isSideInst == false)
{
sideCurrentWeapon = _weaponS;

sideWepHolderG.SetActive(true);
weaponHolderG.SetActive(false);
isSideInst = true;
side = true;
main = false;

GameObject _weaponSIns = (GameObject)Instantiate(_weaponS.graphics, sideWepHolder.position, sideWepHolder.rotation);
_weaponSIns.transform.SetParent(sideWepHolder);

currentGraphics = _weaponSIns.GetComponent<MyWeaponGraphics>();
if (currentGraphics == null)
Debug.LogError(“No WeaponGraphics component on the weapon object: ” + _weaponSIns.name);

if (isLocalPlayer)
Util.SetLayerRecursively(_weaponSIns, LayerMask.NameToLayer(weaponLayerName));
}
}
}

  • Liked by
Reply
0 on April 19, 2017

how to set up it with PlayerShoot script ? it wont update new info about weapon like (fire rate, or damage ) . P.s thx for this cool weapon switch script.

  • Liked by
Reply
Cancel
0 on May 10, 2017

is this still working to you ?

  • Liked by
Reply
Cancel
1 on May 17, 2017
not qiute . it is not  updating weapon stats. using only default stats :'(.
I’m waiting for holyday to have some free time and try to fix it .

on May 17, 2017

Let me know if you succeed 🙂

Show more replies
  • Liked by
Reply
Cancel
1 on May 17, 2017

if sb had same problem and fixed it . I would a grateful for help .

on May 17, 2017

I can’t even get the weapon switching work on the network, i’ve tried yours and others scripts but didn’t work 🙁

Show more replies
  • Liked by
Reply
Cancel