Reputation: 69
I have animation events linked up to my characters that play a random footstep noise when they step. I'm trying to set it up for multiplayer, but I'm having some issues. With one person, the sounds only play once when they're supposed to. However, as tested with 2 people, it plays each footstep twice at the same time when one player steps. Each player has an audiosource component. Both footsteps sounds come from the audiosource of the player running, so it's not a case of both players playing the same sound. Any ideas as to why the sound is duped and played at the same time? The double sound comes from the same client, but only when that client is in multiplayer. And it's not when other people are walking, only the client. I must be setting up something wrong or putting something in the wrong place with my RPC.
1 player with 1 audiosource: sounds plays once
2 players with their own audiosource: sounds duplicates and plays at the same time
2 players with audiosource enabled for only the one walking: sounds still plays twice
From my player code
public void PlayFootstep()
{
int clipPick = Random.Range(0, footstepArray.Length);
GetComponent<AudioSource>().clip = footstepArray[clipPick];
photonView.RPC("PlayFootstepRPC", RpcTarget.All);
}
[PunRPC]
private void PlayFootstepRPC()
{
if (GetComponent<AudioSource>().isActiveAndEnabled && GetComponent<PlayerMovement>().ySpeed > 1.15)
{
GetComponent<AudioSource>().Play();
}
}
Upvotes: 2
Views: 1117
Reputation: 239
If PlayFootstep is called via an animation event, and you have animations synchronized via PhotonAnimatorView, then the PlayFootstepRPC() gets called several times, once per each connected client.
PhotonAnimatorView makes an object to play the same animations on every client. The PlayFootstep function gets called on every client, and every client sends RPC to itself and other clients, and that RPC plays the sound.
I suggest you should either not play footstep sounds via RPC, playing it locally instead (because animation event handles it for you), or add a check of PhotonView.IsMine before calling an RPC.
Upvotes: 2