- Home /
Question by
mogren3000 · Sep 07, 2016 at 12:47 PM ·
camerascript.vrsound
Sound OnTriggerEvent with HTC Vive Camera (head) wont play.
Have a BIG problem I whould say. I want to play a sound when the MainCamera of the HTC Vive [CameraRig] enter my trigger that is a CUBE.
So this is how I have set it up. I have a CUBE that is set is trigger*. The [CameraRig]** has a AudioListener and the CUBE has a AudioSource and my Script is attached to the CUBE.
The MainCamera of the [CameraRig] is Camera (head) and it has a Rigidbody attach that is set to is kinematic.
So that what the setup looks like and here is the script:
using UnityEngine;
using System.Collections;
public class PlaySound : MonoBehaviour {
public AudioClip soundToPlay;
public float volume;
AudioSource audio;
public bool played = false;
// Use this for initialization
void Start () {
audio = GetComponent<AudioSource>();
}
void OnTriggerEnter() {
if (!played) {
audio.PlayOneShot (soundToPlay, volume);
played = true;
}
}
// Update is called once per frame
void Update () {
}
}
When the Camera (head) collide with the CUBE nothing happen. Why is this?
Comment
Answer by JEFFDOG11111 · Sep 07, 2016 at 09:36 PM
using UnityEngine;
using System.Collections;
[RequireComponent ( typeof(AudioSource))]
public class PlaySound : MonoBehaviour
{
//-----------[Variables]-------------//
public AudioClip soundToPlay;
public float volume;
public bool played = false;
//-----------------------------------//
void Start ()
{
this.GetComponent<AudioSource>().clip = soundToPlay;
this.GetComponent<AudioSource> ().volume = volume;
}
// Make sure your camera or your camera's parent has a collider...
// Make sure Box collider has the trigger check marked on ckecked...
// MAke Sure This Script is attached to the Cube only...
// Make sure to take frequent brakes when programming. in helps calm you to make you think better..
void OnTriggerEnter()
{
if (!played)
{
this.GetComponent<AudioSource> ().Play ();
played = true;
}
}
}