Search Unity

[SOLVED] [UNET] How to correctly sync Weapon Switching (Using SetActive()) over network?

Discussion in 'UNet' started by sambid123sameer, Jul 20, 2017.

  1. sambid123sameer

    sambid123sameer

    Joined:
    Jun 29, 2016
    Posts:
    11
    Hello!

    I have this code to Equip a weapon
    1. [Command]
    2. void CmdEquipWeapon(int val){
    3. RpcEquipWeapon (val);
    4. }
    5. [ClientRpc]
    6. void RpcEquipWeapon(int val){
    7. EquipWeapon (val);
    8. }
    9. void EquipWeapon(int val) {
    10. Debug.Log ("Equipping weapons for = " + GetComponent<Player> ().Username);
    11. if (isReloading) {
    12. return;
    13. }
    14. foreach (GameObject wep in Weapons){
    15. wep.SetActive (false);
    16. }
    17. Weapons [val].SetActive (true);
    18. WeaponNameText.text = Weapons [val].transform.name;
    19. weapon = WeaponHolderObject.GetComponentInChildren<Weapon> ();
    20. GetComponent<WeaponSyncRotation> ().leftHandTransform = weapon.gameObject.transform;
    21. WeaponHUD.sprite = weapon.HUD;
    }

    This works perfectly when im switching weapons on the client , it gets synced across network But when i switch weapons on the Server (Host) , It switches the weapons of all clients on the server And the debug message is thrown on all the clients , saying they changed their own weapons ;_;

    What am i doing wrong?

    Please help TIA :)
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    You're transmitting Rpc to ALL objects that can receive that Rpc.
    With that in mind I would've used the following - [TargetRpc] attribute, to send only to the player that requested that command. Use connectionToClient for that.

    Another alternative is to write a custom NetworkMessage handler, if you prefer LLAPI.
    You can always go with a pleb way of doing this - checking isLocalPlayer. Although, in my opinion, this approach is far beyond inefficient.

    Also, try to use code tag for pasting code in the future.
     
  3. Deleted User

    Deleted User

    Guest

    Use a islocalplayercheck before calling the command. Like this:

    Code (CSharp):
    1. if(islocalplayer)
    2. {
    3.       CmdEquipWeapon(val);
    4. }
    I hope this works for you!
     
  4. sambid123sameer

    sambid123sameer

    Joined:
    Jun 29, 2016
    Posts:
    11
    Hey Thanks for your reply
    Can you please tell me more about it? im kind of a beginner :p
    I tried using TargetRpc and connectionToClient , but i think connectionToClient is null and it didnt sync with anything
     
  5. sambid123sameer

    sambid123sameer

    Joined:
    Jun 29, 2016
    Posts:
    11
    Thanks for you reply
    Unfortunately , i have already tried it and it seems to make no change , what it just did is not update it on the Server but it did the weapon changing on all clients
     
  6. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    All you have to do is this:
    Make a syncvar of the color.
    On the hook method, apply the syncvar value and then apply the value to the flag.
    And then during initalization, also apply the color.
    And then before you spawn the object over the network. Just set the syncvar on the SERVER instance. (from Server Code)
     
  7. sambid123sameer

    sambid123sameer

    Joined:
    Jun 29, 2016
    Posts:
    11
    Thanks for your reply
    I got it fixed finally , what i did is just disabled the Weapon script on other clients except mine and it works now !
     
  8. sambid123sameer

    sambid123sameer

    Joined:
    Jun 29, 2016
    Posts:
    11
    Finally Fixed it
    I just had to disable the equipping script on the other clients except the local player and its works fine now
    Thanks community
     
    Slupchuk likes this.