- Home /
Correct code for triggering audio source by walking through box colliders (I'm using Oculus Native Spatialiser)
Hello,
I am aware variations on this question has been asked and answered numerous times, and I have spent the past 24 hours trying to make the answers solve my problem but to no avail, so I'm asking my own question with the hope you guys can help me solve it
I am doing resit coursework for 2nd year of my degree and my project is due in a few days so would be really grateful for some help on this.
I am populating a game level with various one shot SFX attached to numerous box colliders with audio source components on them. I want my player to trigger the audio clip attached to the audio source whenever I walk through the box collider. I am using the Oculus Native Spatialiser so I fear the code may have to include reference to this fact but I'm not sure as all I want the code to do is trigger the audio clip attached to the audio source to play.
I'm using Unity 5.2.1f1
Many thanks for your help,
Patrick
After finding some answers, I have added this script to a box collider (with audio source attached as well as the Oculus ONSP audio source script) but it doesn't work
using UnityEngine;
using System.Collections;
public class playclip1 : $$anonymous$$onoBehaviour {
public AudioSource source;
public void OntriggerEnter(Collider player) {
source.Play ();
}
}
I have also tried this js script, again to no avail`var soundFile:AudioClip;
function OnTriggerEnter(trigger:Collider) {
if(trigger.collider.tag=="Player") {
audio.clip = soundFile;
audio.Play();
}
}`
Hi @crazy$$anonymous$$night or @$$anonymous$$ikeNewall, would you be able to shed some light on this? Thanks, Patch
Answer by Patchbristol · Aug 13, 2016 at 07:21 PM
Someone helped me and made a working code.
For anyone interested I hope this helps.
using UnityEngine;
using System.Collections;
public class PlaySoundOnCollision : MonoBehaviour
{
[SerializeField]
private AudioClip[] _audioClip;
private AudioSource _audioSource;
void Start ()
{
_audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
_audioSource.Play();
}
}
}
You just need to change the public class name on line 4 to what ever your script is called. So ins$$anonymous$$d of, '.....PlaySoundOnCollision.....' - replace that with your script name.
Answer by FabriBertani · Aug 12, 2016 at 09:44 AM
Try this (is c#):
using UnityEngine;
using System.Collections;
public class playclip : MonoBehaviour {
public AudioClip yourAudioClip;
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "Player") {
GetComponent<AudioSource> ().clip = yourAudioClip;
GetComponent<AudioSource> ().Play ();
}
}
}
And you must drag your audioclip to script component in the inspector. Hope it help.
Hi Fabri, many thanks for the code. It's giving me an error which you can see in the picture
I've added the cs script to my box collider, checked 'is trigger', and added an audio source to the box collider with the audio clip added.
In the script, I changed the public class from playclip to playclip3 as that is what the script is called, I've changed line 5 'yourAudioClip' to 'siren1', and I've changed line 9 yourAudioClip' to 'siren1' also.
Thanks,
Patch!
The 3rd error in the console is related to a door and is unrelated to my problem
I got it to work.. I made a new script and changed the name to oneshot, whilst also changing the public class in the script to match - perhaps it didn't like the number part of the old name?
I put my SFX into the Your Audio Clip box and it played correctly, I'm very happy but wonder if in doing so this negates all of the functions of the Audio Source component? When I adjust the pitch or volume in the Audio Source for example the sound no longer plays. I need to do things such as send the sound to a mixer, change pitch, adjust the spatial blend
Is there a way to just reference the AudioClip in the Audio Source?
$$anonymous$$any thanks