Search Unity

Problem With Syncing Player Weapons Unity Networking

Discussion in 'Scripting' started by sourlime99, Sep 21, 2017.

  1. sourlime99

    sourlime99

    Joined:
    Apr 19, 2013
    Posts:
    16
    Hi all
    im making a small fps multiplayer using the unity multiplayer stuff. i have made it so you can switch weapons from the choice of two. i have two game objects one is a assault rifle and one is a sniper. i have attached a gun script to both that has fire rate and range. this works all fine. To change gun i simply disabled the gun script for that certain gun and also disabled the mesh render for that gun. I was having the problem where
    if you switch weapon would switch all the clients weapons visually. fixed this now tho but now im having the problem of syncing what weapon is currently out. Im not sure how to go ahead and do it as i know i need to use [synvar] and [clientrpc] i think but i dont know how to go around it. thanks for your help

    i have included the script i made to change weapons. i know its probably quite basic and i have tried to do some research on it but im really not too sure about this one thank you!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class SwitchGuns : NetworkBehaviour {
    7.    
    8.     NetworkIdentity NetworkIdentity;
    9.     GunScript AkScript;
    10.     GunScript SniperScript;
    11.     MeshRenderer AKRender;
    12.     MeshRenderer SniperRender;
    13.  
    14.  
    15.  
    16.  
    17.     // Use this for initialization
    18.     void Start()
    19.     {
    20.  
    21.         //find components
    22.         NetworkIdentity = GetComponentInParent<NetworkIdentity>();
    23.         if (NetworkIdentity.isLocalPlayer == true)
    24.         {
    25.        
    26.         AkScript = transform.Find("AK-47").GetComponent<GunScript>();
    27.         SniperScript = transform.Find("L96_Sniper_Rifle").GetComponent<GunScript>();
    28.         AKRender = transform.Find("AK-47").GetComponent<MeshRenderer>();
    29.         SniperRender = transform.Find("L96_Sniper_Rifle").GetComponent<MeshRenderer>();
    30.  
    31.  
    32.  
    33.         //initilise varables on spawn
    34.  
    35.  
    36.  
    37.         AkScript.enabled = true;
    38.         SniperScript.enabled = false;
    39.         AKRender.enabled = true;
    40.         SniperRender.enabled = false;
    41.         }
    42.     }
    43.  
    44.     // Update is called once per frame
    45.     void Update()
    46.     {
    47.         if (NetworkIdentity.isLocalPlayer == true)
    48.         {
    49.             //ak enabled
    50.             if (Input.GetButtonDown("PrimaryGun"))
    51.             {
    52.  
    53.                 SniperScript.enabled = false;
    54.                 AkScript.enabled = true;
    55.  
    56.                 AKRender.enabled = true;
    57.                 SniperRender.enabled = false;
    58.  
    59.              
    60.             }
    61.             //sniper enabled
    62.             if (Input.GetButtonDown("SecondaryGun"))
    63.             {
    64.                 AkScript.enabled = false;
    65.                 SniperScript.enabled = true;
    66.  
    67.                 SniperRender.enabled = true;
    68.                 AKRender.enabled = false;
    69.                
    70.              
    71.  
    72.             }
    73.         }
    74.     }
    75.  
    76.    
    77.  
    78.    
    79. }
    80.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Generally when you want to do something from a client and have it set on all other clients, you do the following.

    Call a command on the client's Player object (where isLocalPlayer is true) which runs that command on the server/host. The server/host within the command then does the change like updating SyncVars or calling a ClientRpc, which will update the same information on all copies of your Player object on all clients. SyncVars are pretty easy to use for updating things like gun stats, etc, and a ClientRpc would be easy to use for instructing clients to change gun models. Though you could do either with either really.