2

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();
            }
        }
Jack Sebben
  • 69
  • 2
  • 9
  • 1
    In general: Don't use `GetComponent` repeatedly. Store the reference **once** in `Awake` to a class field and then reuse it later. – derHugo Jan 19 '20 at 10:02
  • 1
    Will it still play twice if you also remove this script from one of the players? – Iggy Jan 19 '20 at 13:18
  • 1
    Also, speculation, if you run two instances on the game on the same device, of course, you will hear two sounds from each game window. But are you seeing this issue on different devices? – Iggy Jan 19 '20 at 13:22
  • @Iggy Even if I disable the player script and audio source of the remote player (and disable the volume for the standalone exe), the sound will still play twice. I haven't tested it on different devices but I'm not sure if having two instances is the problem. (However I will still test that when I can.) – Jack Sebben Jan 19 '20 at 19:56
  • How does PlayFootstep get called? – Aleksandr Stepanov Jan 21 '20 at 02:25
  • @AleksandrStepanov animation event – Jack Sebben Jan 21 '20 at 05:05

1 Answers1

2

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.

  • This works, but now footstep sounds don't play on remote clients, only on local. – Jack Sebben Jan 23 '20 at 05:49
  • @Alexsandr Stepanov the function is called with an animation event `public void PlayFootstep() { int clipPick = Random.Range(0, footstepArray.Length); myASource.clip = footstepArray[clipPick]; if (myASource.isActiveAndEnabled && GetComponent().ySpeed > 1.15) { myASource.Play(); } }` – Jack Sebben Jan 24 '20 at 04:22
  • 1
    Does PlayerMovement work correctly on remote clients? Specifically, is ySpeed updated properly? – Aleksandr Stepanov Jan 24 '20 at 08:20
  • 1
    In any case, you should put Debug.Log statements everywhere, at start of PlayFootstep, and inside if block, also output the gameObject.name, to see which client is it.. – Aleksandr Stepanov Jan 24 '20 at 08:23
  • Thank you for the help I figured it out. I decided to have the local function like in my previous comment, but I also added an RPC with an RpcTarget of Others. This fixed my issues, thank you for your help! – Jack Sebben Jan 26 '20 at 03:14