- Home /
How to Network Sound?
EDIT: Does anyone know of any mp unity open source projects that have mp sound that I could check out? Kinda needing some reference.
Right now I'm making a multiplayer death-match game. So far I have a lot of stuff networked like damage, respawing. But I'm having a problem with sound. Right now I have a prefab player that spawns into the game when he joins a server. Everyone uses this same prefab and it clones. What I'm trying to do is when a player shoots his gun a sound rpc goes to the other players. I'm not quite sure how to set this up though. Because they both have the same gameobject that controls their sounds. When A shoots, B hears HIS selected gun and HIS soundsource holder. But I want A to shoot and B to hear it from A's sound source, not B. Currently when A shoots, B hears the shot from B. Not A. How would I set up the rpc to make the sound ON the person that is shooting so the other player can hear that sound coming from where the other person is shooting.
Here is my code so far.
using UnityEngine;
using System.Collections;
public class GunfireSFX : MonoBehaviour
{
public AudioSource aS;
public AudioClip pistolShot;
public AudioClip revolverShot;
public GameObject pistolActive;
public GameObject revolverActive;
// Detemine which gun is equiped and change the sound source to that
void Awake()
{
}
void Update()
{
CheckEquipedGun();
GunFireSFXCallRPC();
}
public void CheckEquipedGun()
{
if (networkView.isMine)
{
if (pistolActive.activeInHierarchy == true)
{
aS.clip = pistolShot;
}
if (revolverActive.activeInHierarchy == true)
{
aS.clip = revolverShot;
}
}
}
public void GunFireSFXCallRPC()
{
if (Input.GetMouseButtonUp(0))
{
networkView.RPC("MPPlayFireSound", RPCMode.Others);
}
}
// Play that soundsource as RPC.
[RPC]
public void MPPlayFireSound()
{
print(aS.clip.name);
aS.Play();
print("RPC Played GunFire");
}
}
That just creates null references. I need to somehow send the RPC FRO$$anonymous$$ A, to B. But its just going to B's soundsource, thus the null reference if i have enabled false if its not $$anonymous$$e.
Yes, of course you need to valuate the vars you will need on RPC calls before disabling the script :)
and what do you mean by valuate the vars before disabling the script. if the vars are in the script and network view isn't $$anonymous$$e they instantly are null.
In the awake method, you disable the script with :
if (!networkView.is$$anonymous$$ine)
{
this.enabled = false;
return;
}
If you can't valuate the aS var before (like here because you need to know what weapon has the other player), you need to pass the information of what clip should be played into the RPC call. So maybe you can declare an enum containing all sorts of shots you can play : pistolShot, revolverShot, ... and then in the RPC call you pass an int corresponding to the shot you want to play. This way the other player knows what weapon the 1st player shoots with.
With this, the Update method is never called, because it is disabled, but the RPC calls are still made, and if enough information is passed threw the RPC call, it will work.
Answer by KiraSensei · Oct 04, 2013 at 12:19 PM
I'm doing the same on my game, but on the Awake() method I check if the network view is mine, if not, I disable the script. Try that for a start.
Your answer
Follow this Question
Related Questions
Problem with identifying players in network 1 Answer
Problem with RPC life 0 Answers
Transfering vars to RPCs? 0 Answers
Playing Audio Over Network -1 Answers