what's the wrong in switching Script - Unity Multiplayer

by AnasTe   Last Updated May 15, 2018 02:13 AM

i'm trying to switch between weapon and switched but when show another screen * some example * in client i don't look any change ! this screen about what i mean !

[1] : https://imgur.com/uEUOFhC "what's i mean"

[2] :https://imgur.com/GqT8OZV "Player Inspector"

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
[RequireComponent(typeof(PlayerShoot))]

public class WeaponManager : NetworkBehaviour
{

     [SerializeField]
     private string weaponLayerName = "Weapon";

     [SerializeField]
     private Transform weaponHolder; // position of weapon :D

     [SerializeField] private PlayerWeapon primaryWeapon;

     [SerializeField] private PlayerWeapon secondaryWeapon;

      [SerializeField] private PlayerWeapon specialWeapon;

      [SerializeField] private PlayerWeapon specialWeaponS;
      [SyncVar] private int currentWeaponIndex = 0;

      private PlayerWeapon currentWeapon;
      private WeaponGraphics currentGraphics;
      private PlayerControllers animm;

      public bool isReloading = false;
      private Animator anim;

      private PlayerShoot playerSh;


   void Start()
    {
        //EquipWeapon(primaryWeapon);

         EquipWeapon(1);
         anim = GetComponent<Animator>();
   }
   public override void OnStartClient()
    {
        //base.OnStartClient();
        EquipWeapon(1);
    }

    public PlayerWeapon GetCurrentWeapon()
     {
        return currentWeapon;
     }

     public WeaponGraphics GetCurrentGraphics()
       {
          return currentGraphics;
       }


     public void EquipWeapon(int _weapon)
      {

         CmdEquipWeapon(_weapon);
         RpcDoEquipWeapon(_weapon);
      }


    [Command]
    public void CmdEquipWeapon(int _weapon)
    {
          RpcDoEquipWeapon(_weapon);
    }


    [ClientRpc]
    public void RpcDoEquipWeapon(int _weapon)
     {

          if (_weapon == currentWeaponIndex)
           return;

          currentWeaponIndex = _weapon;

          switch (_weapon)
           {
             case 0:
                // no weapon
                currentWeapon = null;
                break;
            case 1:
               // primary
                currentWeapon = primaryWeapon;
                break;
            case 2:
                // secondary
                  currentWeapon = secondaryWeapon;
                  break;
            case 3:
                // special
                  currentWeapon = specialWeapon;
                  break;
            case 4:
                 // last one
                  currentWeapon = specialWeaponS;
                  break;
         }

        if (isLocalPlayer)
          {
              currentWeapon.weaponGraphics.SetActive(true);
              Utility.SetLayerRecurcively(currentWeapon.weaponGraphics, LayerMask.NameToLayer(weaponLayerName));
          }
        currentGraphics = currentWeapon.weaponGraphics.GetComponent<WeaponGraphics>();

    }

//[ClientRpc]
//void RpcDoEquipWeapon(int _weapon)
//{
//    if (!isLocalPlayer)
//    {
//        currentWeapon.weaponGraphics.SetActive(true);
//    }
//}



public void Reload()
{

    if (isReloading)
        return;
    anim.SetTrigger("reload");
    StartCoroutine(Relaad_Coroutine());
}

private IEnumerator Relaad_Coroutine()
{
    anim = GetComponent<Animator>();
    anim.SetTrigger("reload");

    Debug.Log("Reloadin . . . ");
    isReloading = true;

    yield return new WaitForSeconds(1f);
    currentWeapon.bullets = currentWeapon.maxBullets; // reload the 0bullets to max bullet = 50
    isReloading = false;
}


[Command]
void CmdOnReload()
{
    RpcOnReload();
}


[ClientRpc]
void RpcOnReload()
{
    anim = GetComponent<Animator>();

    //Animator anim = animm.GetComponent<Animator>();
    if (anim != null)
    {
        anim.SetTrigger("reload");
    }

}
// Update is called once per frame
void Update()
{


    if (!isLocalPlayer)
        return;
    if (Input.GetKeyDown(KeyCode.Alpha1))
    {
        currentWeapon.weaponGraphics.SetActive(false);
        EquipWeapon(3);

    }
    if (Input.GetKeyDown(KeyCode.Alpha2))
    {
        currentWeapon.weaponGraphics.SetActive(false);
        EquipWeapon(2);

    }

    if (Input.GetKeyDown(KeyCode.Alpha3))
    {
        currentWeapon.weaponGraphics.SetActive(false);
        EquipWeapon(1);

    }


 }


 }


Related Questions




Delayed linear interpolation for networking

Updated June 13, 2017 23:13 PM

Creating a master server for Unity game (Unity 2017)

Updated September 08, 2017 15:13 PM