Search Unity

Sync Weapon Obj.

Discussion in 'Multiplayer' started by casborg, Mar 16, 2018.

  1. casborg

    casborg

    Joined:
    Sep 20, 2013
    Posts:
    8
    Hello everybody!
    So the problem is the first player who connects sees the weapon of second connected player, but the second player doesn't see first connected player's weapon.

    Should I make after connecting to the server, get every player's weapon and display them on current player's display? I mean syncing.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Can you show the code where you are creating or showing weapons? Use code tags also.
     
  3. casborg

    casborg

    Joined:
    Sep 20, 2013
    Posts:
    8
    [Command]
    public void CmdChangeWeapon(WeaponType wt)
    {
    ChangeWeapon(wt);
    RpcChangeWeapon(wt);
    }
    [ClientRpc]
    private void RpcChangeWeapon(WeaponType wt)
    {
    ChangeWeapon(wt);
    }
    private void ChangeWeapon(WeaponType wt)
    {
    switch (wt)
    {
    case WeaponType.skorpion:
    hhc.leftHandObj = handPoints[0];
    hhc.rightHandObj = handPoints[1];
    weaponObjs[0].SetActive(true);
    _as = weaponObjs[0].GetComponent<AudioSource>();
    shootPoint = shootPoints[0];
    shootParticle = shootPoints[0].GetComponentInChildren<ParticleSystem>().gameObject;
    accuracy = 1.055f;
    shootTimer = 0.15f;
    shootSound = shootSounds[0];
    reloadSound = reloadSounds[0];
    curWeaponObj = weaponObjs[0];
    break;
    case WeaponType.ump45:
    hhc.leftHandObj = handPoints[2];
    hhc.rightHandObj = handPoints[3];
    weaponObjs[1].SetActive(true);
    _as = weaponObjs[1].GetComponent<AudioSource>();
    shootPoint = shootPoints[1];
    shootParticle = shootPoints[1].GetComponentInChildren<ParticleSystem>().gameObject;
    accuracy = 1.035f;
    shootTimer = 0.22f;
    shootSound = shootSounds[1];
    reloadSound = reloadSounds[1];
    curWeaponObj = weaponObjs[1];
    break;
    case WeaponType.ak47:
    hhc.leftHandObj = handPoints[4];
    hhc.rightHandObj = handPoints[5];
    weaponObjs[2].SetActive(true);
    _as = weaponObjs[2].GetComponent<AudioSource>();
    shootPoint = shootPoints[2];
    shootParticle = shootPoints[2].GetComponentInChildren<ParticleSystem>().gameObject;
    accuracy = 1.015f;
    shootTimer = 0.28f;
    shootSound = shootSounds[2];
    reloadSound = reloadSounds[2];
    curWeaponObj = weaponObjs[2];
    break;
    case WeaponType.m4:
    hhc.leftHandObj = handPoints[6];
    hhc.rightHandObj = handPoints[7];
    weaponObjs[3].SetActive(true);
    _as = weaponObjs[3].GetComponent<AudioSource>();
    shootPoint = shootPoints[3];
    shootParticle = shootPoints[3].GetComponentInChildren<ParticleSystem>().gameObject;
    accuracy = 1.025f;
    shootTimer = 0.19f;
    shootSound = shootSounds[3];
    reloadSound = reloadSounds[3];
    curWeaponObj = weaponObjs[3];
    break;
    case WeaponType.grenade:
    weaponObjs[4].SetActive(true);
    break;
    }
    }
    }
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Here's how you use code tags on the forum:
    https://forum.unity.com/threads/using-code-tags-properly.143875/

    You haven't' posted where CmdChangeWeapon gets called, or if RpcChangeWeapon is only ever called from that command. I'm going to assume that you are calling CmdChangeWeapon when a new player is created. If that is the case then later players will never see the weapons of earlier players, since RPC's are not cached. Only clients that are connected at the time the RPC is called will receive it.

    You could change your system so it works off of a syncvar and runs out of start so all newly connecting players will know what weapon to setup, or you could call the RPC every time a new player connects.
     
  5. casborg

    casborg

    Joined:
    Sep 20, 2013
    Posts:
    8
    It gets called in void Start(){}, once player connects to the server.
    I really like the second option (new player connects), is in NetworkBehaviour any function to check if a new player is connected?
    Anyway, I am going to check about SyncVar in reference.
    Thanks for helping!
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I believe there is a networkserver or network manager callback available, but I usually just use Start in the Player object, since you generally want to make sure the new Player object has been instantiated/spawned before you start sending out further instructions to the client anyways.
     
  7. casborg

    casborg

    Joined:
    Sep 20, 2013
    Posts:
    8
    So I just added the function to showMyWeapon() to the player object and once someone connects to the server, the server calls this functions from the localplayer as RPC. Thanks for clarifying, now everything is working.