Hello , folks , I made a box that opens with the entry of a password, one audio starts with the opening . So far everything works perfect, but the audio is looping , and I want it to stop at the end of opening the box. Where should I place the command line to stop the audio ? Thank you all .
Here’s the code
var close = false;
var CodigoBau : String = "";
var stringToEdit : String;
//var estiloAviso = GUIStyle;
var AngleX : float = 0.0;
var AngleY : float = 0.0;
var AngleZ : float = 0.0;
private var targetValue : float = 0.0;
private var valorAtual : float = 0.0;
private var easing : float = 0.003;
var Sound:AudioClip;
var Target : GameObject;
function SetTheGUI () {
close = true;
targetValue = AngleX;
//valorAtual = 0.0;
}
function UnSetGUI () {
close = false;
}
function OnGUI(){
if (close){
GUI.Box (Rect(Screen.width/2-60, Screen.height/2-80, 150, 35),"DIGITE O CÓDIGO");
stringToEdit = GUI.TextField (Rect(Screen.width/2-60, Screen.height/2-40, 150, 25),stringToEdit, 11);
if(stringToEdit == CodigoBau){
audio.PlayOneShot(Sound);
valorAtual = valorAtual + (targetValue - valorAtual) * easing;
//GameObject.particleEmitter.emit = true;
Target.transform.rotation = Quaternion.identity;
Target.transform.Rotate (valorAtual , 0, 0);
}
}
}
I think you should modify the logic: when the password is ok, start the sound and set a flag (opening, for instance) that will make the box start opening at Update; when valorAtual gets close enough to targetValue, stop the sound and the opening:
...
var speed: float = 5; // open speed
var opening = false; // opens the box when true
function Update(){
if (opening){
// this does the same as your code, but is much less framerate dependent:
valorAtual = Mathf.Lerp(valorAtual, targetValue, speed * Time.deltaTime);
// easier way to set the angle around X:
Target.transform.eulerAngles = Vector3(valorAtual, 0, 0);
// when valorAtual is close to targetValue (0.5 degree)...
if (Mathf.Abs(valorAtual-targetValue) < 0.5){
opening = false; // end opening...
audio.Stop(); // and stop sound
}
}
}
function OnGUI(){
if (close){
GUI.Box (Rect(Screen.width/2-60, Screen.height/2-80, 150, 35),"DIGITE O CÓDIGO");
stringToEdit = GUI.TextField (Rect(Screen.width/2-60, Screen.height/2-40, 150, 25),stringToEdit, 11);
if(stringToEdit == CodigoBau){ // when password is ok...
close = false; // close GUI...
audio.clip = Sound; // start the sound...
audio.Play();
opening = true; // and start opening
}
}
NOTE: You may have to adjust things to make the sound match the opening time. If the sound is loopable, just check Loop at the Inspector or set audio.loop = true. If the sound isn’t loopable, you can adjust the variable speed to open the box during the sound duration.