Playing audio when the user is dancing / moving a VR (GEAR) Controller
HI, I am creating an interactive VR (GEAR) experience. Using Unity 2018.2.6f1.
The user is encouraged to dance along with the characters and if the user is actually moving, positive random audio is to be played.
I have setup a random number generator to randomly pick an audio clip from an array. This is working upon play (Print string through the console), but does not play audio through my OVRCameraRig AudioSource. (I have music playing through the OVRCameraRig AudioSource so i know it is working).
I am tracking if the controller is moving / user is dancing and if so to play the audio clip.
I have struggled with this for weeks and would appreciate any help, Thank you.
My code is as follows -
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Controller_TriggerAudio : MonoBehaviour {
public AudioClip[] audioArray;
AudioSource audioSource;
AudioClip currentAudio;
int index;
private bool canPlay = true;
void Start()
{
index = Random.Range (0, audioArray.Length);
currentAudio = audioArray[index];
print (currentAudio.name);
}
private void Update()
{
var forward = transform.TransformDirection(Vector3.forward);
var toOther = Vector3.up;
//gets the angle between the controller's forward vector and the upwards vector (0,1,0) and
returns it as a value between -1 and 1.
//-1 means you're pointing directly away from the target and 1 means you're pointing directly
at it
var dotProduct = Vector3.Dot(forward, toOther);
//fires the sound and makes it so that canPlay=false preventing it from firing again
if (dotProduct > 0.95f && canPlay)
{
canPlay = false;
currentAudio = audioArray[Random.Range(0, audioArray.Length)];
AudioSource.PlayClipAtPoint(currentAudio, new Vector3(5, 1, 2));
}
else if (dotProduct < 0.35f && canPlay == false)
{
canPlay = true;
}
}
}