I have an area that when a player enters the collider it will play a sound. Because im new to unity i have no idea how to code so what would i put and how would i make the sound only go off once?
To have the audio only play once, use PlayOneShot(clip, volume) I have an example below. Volume ranges from 0.0 to 1.0, which makes is a float, so you use 0.0f to 1.0f. This is a C# example btw. You may also only want the audio to play if it is not already playing, so you don’t get a bunch of the same sound playing over itself. To do this, you would use if(!audio.isPlaying) the “!” means “not”. You could use if(!audio.isPlaying) audio.Play(); and it will still only play once per collision, if it’s not already playing. This might not make sense if you don’t know how to code at all, take a look at the script reference for 1 for more information.
If the area is a TRIGGER:
void OnTriggerEnter(Collider coll)
{
if(coll.gameObject.Tag == "Player")
audio.PlayOneShot(audio.clip, 1.0f);
}
If the area is NOT a Trigger, but a physical collider:
void OnCollisionEnter(Collision coll)
{
if(coll.gameObject.tag == "Player")
audio.PlayOneShot(audio.clip, 1.0f);
}
OR you can just use Play(); But first, make sure audio is not already playing. If you want this script to only work once, Destroy it after trigger.
if(coll.gameObject.tag == "Player")
{
if(!audio.isPlaying)
{
audio.Play();
Destroy(this);
}
}