[Fixed It] Equip secondary weapon

Updated on July 13, 2017 in [A] Multiplayer
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
10 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
1 on June 30, 2017
using UnityEngine;
using UnityEngine.Networking;
[RequireComponent(typeof(weaponManinger))]
public class PlayerShoot1 : NetworkBehaviour
{
    private const string PLAYER_TAG = “Player”;
    // [SerializeField]
    // private Transform Spell_cast;
    // [SerializeField]
    //  private GameObject FireBall;
    [SerializeField]
    private Camera cam;
    [SerializeField]
    private LayerMask mask;
    private PlayerWeapon1 sideWeapon;
    private PlayerWeapon currentWeapon;
    private weaponManinger weaponManager;
    public bool side_Wep = false;
    public bool Main_Wep = true;
    void Start()
    {
        if (cam == null)
        {
            Debug.LogError(“PlayerShoot: No camera referenced!”);
            this.enabled = false;
        }
        weaponManager = GetComponent<weaponManinger>();
    }
    void Update()
    {
        currentWeapon = weaponManager.GetCurrentWeapon();
        sideWeapon = weaponManager.GetSideWeapon();
        side_Wep = weaponManager.side;
        Main_Wep = weaponManager.main;
        if (PauseGame.IsOn)
            return;
        // if (Input.GetButtonDown(“Spell_1”))
        //  {
        //     fireball_lanch();
        //   }
        if (side_Wep == true)
        {
            if (sideWeapon.bullets1 < sideWeapon.maxBullets1)
            {
                if (Input.GetButtonDown(“Reload”))
                {
                    weaponManager.Realoading();
                    return;
                }
            }
            if (sideWeapon.fireRate1 <= 0f)
            {
                if (Input.GetButtonDown(“Fire1”))
                {
                    ClientShootSide();
                }
            }
            else
            {
                if (Input.GetButtonDown(“Fire1”))
                {
                    InvokeRepeating(“Shoot”, 0f, 1f / sideWeapon.fireRate1);
                }
                else if (Input.GetButtonUp(“Fire1”))
                {
                    CancelInvoke(“Shoot”);
                }
            }
        }
        if (Main_Wep == true)
        {
            if (currentWeapon.bullets < currentWeapon.maxBullets)
            {
                if (Input.GetButtonDown(“Reload”))
                {
                    weaponManager.Realoading();
                    return;
                }
            }
            if (currentWeapon.fireRate <= 0f)
            {
                if (Input.GetButtonDown(“Fire1”))
                {
                    ClientShoot();
                }
            }
            else
            {
                if (Input.GetButtonDown(“Fire1”))
                {
                    InvokeRepeating(“Shoot”, 0f, 1f / currentWeapon.fireRate);
                }
                else if (Input.GetButtonUp(“Fire1”))
                {
                    CancelInvoke(“Shoot”);
                }
            }
        }
    }
  
    //Is called on the server when a player shoots
    [Command]
    void CmdOnShoot()
    {
        RpcDoShootEffect();
    }
    //Is called on all clients when we need to to
    // a shoot effect
    [ClientRpc]
    void RpcDoShootEffect()
    {
        weaponManager.GetCurrentGraphics().muzzleFlash.Play();
    }
    //Is called on the server when we hit something
    //Takes in the hit point and the normal of the surface
    [Command]
    void CmdOnHit(Vector3 _pos, Vector3 _normal)
    {
        RpcDoHitEffect(_pos, _normal);
    }
    //Is called on all clients
    //Here we can spawn in cool effects
    [ClientRpc]
    void RpcDoHitEffect(Vector3 _pos, Vector3 _normal)
    {
        GameObject _hitEffect = (GameObject)Instantiate(weaponManager.GetCurrentGraphics().HitEffectPrefab, _pos, Quaternion.LookRotation(_normal));
        Destroy(_hitEffect, 2f);
    }
 
  //  void fireball_lanch()
   // {
     //   if (!isLocalPlayer)
     //   {
     //       return;
    //    }
    //  Instantiate(FireBall, Spell_cast.position, Spell_cast.rotation);
    //}
  [Client]
   void ClientShootSide()
    {
       
        if (!isLocalPlayer || weaponManager.isReloading )
        {
            return;
        }
        if (sideWeapon.bullets1 <= 0)
        {
            weaponManager.Realoading();
            return;
        }
        sideWeapon.bullets1–;
        Debug.Log(“Remaining bullets: ” + sideWeapon.bullets1);
        //We are shooting, call the OnShoot method on the server
        CmdOnShoot();
        RaycastHit _hit;
        if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, sideWeapon.range1, mask))
        {
            if (_hit.collider.tag == PLAYER_TAG)
            {
                Debug.Log(“shoot” + _hit.collider.name);
                CmdPlayerShot(_hit.collider.name, sideWeapon.damage1, transform.name);
            }
            // We hit something, call the OnHit method on the server
            CmdOnHit(_hit.point, _hit.normal);
        }
        if (currentWeapon.bullets <= 0)
        {
            weaponManager.Realoading();
        }
   
}
    [Client]
    void ClientShoot()
    {
        if (!isLocalPlayer || weaponManager.isReloading)
        {
            return;
        }
        if (currentWeapon.bullets <= 0)
        {
            weaponManager.Realoading();
            return;
        }
        currentWeapon.bullets–;
        Debug.Log(“Remaining bullets: ” + currentWeapon.bullets);
        //We are shooting, call the OnShoot method on the server
        CmdOnShoot();
       
        RaycastHit _hit;
        if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, currentWeapon.range, mask))
        {
            if (_hit.collider.tag == PLAYER_TAG)
            {
                Debug.Log(“shoot” + _hit.collider.name);
                CmdPlayerShot(_hit.collider.name, currentWeapon.damage, transform.name);
            }
            // We hit something, call the OnHit method on the server
            CmdOnHit(_hit.point, _hit.normal);
        }
        if (currentWeapon.bullets <= 0)
        {
            weaponManager.Realoading();
        }
    }
    [Command]
    void CmdPlayerShot(string _playerID, int _damage, string _sourceID)
    {
        Debug.Log(_playerID + ” has been shot.”);
        Player _player = GameManiger.GetPlayer(_playerID);
        _player.RpcTakeDamage(_damage, _sourceID);
    }
}

using UnityEngine;
using UnityEngine.Networking;
[RequireComponent(typeof(weaponManinger))]
public class PlayerShoot1 : NetworkBehaviour
{
    private const string PLAYER_TAG = “Player”;
    // [SerializeField]
    // private Transform Spell_cast;
    // [SerializeField]
    //  private GameObject FireBall;
    [SerializeField]
    private Camera cam;
    [SerializeField]
    private LayerMask mask;
    private PlayerWeapon1 sideWeapon;
    private PlayerWeapon currentWeapon;
    private weaponManinger weaponManager;
    public bool side_Wep = false;
    public bool Main_Wep = true;
    void Start()
    {
        if (cam == null)
        {
            Debug.LogError(“PlayerShoot: No camera referenced!”);
            this.enabled = false;
        }
        weaponManager = GetComponent<weaponManinger>();
    }
    void Update()
    {
        currentWeapon = weaponManager.GetCurrentWeapon();
        sideWeapon = weaponManager.GetSideWeapon();
        side_Wep = weaponManager.side;
        Main_Wep = weaponManager.main;
        if (PauseGame.IsOn)
            return;
        // if (Input.GetButtonDown(“Spell_1”))
        //  {
        //     fireball_lanch();
        //   }
        if (side_Wep == true)
        {
            if (sideWeapon.bullets1 < sideWeapon.maxBullets1)
            {
                if (Input.GetButtonDown(“Reload”))
                {
                    weaponManager.Realoading();
                    return;
                }
            }
            if (sideWeapon.fireRate1 <= 0f)
            {
                if (Input.GetButtonDown(“Fire1”))
                {
                    ClientShootSide();
                }
            }
            else
            {
                if (Input.GetButtonDown(“Fire1”))
                {
                    InvokeRepeating(“Shoot”, 0f, 1f / sideWeapon.fireRate1);
                }
                else if (Input.GetButtonUp(“Fire1”))
                {
                    CancelInvoke(“Shoot”);
                }
            }
        }
        if (Main_Wep == true)
        {
            if (currentWeapon.bullets < currentWeapon.maxBullets)
            {
                if (Input.GetButtonDown(“Reload”))
                {
                    weaponManager.Realoading();
                    return;
                }
            }
            if (currentWeapon.fireRate <= 0f)
            {
                if (Input.GetButtonDown(“Fire1”))
                {
                    ClientShoot();
                }
            }
            else
            {
                if (Input.GetButtonDown(“Fire1”))
                {
                    InvokeRepeating(“Shoot”, 0f, 1f / currentWeapon.fireRate);
                }
                else if (Input.GetButtonUp(“Fire1”))
                {
                    CancelInvoke(“Shoot”);
                }
            }
        }
    }

    //Is called on the server when a player shoots
    [Command]
    void CmdOnShoot()
    {
        RpcDoShootEffect();
    }

    //Is called on all clients when we need to to
    // a shoot effect
    [ClientRpc]
    void RpcDoShootEffect()
    {
        weaponManager.GetCurrentGraphics().muzzleFlash.Play();
    }
    //Is called on the server when we hit something
    //Takes in the hit point and the normal of the surface
    [Command]
    void CmdOnHit(Vector3 _pos, Vector3 _normal)
    {
        RpcDoHitEffect(_pos, _normal);
    }
    //Is called on all clients
    //Here we can spawn in cool effects
    [ClientRpc]
    void RpcDoHitEffect(Vector3 _pos, Vector3 _normal)
    {
        GameObject _hitEffect = (GameObject)Instantiate(weaponManager.GetCurrentGraphics().HitEffectPrefab, _pos, Quaternion.LookRotation(_normal));
        Destroy(_hitEffect, 2f);
    }

  //  void fireball_lanch()
   // {
     //   if (!isLocalPlayer)
     //   {
     //       return;
    //    }
    //  Instantiate(FireBall, Spell_cast.position, Spell_cast.rotation);

    //}
  [Client]
   void ClientShootSide()
    {

        if (!isLocalPlayer || weaponManager.isReloading )
        {
            return;
        }

        if (sideWeapon.bullets1 <= 0)
        {
            weaponManager.Realoading();
            return;
        }
        sideWeapon.bullets1–;
        Debug.Log(“Remaining bullets: ” + sideWeapon.bullets1);
        //We are shooting, call the OnShoot method on the server
        CmdOnShoot();
        RaycastHit _hit;
        if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, sideWeapon.range1, mask))
        {
            if (_hit.collider.tag == PLAYER_TAG)
            {
                Debug.Log(“shoot” + _hit.collider.name);
                CmdPlayerShot(_hit.collider.name, sideWeapon.damage1, transform.name);
            }
            // We hit something, call the OnHit method on the server
            CmdOnHit(_hit.point, _hit.normal);
        }
        if (currentWeapon.bullets <= 0)
        {
            weaponManager.Realoading();
        }
}
    [Client]
    void ClientShoot()
    {
        if (!isLocalPlayer || weaponManager.isReloading)
        {
            return;
        }
        if (currentWeapon.bullets <= 0)
        {
            weaponManager.Realoading();
            return;
        }
        currentWeapon.bullets–;
        Debug.Log(“Remaining bullets: ” + currentWeapon.bullets);
        //We are shooting, call the OnShoot method on the server
        CmdOnShoot();

        RaycastHit _hit;
        if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, currentWeapon.range, mask))
        {
            if (_hit.collider.tag == PLAYER_TAG)
            {
                Debug.Log(“shoot” + _hit.collider.name);
                CmdPlayerShot(_hit.collider.name, currentWeapon.damage, transform.name);
            }

            // We hit something, call the OnHit method on the server
            CmdOnHit(_hit.point, _hit.normal);
        }
        if (currentWeapon.bullets <= 0)
        {
            weaponManager.Realoading();
        }
    }
    [Command]
    void CmdPlayerShot(string _playerID, int _damage, string _sourceID)
    {
        Debug.Log(_playerID + ” has been shot.”);
        Player _player = GameManiger.GetPlayer(_playerID);
        _player.RpcTakeDamage(_damage, _sourceID);
    }
}
—————————————–
And weaponManiger
————————————
 
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class weaponManinger : NetworkBehaviour {
    public bool isReloading = false;
    [SerializeField]
    private string weaponLayerName = “Weapon”;
[SerializeField]
    private Transform weaponHolder;
    [SerializeField]
    private GameObject weaponHolderG;
    [SerializeField]
    private PlayerWeapon primaryWeapon;
    [SerializeField]
    private Transform sideWepHolder;
    [SerializeField]
    private GameObject sideWepHolderG;
    [SerializeField]
    private PlayerWeapon1 sideWeapon;
    private PlayerWeapon currentWeapon;
    private PlayerWeapon1 sideCurrentWeapon;
    private weaponGFX currentGraphics;
    public bool isMainInst;
    public bool isSideInst;
    public bool main;
    public bool side;
    void Start()
    {
        EquipWeapon(primaryWeapon);
    }
    void Update()
    {
        if (isLocalPlayer) {
            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 PlayerWeapon GetCurrentWeapon()
    {
        return currentWeapon;
    }
    public PlayerWeapon1 GetSideWeapon()
    {
        return sideCurrentWeapon;
    }
    public weaponGFX GetCurrentGraphics()
    {
        return currentGraphics;
    }
    void EquipWeapon(PlayerWeapon _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.GFX, weaponHolder.position, weaponHolder.rotation);
            _weaponIns.transform.SetParent(weaponHolder);
            currentGraphics = _weaponIns.GetComponent<weaponGFX>();
            if (currentGraphics == null)
                Debug.LogError(“No WeaponGraphics component on the weapon object: ” +_weaponIns.name);
            if (isLocalPlayer)
                UTIL.SetLayerRecursively(_weaponIns, LayerMask.NameToLayer(weaponLayerName));
        }
    }
    void EquipSideWeapon(PlayerWeapon1 _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.GFX, sideWepHolder.position, sideWepHolder.rotation);
            _weaponSIns.transform.SetParent(sideWepHolder);
            currentGraphics = _weaponSIns.GetComponent<weaponGFX>();
            if (currentGraphics == null)
                Debug.LogError(“No WeaponGraphics component on the weapon object: ” + _weaponSIns.name);
            if (isLocalPlayer)
                UTIL.SetLayerRecursively(_weaponSIns, LayerMask.NameToLayer(weaponLayerName));
        }
    }
public void Realoading()
    {
        if (isReloading)
            return;
        StartCoroutine(Reload_Coroutine());
    }
    private IEnumerator Reload_Coroutine()
    {
        isReloading = true;
        CmdOnReload();
        yield return new WaitForSeconds(currentWeapon.reloadTime);
        currentWeapon.bullets = currentWeapon.maxBullets;
        isReloading = false;
    }
    [Command]
    void CmdOnReload()
    {
        RpcOnReload();
    }
    [ClientRpc]
    void RpcOnReload()
    {
        Animator anim = currentGraphics.GetComponent<Animator>();
        if(anim != null)
        {
            anim.SetTrigger(“Reloading”);
        }
    }
    void SetLayerRecursively(GameObject obj, int newLayer)
    {
        obj.layer = newLayer;
        foreach (Transform child in obj.transform)
        {
            SetLayerRecursively(child.gameObject, newLayer);
        }
    }
}
————————————————————————————————
still working on UI I will post code ASAP.
on July 13, 2017

Hi. I have a question about the weapon manager script. What is the purpose of the game objects weaponHolderG and sideWepHolderG within the scipt?

Show more replies
  • Liked by
Reply
Cancel
0 on July 13, 2017

I have tried adding 2 weapon holders to my game and when I create a room, it spawns in 2 Player 1’s, has 2 audio listeners because of it, the secondary weapon graphic doesn’t spawn and I can’t shoot. Please help if you can

  • Liked by
Reply
Cancel
0 on July 13, 2017

use the ‘Insert Codes’ option in the forum response window next time please!

  • Liked by
Reply
Cancel