Facebook
From Sexy Lemur, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 202
  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3.  
  4. public class WeaponSwitch : NetworkBehaviour
  5. {
  6.  
  7.     [SyncVar] public int currentWeapon = 0;
  8.  
  9.     private PlayerShooting shootScript;
  10.     private bool weaponEquipped;
  11.     [SerializeField] private GameObject weaponHolder;
  12.  
  13.  
  14.     private
  15.  
  16.         // Use this for initialization
  17.         void Start () {
  18.         shootScript = GetComponent<PlayerShooting>();
  19.     }
  20.  
  21.     // Update is called once per frame
  22.    
  23.     void Update () {
  24.  
  25.         GetWeaponInput();
  26.  
  27.     }
  28.    
  29.    
  30.     void GetWeaponInput()
  31.     {
  32.  
  33.         if (!isLocalPlayer) return;
  34.  
  35.         SelectWeapon(gameObject.name);
  36.  
  37.         int prevWeapon = currentWeapon;
  38.  
  39.  
  40.         if (Input.GetAxis("Mouse ScrollWheel") > 0f)
  41.         {
  42.             if (currentWeapon >= weaponHolder.transform.childCount - 1)
  43.                 currentWeapon = 0;
  44.             else
  45.             {
  46.                 currentWeapon++;
  47.             }
  48.         }
  49.  
  50.         if (Input.GetAxis("Mouse ScrollWheel") < 0f)
  51.         {
  52.             if (currentWeapon <= 0)
  53.                 currentWeapon = weaponHolder.transform.childCount - 1;
  54.             else
  55.             {
  56.                 currentWeapon--;
  57.             }
  58.         }
  59.  
  60.         if (Input.GetKeyDown(KeyCode.L))
  61.             currentWeapon = 0;
  62.  
  63.         if (Input.GetKeyDown(KeyCode.Alpha1))
  64.             currentWeapon = 1;
  65.  
  66.         if (Input.GetKeyDown(KeyCode.Alpha2))
  67.             currentWeapon = 2;
  68.  
  69.         if (Input.GetKeyDown(KeyCode.Alpha3))
  70.             currentWeapon = 3;
  71.  
  72.         if (Input.GetKeyDown(KeyCode.Alpha4))
  73.             currentWeapon = 4;
  74.  
  75.         weaponEquipped = currentWeapon > 0;
  76.  
  77.         if (currentWeapon != prevWeapon)
  78.             SelectWeapon(gameObject.name);
  79.  
  80.  
  81.         shootScript.weaponEquipped = weaponEquipped;
  82.  
  83.     }
  84.  
  85.     void SelectWeapon(string _playerID)
  86.     {
  87.         Player _player = GameManager.GetPlayer(_playerID);
  88.  
  89.         Debug.Log("Player ID: " + _player.name);
  90.  
  91.         Transform transformsInChild = _player.GetComponentInChildren<Transform>();
  92.         foreach (Transform transformInChild in transformsInChild)
  93.         {
  94.             if (transformInChild.name == "_Weapons")
  95.                 weaponHolder = transformInChild.gameObject;
  96.         }
  97.  
  98.         if (weaponHolder != null)
  99.         {
  100.             Debug.Log("Looping weapons...");
  101.  
  102.             int i = 0;
  103.             foreach (Transform weapon in weaponHolder.transform)
  104.             {
  105.                 if (i == currentWeapon)
  106.                 {
  107.                     Debug.Log(_playerID + " equipping weapon (" + weapon.name + ")");
  108.                     weapon.gameObject.SetActive(true);
  109.                     CmdSyncWeaponWithRemote(i, _playerID, true);
  110.                 }
  111.                 else
  112.                 {
  113.                     weapon.gameObject.SetActive(false);
  114.                     CmdSyncWeaponWithRemote(i, _playerID, false);
  115.                    
  116.                 }
  117.  
  118.  
  119.                 i++;
  120.             }
  121.         }
  122.     }
  123.  
  124.     [Command]
  125.     void CmdSyncWeaponWithRemote(int _weaponID, string _playerID, bool _active)
  126.     {
  127.         if (isServer)
  128.             RpcSyncWeaponWithRemote(_weaponID, _playerID, _active);
  129.     }
  130.  
  131.  
  132.     /*   Bug: Doesnt sync equipped weapon from remote to host, i.e. host player equips a M4 Carbine, the remote will see it, but if a remote equips a weapon, the host will not see it      */
  133.  
  134.  
  135.  
  136.     [ClientRpc]
  137.     void RpcSyncWeaponWithRemote(int _weaponID, string _playerID, bool _active)
  138.     {
  139.         Player _player = GameManager.GetPlayer(_playerID);
  140.  
  141.         Transform transformsInChild = _player.GetComponentInChildren<Transform>();
  142.         foreach (Transform transformInChild in transformsInChild)
  143.         {
  144.             if (transformInChild.name == "_Weapons")
  145.                 weaponHolder = transformInChild.gameObject;
  146.         }
  147.  
  148.         weaponHolder.transform.GetChild(_weaponID).gameObject.SetActive(_active);
  149.     }
  150.  
  151. }
  152.