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

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.

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();
        }
    }
}

Hola!
i have the problem, that trigger only happens when I enter the trigger box by using continuous motion or teletransport, but not when I actually walk into it walking in real world.

How to trigger it when the player walks in real space (and gets inside the trigger box in VR)

Thanks in advance!