Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party Sync weapon switching (Mirror)

Discussion in 'Multiplayer' started by Lenda142, Feb 27, 2023.

  1. Lenda142

    Lenda142

    Joined:
    Jan 1, 2023
    Posts:
    1
    Hi everyone,

    I am fairly new to Mirror networking so I need your help with syncing weapon switching. My problem is that when I switch weapons on client 1, the weapon is still visible on client 2. I tried making a separate variable I would [SyncVar] over the network as a GameObject using [Command] or [ClientRpc] but I couldn't make work that way and I don't understand how to make custom data writers when I try to sync unsupported types and if someone knows I would really like to know. Thanks in advance, below are the script code and player hierarchy.

    Code (CSharp):
    1. using UnityEngine;
    2. using Mirror;
    3.  
    4. public class scr_WeaponManager : NetworkBehaviour {
    5.  
    6.     [SerializeField] private int selectedWeapon = 0;
    7.  
    8.     [SerializeField] private Transform weaponHolder;
    9.  
    10.     public scr_Weapon currentWeapon;
    11.  
    12.     private void Start() {
    13.         SelectWeapon();
    14.     }
    15.  
    16.     [Client]
    17.     private void SelectWeapon() {
    18.         if (!isLocalPlayer)
    19.             return;
    20.  
    21.         int i = 0;
    22.  
    23.         foreach (Transform weapon in weaponHolder) {
    24.             if (i == selectedWeapon) {
    25.                 weapon.gameObject.SetActive(true);
    26.  
    27.                 if (weapon.childCount == 0) {
    28.                     currentWeapon = null;
    29.                     weapon.gameObject.SetActive(false);
    30.                     i++;
    31.                     continue;
    32.                 }
    33.  
    34.                 currentWeapon = weapon.childCount > 0 ? weapon.GetChild(0).GetComponent<scr_Weapon>() : null;
    35.  
    36.  
    37.                 SetLayerRecursively();
    38.             }
    39.             else {
    40.                 weapon.gameObject.SetActive(false);
    41.             }
    42.  
    43.             i++;
    44.         }
    45.     }
    46.  
    47.     [Client]
    48.     public void SwitchWeapon(float direction) {
    49.  
    50.         if (!isLocalPlayer)
    51.             return;
    52.  
    53.         if(direction > 0) {
    54.             if(selectedWeapon >= weaponHolder.childCount - 1) {
    55.                 selectedWeapon = 0;
    56.             } else {
    57.                 selectedWeapon++;
    58.             }
    59.         } else if (direction < 0) {
    60.             if (selectedWeapon <= 0) {
    61.                 selectedWeapon = weaponHolder.childCount - 1;
    62.             }
    63.             else {
    64.                 selectedWeapon--;
    65.             }
    66.         } else {
    67.             return;
    68.         }
    69.  
    70.         SelectWeapon();
    71.     }
    72.  
    73.     private void SetLayerRecursively() {
    74.         if (!currentWeapon || !isLocalPlayer)
    75.             return;
    76.  
    77.         LayerMask weaponLayer = LayerMask.NameToLayer(scr_PlayerGlobals.weaponLayer);
    78.         if (currentWeapon.gameObject.layer != weaponLayer.value) {
    79.             currentWeapon.gameObject.layer = weaponLayer.value;
    80.             foreach (Transform weaponPart in currentWeapon.transform) {
    81.                 weaponPart.gameObject.layer = weaponLayer.value;
    82.             }
    83.         }
    84.     }
    85. }
    86.  
     

    Attached Files: