- Home /
How to activate a sound when a player walks through a collider
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?
Answer by clunk47 · Dec 13, 2012 at 06:54 PM
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 AUDIO 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);
}
}
Because you didn't define a class. $$anonymous$$y example wasn't just for copy and paste lol. This is C#, you need to know a bit of C# to do it.
using UnityEngine;
using System.Collections;
public class audioTriggerTest : $$anonymous$$onoBehaviour
{
void OnTriggerEnter(Collider coll)
{
if(coll.gameObject.tag == "Player")
audio.PlayOneShot(audio.clip, 1.0f);
}
}
Note the class name "audioTriggerTest", you can name whatever you want, then you must name the script file the same as your class or it won't work. Note this C# Script derives from $$anonymous$$onoBehaviour.
oh sorry, my bad i know nothing of C# just a bit of java. This may sound stupid but it says that the mono script is missing, what exactly is this? And also it says that 'Type 'UnityEngine.GameObject' does not contain a definition fo 'Tag' and no extension method 'Tag' of type 'UnityEngine.GameObject' could not be found' thanks, i appreciate your help!
That's what PlayOneShot means. Take a look at the script ref I posted in my original answer. Well your original question isn't very specific... Do you want the audio to play only once, period? Or each time the player interacts? If only ONCE throught the whole level, Destroy the script after it has been used.
if(coll.gameObject.tag == "Player")
{
if(!audio.isPlaying)
{
audio.Play();
Destroy(this);
}
}
Notice I didn't use PlayOneShot this time, I just checked if audio was playing. "!" means not. So this declares that if the audio is NOT playing, then Play it. Then destroy the script. You can also replace Play(); with PlayOneShot(audio.clip, 1.0f);, but I don't know exactly what you want. Experiment around with and without destroying the script, Play and PlayOneShot, checking if audio is already playing, etc....