- Home /
how to sync audio in instantiated object so all players can hear it?
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:
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<Animation>().Play("Close");
}
if(IsLazerTrap == true)
{
Debug.Log("LoudNoise");
audioSource.Play();
}
}
}
void Start()
{
m_Rigidbody = GetComponent<Rigidbody>();
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;
}
}