Search Unity

ClientRpc Weapon change and Network Identity Problems

Discussion in 'Multiplayer' started by Tritos, Dec 13, 2017.

  1. Tritos

    Tritos

    Joined:
    Dec 3, 2017
    Posts:
    1
    Hey,

    I am currently trying my best to programm an online FPS with the Networking functionality built into Unity. However while setiing up a script for equiping and unequiping weapons, I have run into some problems.

    This is my code for doing this:

    Code (CSharp):
    1.  
    2. [SerializeField]
    3.  
    4.     private Transform weaponHolder;
    5.  
    6.     [SerializeField]
    7.     private Transform secondaryWeaponHolster;
    8.  
    9.     [SerializeField]
    10.     private Transform primaryWeaponHolster;
    11.  
    12. [SerializeField]
    13.     private GameObject primaryWeapon;
    14.  
    15.     [SerializeField]
    16.     private GameObject secondaryWeapon;
    17.  
    18.     private GameObject currentWeapon;
    19.  

    Code (CSharp):
    1.    
    2.  
    3. [Command]
    4.     void CmdEquipWeapon(GameObject _weapon)
    5.     {
    6.         RpcEquipWeapon(_weapon);
    7.     }
    8.  
    9.  
    10. [ClientRpc]
    11.     void RpcEquipWeapon(GameObject _weapon)
    12.     {
    13.         RpcUnequipCurrentWeapon();
    14.         currentWeapon = _weapon;
    15.  
    16.         currentWeapon.transform.SetParent(weaponHolder);
    17.         currentWeapon.transform.position = weaponHolder.position;
    18.         currentWeapon.transform.rotation = weaponHolder.rotation;
    19.     }
    20.  
    21.     [ClientRpc]
    22.     void RpcUnequipCurrentWeapon()
    23.     {
    24.         if(currentWeapon != null)
    25.         {
    26.             if(currentWeapon == primaryWeapon)
    27.             {
    28.                 currentWeapon.transform.SetParent(primaryWeaponHolster);
    29.                 currentWeapon.transform.position = primaryWeaponHolster.position;
    30.                 currentWeapon.transform.rotation = primaryWeaponHolster.rotation;
    31.             }
    32.             else if(currentWeapon == secondaryWeapon)
    33.             {
    34.                 currentWeapon.transform.SetParent(secondaryWeaponHolster);
    35.                 currentWeapon.transform.position = secondaryWeaponHolster.position;
    36.                 currentWeapon.transform.rotation = secondaryWeaponHolster.rotation;
    37.             }
    38.             currentWeapon = null;
    39.         }
    40.     }

    Some explanations:
    • I have two weapons: a primary and a secondary (these are already instantiated (in the Start() method))
    • For both I have "Holsters" (basically just positions on the player, where they can passively stored)
    • The weapon Holder is a transform that represents the hands of the player
    • current weapon stores the weapon which is currently being used. This variable is later accesed by another script that "shoots"
    This code works offline (if I remove the command and make the Rpcs normal voids). However when I try it out on a server I get an error stating that my weapon object needs a network Identity. I tried adding one but it did not help.

    I also searched on the forums for solutions, but the only one I found was this one:
    https://forum.unity.com/threads/networking-weapon-switching-in-a-multiplayer-fps.455277/
    (It did not work...)

    Thanks in advance
    Philipp
     
    Last edited: Dec 13, 2017
  2. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    What I did for weapons was use a synchronized integer index representing the weapon equipped for each slot.

    i.e.
    0 = nothing equipped
    1 = gun
    2 = sword
    3 = axe

    Then when the index changes you can destroy the old weapon attached to the transform node, then instantiate and attach a new weapon to the node.