- Home /
Audio in Multiplayer at Players Location
New to the entire process and been wracking my brain over this one. I've got the audio triggering when a player fires their weapon, works great. Where I'm stuck is when other people in the game fire their weapons, all the other players being able to hear them at that at the location the weapon was fired if their close enough.
I've got an AudioSource attached to the playing characters with the AudioClip in it. I'm calling on it by finding it as a child in my FireWeapon script then doing weaponFire.audio.Play();
I'm wondering how I'd write an RPC to be send out to the network that would tell everyone else the location the sound is originating. If code I have is needed, let me know.
C# if you post code, as that's what I'm currently working with. Thanks!
Answer by whydoidoit · Jun 22, 2012 at 07:34 PM
So just send an RPC to the player to play the clip on the local object.
In your player script:
@RPC
function PlaySound() {
if(networkView.isMine) {
networkView.RPC("PlaySound", RPCMode.Others);
}
//Play your sound on this character
}
(not sure how to post clean code in a comment, sorry)
private GameObject blasterSound;
blasterSound = GameObject.Find("BlasterSound");
[RPC] void PlaySound(GameObject sound) { if(networkView.is$$anonymous$$ine == true) { networkView.RPC("PlaySound", RPC$$anonymous$$ode.Others); } sound.audio.Play(); }
And then I call on this RPC when they fire the weapon with:
PlaySound(blasterSound);
This doesn't work and everyone can only hear their own weapons firing. I'm betting it's something simple to fix too but my limited C# knowledge base is showing. haha
Does it have something to do with each player having an object called that on them?
Ah yeah your problem is that you aren't passing the sound to it - you can't send one of those as an RPC so I meant that you needed to do it like this:
[RPC]
public void PlaySound() {
if(networkView.is$$anonymous$$ine == true) {
networkView.RPC("PlaySound", RPC$$anonymous$$ode.Others);
}
blasterSound.audio.Play();
}
If you needed to use more than one sound - put them in an array and send an index, like this the way you have it now:
public GameObject[] soundObjects;
[RPC]
public void PlaySound(int index) {
if(networkView.is$$anonymous$$ine == true) {
networkView.RPC("PlaySound", RPC$$anonymous$$ode.Others, index);
}
soundObject[index].audio.Play();
}
But you might be better off with an array of audio clips and just set the right clip before playing it...
NullReferenceException FireBlaster.PlaySound () (at Assets/Scripts/FireBlaster.cs:107)
That is the RPC line: blasterSound.audio.Play();
Something tells me I'm not finding the object correctly when I'm defining blasterSound. It's playing for the person whose machine it is, but I get that error and no one else can hear. I used the code just like you listed and called PlaySound(); when they fired their weapon.
I'll probably use the array way of doing it once I have more sounds, currently this is just a learning project. :D
So what is this BlasterSound? Presumably there must be a few of them right - one per player?
So presumably your gun on your player should have an AudioSource on it right? Or something on your player has one...
Add the blaster sound like this:
public AudioClip blasterSource;
Assign it in the inspector to the sound of the gun and then:
[RPC]
public void PlaySound() {
if(networkView.is$$anonymous$$ine == true) {
networkView.RPC("PlaySound", RPC$$anonymous$$ode.Others);
}
GetComponentInChildren<AudioSource>().PlayOneShot(blasterSound);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613073122im_/https://answers.unity.com/themes/thub/images/avi.jpg)