Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by byurocks23 · Mar 21, 2014 at 04:12 AM · javascriptaudioscoreincrement

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image byurocks23 · Mar 21, 2014 at 05:23 AM 0
Share

It was a bit troubling to convert to JavaScript, but it worked great. Thank You

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Have readout of score at certain increments (JavaScript) 1 Answer

Play music trough scenes 0 Answers

Quick way to enter and condense repeated lines of code 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges