Hello im kinda new to unity and am making a TardisVR game however im now setting up the scripts for my levers to play sound im using the below script to play a sound when the lever rot is between 85 and 90 but being new i dont think ive wrote it right
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MatSound : MonoBehaviour {
public AudioClip TardisClip;
public AudioSource TardisSource;
// Use this for initialization
void Start () {
TardisSource.clip = TardisClip;
}
// Update is called once per frame
void Update () {
if (gameObject.transform.localEulerAngles.x >= 80 && gameObject.transform.localEulerAngles.x <= 90) {
TardisSource.Play ();
}
}
}
The assets im using are
Oculus SDK
VRTK Toolkit
please if anyone can help i would really appreciate it
Hello there,
You could probably start by adding Debug.Log("X = " + transform.localEulerAngles.x.ToString());
in your update, just to make sure you are indeed between the correct values.
Other than that, be careful about playing an audio source on Update(). It means that it will try playing the sound every frame (60 times a second). This might also be your problem. I would recommend using something like THIS to make sure that doesn’t happen.
I hope that helps!
Cheers,
~LegendBacon
@Legend_Bacon Hey yea ive got it working now apart from when the lever is no longer in those values it instantly stops the audio, is there a way where i can make it so when the lever has entered the value range it starts when even if the lever moves out of that range it doesnt stop and also if the lever were to enter that range mid audio playing that it wouldnt play it again ? sorry if this is a lot