1
\$\begingroup\$

I am new to networking with so this is probably a noob question. For context I am trying to get a gunshot sound effect to play and be heard by all players.

\$\endgroup\$
1
\$\begingroup\$

The specifics of Unity networking kind of fly right over my brain, but here's a rough conceptual model to how a game would conventionally handle this.

1. Client 1 tells the server "I'm shooting my gun now". Client 1 plays the gun noise even if the server says later they didn't shoot their gun and they've been a very naughty cheaty boy. (Damage deductions should be checked by the server though)

2. The Server confirms that Client 1 can shoot their gun and they are where they say they are. (Server should always be authoritative over actions that can cause damage to other players or lead to potential exploits)

3. If Client 1 could shoot their gun then the Server multicasts this information to the other Clients ("Hey fellas, Client 1 just shot a thing!"). They then update their local representation of Client 1 to play the gun audio sting.

Rejection Path: Server does not agree with Client 1 over this violent shooty interruption, and does not propagate the change to the other Clients.

The server shouldn't know anything about the audio assets. It's multicasting the concept that the player has shot, not the sound itself. Let the client code play the sound when they get the message.

\$\endgroup\$
0
\$\begingroup\$

The client will already have the sound ready to be played, it just needs to know when to play it.

All you need to do, is ensure you are playing the sounds at the right time. In most cases the sound will play due to another action or event happening.

Did the server send out a notification telling you that someone died? Play the death sound. Did you get a notification from the server that a coin was picked up? Play the coin was picked up sound.

\$\endgroup\$
0
\$\begingroup\$

If you want server controlled sound on the clients, the clients would either have to have them already or download them while connecting (you could also stream them, but I don't see a point in that + playback will delay a bit). To know, when to play what sound add sound-sources the server can send to the client, which contain information about things like what sound to play (of course), when to play it, from where to play it, how loud and so on. You could for example store all sounds in a hashmap (client) to map sound names to actual sounds.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.