can't Sync In Clients! unity Fps

by AnasTe   Last Updated May 14, 2018 01:13 AM

when I'm trying to build my Multiplayer Game to test my method I have some issue with a client! when shooting in client all of my new methods can't works I say about "Shooting and Equipped Weapons" I need someone to help me, please! that's my code"it's too long But i'm sorry Guys " :

 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()
    {
        EquipWeapon(1);
    }

    public PlayerWeapon GetCurrentWeapon()
    {
        return currentWeapon;
    }

    public WeaponGraphics GetCurrentGraphics()
    {
        return currentGraphics;
    } 



    void EquipWeapon(int _weapon)
    {

        CmdEquipWeapon(_weapon);
        DoEquipWeapon(_weapon);
    }


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



    void DoEquipWeapon(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:
            currentWeapon = specialWeaponS;
            break;
        }

      if (isLocalPlayer)
        {
            currentWeapon.weaponGraphics.SetActive(true);
        }

       currentGraphics = currentWeapon.weaponGraphics.GetComponent<WeaponGraphics>();


       if (isLocalPlayer)
        {

         Utility.SetLayerRecurcively(currentWeapon.weaponGraphics, LayerMask.NameToLayer(weaponLayerName));
        }
    }

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




 // 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




Object spawn only on Server in multiplayer unity3d game

Updated September 18, 2016 08:07 AM