Totally lost with UNET - Client to Server Networking

Hello there,

I’m trying to recreate a bomb system like CS:GO has, but I have been trying for some time now, and I’m at the edge of giving up.


I have had everything working multiple times, like sound, UI etc… But one thing just keeps messing me up and that’s client to server. Everything always works fine when the server player does something, but the other way around I never got it working.


A quick example of what I want to do:


A player presses “E” and then music starts playing. This code is on the “Bomb” prefab with a network Identity etc…

When the host presses “E”, every client starts playing the sound too! But the other way around I always got stuck at authorisation errors.


My current very simple code:

public class Bomb : NetworkBehaviour {

    public AudioSource bombMusic;

    [SyncVar]
    public float timeLeft = 40f;

    [SyncVar]
    public bool hasBeenPlanted;

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.P))
        {
            if (isLocalPlayer)
            {
                RpcStartBombSequence();
            } else
            {
                CmdStartBombSequence();
            }
        }
    }


    [ClientRpc]
    void RpcStartBombSequence()
    {
        hasBeenPlanted = true;
        bombMusic.Play();
    }
   
    [Command]
    void CmdStartBombSequence()
    {
        RpcStartBombSequence();
    }
}

Also, the Syncvar updates on all clients when the server changes it, but not the other way around. How could I fix this?

Thank you very much,
I appreciate all replies.

-Tristan

alt text

All scripts that send Commands to the server, should be attached to the player object.

  • Command methods must be invoked on the client and will be executed on the server in the corresponding object.
  • The client must have authority on the object where the Command is. By default, only the player object spawned for each client has client authority. Attach the scripts with Commands to that object.
  • ClientRpc methods must be invoked on the server and will be executed on all clients in the corresponding object.
  • SyncVar must only be modified on the server. It only syncs from server to all clients. Use Commands with parameters to send state from a client to the server.
  • Read https://docs.unity3d.com/Manual/UNetConcepts.html

Place this script on the bomb object/prefab.

public class Bomb : NetworkBehaviour
{
    public AudioSource bombMusic;

    [SyncVar]
    public float timeLeft = 40f;

    [SyncVar]
    public bool hasBeenPlanted;

    [ClientRpc]
    public void RpcHasBeenPlantedChanged()
    {
        // all clients execute
        if (hasBeenPlanted)
        {
            bombMusic.Play();
        }
        else
        {
            bombMusic.Stop();
        }
    }
}

Place this script on the player object/prefab.

public class BombPlayerInput : NetworkBehaviour
{
    private void Update()
    {
        if (isLocalPlayer)
        {
            // one client executes
            if (Input.GetKeyDown(KeyCode.P))
            {
                GameObject bomb = /* Get a reference to the bomb object this player is interacting with. */
                CmdStartBombSequence(bomb);
            }
        }
    }

    [Command]
    public void CmdStartBombSequence(GameObject bombGo)
    {
        // server executes
        var bomb = bombGo.GetComponent<Bomb>();
        bomb.hasBeenPlanted = true;
        bomb.RpcHasBeenPlantedChanged();
    }
}

With Unet you can only use server methods when you have local authority over the object you want to call the method from. You have an authority over your player by default, but for any other object you have to give authority to the client and it has to be done on server.

Sadly it was quite some time ago so I don’t remember how it’s done exactly, but I hope you understand what I mean and will have something to google. It is there 100%, I remember learning about this

Again, sorry for incomplete answer

Ok, so after trying AssignClientAuthority, I’m still lost. Now the server gives non-authority errors and the clients don’t work either. Anyone else who could help me further?