- Home /
How to get sound to play on certain condition (javascript)
What I want to happen is whenever the score hits a certain amount, I want a sound to play. This is what I have:
var distanceScore: float = 0;
function score(){
distanceScore += (Variables.actualFps + 88) / 5280;
}
function Start () {
InvokeRepeating("score",1,1);
}
Every time distanceScore goes up by 0.1, i want it to play a specific sound to that number (I already know how to play a sound so I don't need help with that). For example, when distanceScore = 0.1, I want it to play a sound. When it equals 0.2, I want it to play a seperate sound, when it equals 0.3, I want to play an even separate sound etc. How can I do this?
Answer by RyanZimmerman87 · Mar 21, 2014 at 04:39 AM
One semi easy way to do it as far as understanding the logic would be to set up a float variable which corresponds to the score. Then every time you call the score function you can use the float variable to determine the condition checks.
Maybe something like this (in C#):
float scoreFloat;
void Start()
{
scoreFloat = 0;
}
void scoreFunction()
{
//calculate score
distanceScore += (Variables.actualFps + 88) / 5280;
//first score condition for sound 1
if (scoreFloat <= 0)
{
if (distanceScore >= .1f)
{
//set scoreFloat this corresponds to the .1f
scoreFloat = 1;
//call sound function
soundFunction();
}
}
//all other score conditions for all other sounds
else if (scoreFloat >= 1)
{
//you already got the distanceScore above
//create a new variable for your condition check
float newScoreFloat = .1f + (scoreFloat/10);
if (distanceScore >= newScoreFloat)
{
//set scoreFloat to use in the next scoreFunction()
scoreFloat += 1;
//call sound function
soundFunction();
}
}
}
//sound function
void soundFunction()
{
//determine which sound to play based on the scoreFloat
if (scoreFloat == 1)
{
//play sound 1
}
else if (scoreFloat == 2)
{
//play sound 2
}
else if (scoreFloat == 3)
{
//play sound 3
}
//etc
}
I would try something like this, hope it helps. This would only work though if it was impossible to go up two tiers of score sounds between just one scoreFunction()
Also sorry about the C# I never used JavaScript so wanted to make sure I didn't do something stupid in this example.
I think the logic could be improved a little but should be a pretty clear example.
It was a bit troubling to convert to JavaScript, but it worked great. Thank You
Answer by Foestar · Mar 21, 2014 at 05:10 AM
So you're trying to play a separate sound for each time the score goes up? That's gonna require a ton of separate sounds. But if this is what you really want to do then just create a variable and check it before playing the sound. Depending on the returned results play the desired sound. All in an if statement.
So for example:
var soundplayed : float = 0;
private var score : float =0;
//enter your function here that will increase score and sound
function scored () {
score ++;
soundplayed++;
//or soundplayed=soundplayed+1;
if (soundplayed=1){
audio.Play();
}else if(soundplayed=2){
audio.Play();
}
}
The reason I would base the audio after a separate variable is because if you wanted to reset the sounds back to the beginning for some reason like if it was based on the score within a time limit like a combo. This way you are still improving your variable score but also dealing with the sound within the same function.