- Home /
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
Answer by villevli · Sep 16, 2018 at 10:39 PM
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.
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();
}
}
Thank you very much man! I really was on the edge of giving up after trying for such a long time. This example now gives me information about how to do other things too.
$$anonymous$$uch thanks and respect!
Answer by Casiell · Sep 16, 2018 at 05:56 PM
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
Yeah, I already tried googling something like this, but nothing ever worked for me. Thanks for the answer anyways, I appreciate every helping hand!
I will try this now: https://docs.unity3d.com/ScriptReference/Networking.NetworkIdentity.AssignClientAuthority.html
with the hope I succeed! :D
Answer by Arilith · Sep 16, 2018 at 09:53 PM
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?
Your answer
Follow this Question
Related Questions
What's an acceptable network send rate? 1 Answer
Unity networking tutorial? 6 Answers
Network how to send data/activate function 1 Answer
What's the best approach for creating a secure authorative server? 0 Answers
Server still sending Packets to inactive clients and throwing errors. How can I stop it? 0 Answers