Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Weapon Switching Multiplayer?

Discussion in 'Multiplayer' started by Lost_Evo, Jun 27, 2020.

  1. Lost_Evo

    Lost_Evo

    Joined:
    Jun 8, 2020
    Posts:
    2
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class WeaponManager : NetworkBehaviour {
    6.  
    7.     [SerializeField]
    8.     private string weaponLayerName = "Weapon";
    9.  
    10.     [SerializeField]
    11.     private Transform weaponHolder;
    12.  
    13.     [SerializeField]
    14.     private PlayerWeapon primaryWeapon;
    15.  
    16.     private PlayerWeapon currentWeapon;
    17.     private WeaponGraphics currentGraphics;
    18.  
    19.     public bool isReloading = false;
    20.  
    21.     void Start ()
    22.     {
    23.         EquipWeapon(primaryWeapon);
    24.     }
    25.  
    26.     public PlayerWeapon GetCurrentWeapon ()
    27.     {
    28.         return currentWeapon;
    29.     }
    30.  
    31.     public WeaponGraphics GetCurrentGraphics()
    32.     {
    33.         return currentGraphics;
    34.     }
    35.  
    36.     void EquipWeapon (PlayerWeapon _weapon)
    37.     {
    38.         currentWeapon = _weapon;
    39.  
    40.         GameObject _weaponIns = (GameObject)Instantiate(_weapon.graphics, weaponHolder.position, weaponHolder.rotation);
    41.         _weaponIns.transform.SetParent(weaponHolder);
    42.  
    43.         currentGraphics = _weaponIns.GetComponent<WeaponGraphics>();
    44.         if (currentGraphics == null)
    45.             Debug.LogError("No WeaponGraphics component on the weapon object: " + _weaponIns.name);
    46.  
    47.         if (isLocalPlayer)
    48.             Util.SetLayerRecursively(_weaponIns, LayerMask.NameToLayer(weaponLayerName));
    49.  
    50.     }
    51.  
    52.     public void Reload ()
    53.     {
    54.         if (isReloading)
    55.             return;
    56.  
    57.         StartCoroutine(Reload_Coroutine());
    58.     }
    59.  
    60.     private IEnumerator Reload_Coroutine ()
    61.     {
    62.         Debug.Log("Reloading...");
    63.  
    64.         isReloading = true;
    65.  
    66.         CmdOnReload();
    67.  
    68.         yield return new WaitForSeconds(currentWeapon.reloadTime);
    69.  
    70.         currentWeapon.bullets = currentWeapon.maxBullets;
    71.  
    72.         isReloading = false;
    73.     }
    74.  
    75.     [Command]
    76.     void CmdOnReload ()
    77.     {
    78.         RpcOnReload();
    79.     }
    80.  
    81.     [ClientRpc]
    82.     void RpcOnReload ()
    83.     {
    84.         Animator anim = currentGraphics.GetComponent<Animator>();
    85.         if (anim != null)
    86.         {
    87.             anim.SetTrigger("Reload");
    88.         }
    89.     }
    90.  
    91. }
    This is my Weapon Manager and I want to know how to add weapon Switching to this script?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Other than cleaning up the previously equipped weapon, doesn't EquipWeapon already do that?
     
  3. Lost_Evo

    Lost_Evo

    Joined:
    Jun 8, 2020
    Posts:
    2
    No thats for the one weapon I hae, but I want to add one more
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    But EquipWeapon takes a parameter for any weapon you choose to equip already. Just call it with a different weapon. Do you want to be more clear what specifically you need which isn't already in your code?