how to sync audio coming from instantiated game objects so all players can hear it?

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

how to sync audio coming from instantiated game objects so all players can hear it?

30werewoof
2022-04-07 22:01:36

hello, I'm sorry I know similar questions have been asked before but I couldn't find an answer that I understood. I have a game object that is instantiated by the player when the button r is pressed. the object has a rigidbody and gets thrown a short distance when instantiated (a script does this). I have a script on that object that plays a sound when it collides with a player with the tag "Monster". when it detects the collision, it is supposed to play a sound. the problem is, best I can figure out, only the player that instantiates the object can hear the sound when the game object is triggered, even if another player is the one who triggered it, but when I have a sound playing coming from the PLAYER in a loop all the players can hear it. does anyone know a way to make it so all players can hear the sound regardless of whether or not they instantiated the object?

Sound making device code: (sorry for the formatting i cant seem find a way to display it as code)

public class MonsterTrap : MonoBehaviour

{

public AudioSource audioSource;

[SerializeField]

private GameObject StunTrap;

[SerializeField]

private bool IsStunTrap = false;

[SerializeField]

private bool IsLazerTrap = false;

Rigidbody m_Rigidbody;

public int TrapThrowForce = 100;

void OnTriggerEnter(Collider other)

{

if (other.gameObject.CompareTag("Monster"))

{

if(IsStunTrap == true)

{

StunTrap.GetComponent().Play("Close");

}

if(IsLazerTrap == true)

{

Debug.Log("LoudNoise");

audioSource.Play();

}

}

}

void Start()

{

m_Rigidbody = GetComponent();

m_Rigidbody.AddForce(transform.forward * TrapThrowForce);

}

}

what i used to Instantiate:

if (Input.GetKey(KeyCode.R))

{

if (DelayTime == 0)

{

PhotonNetwork.Instantiate("LazerDetectTrap", transform.position, transform.rotation);

DelayTime = MaxDelay;

StartDelay = true;

}

}

Comments

Klover
2022-04-08 14:06:03

[PunRPC]

30werewoof
2022-04-09 20:20:21

hello Klover! :D I have tried RPCs and I am not sure if they accomplish what I want. most of my RPCs didn't work and gave me compile errors even when following a tutorial. (I'm probably just missing something) but the main thing is I don't know how to call them to specific objects like I would want the RPC only to trigger the noise on the object he collided with, not every object with the function on it. if there is a way to do this, would you please show me some tutorials or give me some example code? thank you for your time :D

30werewoof
2022-04-18 00:45:23

nevermind RPCs work fine

Back to top