- Home /
VR Lever to play sound between two values doesnt work ,
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
Answer by Legend_Bacon · Jan 30, 2018 at 03:36 PM
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
Answer by DexiTheZombie · Jan 30, 2018 at 03:59 PM
@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
Hello there,
No worries, I'm here to help. The link I sent you above should have been the right solution, but maybe something went wrong. Could you post your code again? If by any chance you put an ELSE statement saying TardisSource.Stop ()
, then that would definitely be the cause.
Cheers,
~LegendBacon
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LeverSoundTesting : $$anonymous$$onoBehaviour {
AudioSource TardisSource;
public AudioClip TardisClip;
// Use this for initialization
void Start () {
TardisSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update () {
if (!TardisSource.isPlaying)
{
TardisSource.clip = TardisClip;
TardisSource.Play();
}
}
}
Now what happens is it just instantly plays the clip
Ah, I see. Sorry, I might not have been very clear before.
What I meant to tell is that you need to ADD this solution to your existing code, not REPLACE it.
In the code above, every frame (60 times a second), your script checks if the sound is playing. And if it isn't, then it plays it. What you actually want to have is the check you originally had:
if (gameObject.transform.localEulerAngles.x >= 80 && gameObject.transform.localEulerAngles.x <= 90)
{
if (!TardisSource.isPlaying)
{
TardisSource.clip = TardisClip;
TardisSource.Play();
}
}
Now, this assumes that your lever will NOT fit the angle conditions by the time the sound is not playing anymore. Otherwise, it might play multiple times as long as the conditions are true. You could implement a bool check, eg "hasPlayedSound", to fix this. It all depends on the behaviour you would like for your game.
I hope that helps!
Cheers,
~LegendBacon