Hi.i wanna play a sound once when the scale of an object reaches to a special number.but when the scale reaches,it wont stop playing until i change the scale. my code is in Update function too!what should i do for my problem?
its kind of slider menu
I want forexample when number 6 reaches that scale,play a sound just ONCE.
[97227-s.png~~|97227]
Here is my code :
for (int s=0; s<LevelThumbnails.Count; s++) {
for (int t=0; t<LevelThumbnails.Count; t++) {
if (HorizontalScrollBar.GetComponent<Scrollbar> ().value > (ScrollSteps / 2) + (s - 1) * (ScrollSteps) && HorizontalScrollBar.GetComponent<Scrollbar> ().value <= Mathf.Clamp (ScrollSteps / 2 + s * ScrollSteps, 0, 1)) {
if (t!=s ){
LevelThumbnails [t].transform.localScale = Vector2.Lerp (LevelThumbnails [t].transform.localScale, new Vector2 (1, 1), Transition_Out);
}
if(t==s){
LevelThumbnails <strike>.transform.localScale = Vector2.Lerp (LevelThumbnails ~~.transform.localScale, new Vector2 (Element_Scale, Element_Scale), Transition_In);~~</strike>
Well with out any code to go off by its hard to tell. Its probably just a case of you having your if statement in an update function and having it play the sound once the object scales to the target scale. You need to have some kind of bool that evaluates to true once youve finished scaling. Have the if(the scale has reached its targt && scaled == false) and then have the code for calling the sound and then evaluate the scaled to true so it wont loop. If that dosent work make sure whatever you are using to emit the sound isnt on a loop.
Vector3 triggerScale; // The scale at which the sound must play.
bool soundPlayed; // A flag to keep track of if the sound has playe or not.
AudioSource sound; // The AudioSource having the sound you want to play.
And you make sure everything is initialized properly.
Since you have provided no code, I can’t integrate my example with what you have done. This is up to you and shouldn’t be difficult.
AudioSource as;
int scaleValue;
int scaleValueOld = 0;
void Start () {
as = GetComponent ();
}
void Update () {
// Get the current scale value
scaleValue = FunctionToGetScaleValue (); // < ----------- Write yourself
// The new play sound function
if (TheCorrectTimeScale ) {
if (scaleValue != scaleValueOld) {
as.PlayOneShot(as.clip);
scaleValueOld = scaleValue;
}
}
}
}