Facebook
From Scanty Human, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 199
  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.  
  12.     [SerializeField] private GameManager manager;
  13.     [SerializeField] private GameObject weaponHolder;
  14.  
  15.  
  16.     private
  17.  
  18.         // Use this for initialization
  19.         void Start () {
  20.         shootScript = GetComponent<PlayerShooting>();
  21.         manager = GameObject.FindGameObjectWithTag("GameManager").GetComponent<GameManager>();
  22.         if (manager == null)
  23.             Debug.LogError("Game Manager doesnt exist within this scene!");
  24.            
  25.     }
  26.  
  27.     // Update is called once per frame
  28.    
  29.     void Update () {
  30.  
  31.         GetWeaponInput();
  32.  
  33.     }
  34.    
  35.    
  36.     void GetWeaponInput()
  37.     {
  38.  
  39.         if (!isLocalPlayer) return;
  40.  
  41.         //SelectWeapon(gameObject.name);
  42.  
  43.         int prevWeapon = currentWeapon;
  44.  
  45.  
  46.         if (Input.GetAxis("Mouse ScrollWheel") > 0f)
  47.         {
  48.             if (currentWeapon >= weaponHolder.transform.childCount - 1)
  49.                 currentWeapon = 0;
  50.             else
  51.             {
  52.                 currentWeapon++;
  53.             }
  54.         }
  55.  
  56.         if (Input.GetAxis("Mouse ScrollWheel") < 0f)
  57.         {
  58.             if (currentWeapon <= 0)
  59.                 currentWeapon = weaponHolder.transform.childCount - 1;
  60.             else
  61.             {
  62.                 currentWeapon--;
  63.             }
  64.         }
  65.  
  66.         if (Input.GetKeyDown(KeyCode.L))
  67.             currentWeapon = 0;
  68.  
  69.         if (Input.GetKeyDown(KeyCode.Alpha1))
  70.             currentWeapon = 1;
  71.  
  72.         if (Input.GetKeyDown(KeyCode.Alpha2))
  73.             currentWeapon = 2;
  74.  
  75.         if (Input.GetKeyDown(KeyCode.Alpha3))
  76.             currentWeapon = 3;
  77.  
  78.         if (Input.GetKeyDown(KeyCode.Alpha4))
  79.             currentWeapon = 4;
  80.  
  81.         weaponEquipped = currentWeapon > 0;
  82.  
  83.         if (currentWeapon != prevWeapon)
  84.             SelectWeapon(gameObject.name);
  85.  
  86.  
  87.         shootScript.weaponEquipped = weaponEquipped;
  88.  
  89.     }
  90.  
  91.     public void SelectWeapon(string _playerID)
  92.     {
  93.         Player _player = GameManager.GetPlayer(_playerID);
  94.  
  95.         Debug.Log("Player ID: " + _player.name);
  96.  
  97.         Transform transformsInChild = _player.GetComponentInChildren<Transform>();
  98.         foreach (Transform transformInChild in transformsInChild)
  99.         {
  100.             if (transformInChild.name == "_Weapons")
  101.                 weaponHolder = transformInChild.gameObject;
  102.         }
  103.  
  104.         if (weaponHolder != null)
  105.         {
  106.             Debug.Log("Looping weapons...");
  107.  
  108.             int i = 0;
  109.             foreach (Transform weapon in weaponHolder.transform)
  110.             {
  111.                 if (i == currentWeapon)
  112.                 {
  113.                     Debug.Log(_playerID + " equipping weapon (" + weapon.name + ")");
  114.                     weapon.gameObject.SetActive(true);
  115.                     manager.CmdSyncWeapons(i, _playerID, true);
  116.                 }
  117.                 else
  118.                 {
  119.                     weapon.gameObject.SetActive(false);
  120.                     manager.CmdSyncWeapons(i, _playerID, false);
  121.  
  122.                 }
  123.  
  124.  
  125.                 i++;
  126.             }
  127.         }
  128.     }
  129.  
  130.     [Command]
  131.     void CmdSyncWeaponWithRemote(int _weaponID, string _playerID, bool _active)
  132.     {
  133.        
  134.            
  135.     }
  136.  
  137.  
  138.     /*   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      */
  139.  
  140.  
  141.  
  142.    
  143.  
  144. }
  145.