0
\$\begingroup\$

I'm trying to play sound on other clients, so they could hear gunfire in the distance, but sound only works for the shooter. Here's my code:

    void Update()
    {
        ***

        if (Input.GetButton("Fire1"))
        {
            CmdFireWeapon(this.gameObject);
        }

        ***
    }

    [Command]
    void CmdFireWeapon(GameObject plr)
    {
       ***        

        if (!plr.GetComponent<AudioSource>().isPlaying)
            RpcPlayWeapon(plr);

       ***
    }

    [ClientRpc]
    void RpcPlayWeapon(GameObject plr)
    {
        plr.GetComponent<AudioSource>().Play();
    }

I'm using 3D Spatial Blend, but there's no sound even with volume == 1 across all distance. Also tried with and without if(isPlaying) - no progress. What am I doing wrong?

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

I think I figured it out. Player Prefab on other clients spawned without clip (it is loaded in its script). So I needed to load it first then play it. And I need to keep loading, because clip changes with different weapon.

[ClientRpc]
    void RpcPlayWeapon(GameObject plr)
    {
            plr.GetComponent<AudioSource>().clip = 
                    plr.GetComponent<PlrController>().curWeapon.shotAudio;
            plr.GetComponent<AudioSource>().Play();
    }
\$\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.