Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by PedroPat · Jun 10, 2017 at 01:06 PM · speedscorescore systemincreaseincrement

Increasing score based on speed

I am having trouble increasing score based on speed of the car with a few lines of code. I know how to do it with score vs time with

 yield return new WaitForSeconds(x);

, and I know how to this with 5 or 6 conditions and 20 lines of code ,but I wanted to do this with a few lines of code. It's possible ?.

What I want is:

 Score + = 1 point every 1 second if car speed = 1
 Score + = 1 point every 0.8 seconds if car speed = 3
 Score + = 1 point every 0.6 seconds if car speed = 6
 Score + = 1 point every 0.4 seconds if car speed = 9
 Score + = 1 point every 0.2 seconds if car speed> 9

I m using two Coroutines, one for score and another for speed, activated on Start function:

     private IEnumerator ChangeSpeed() 
     {
 
         while (true){
 
             yield return new WaitForSeconds(1);
 
                 if(speed < 2.0f || speed < 9.0f && gameStart){
                     speed += 3.0f; 
                 }
 
         
             if(speeding)
                 velocity = speed + 1f;
             else
                 velocity = speed;
         }
     }
 
 
 
 
     private IEnumerator ChangeScore() 
     {
         while (true){
 
             yield return new WaitForSeconds(1);
 
             if(gameStart){
 
                 scoreNum += 1;
 
                 scoreText.text = scoreNum.ToString();
             }
         }
     }



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

1 Reply

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

Answer by Major · Jun 10, 2017 at 07:12 PM

What if you plotted an equation that returned the time interval between each point assignment?

Your independent variable being speed (x-axis) and your dependent variable being the time interval (y-axis). If you plot the function

y = -0.1x + 1.1

you can return any time interval value you want between 1 and 9. Then you plug in this result into your WaitForSeconds to achieve the appropriate wait time. This could look something like this:

     float speed = Mathf.Truncate(objSpeed); //Edit: needed so that you always test an integer
     if (speed >= 1 && speed <= 9) {
         waitTime = -0.1 * speed + 1.1;
     } else if (speed > 9) {
         waitTime = 0.2f;
     } else if (speed < 1) {
         waitTime = Mathf.Infinity;
     }

By truncating the objSpeed and plugging in the result, you can setup these boundaries. It's also important to note that waitTime should be a global variable to ensure that it's value carries from previous frames.

I hope that this helps to answer your question. Sorry it was so long, but I wanted to try to build something from the ground up with an alternative approach. This code is untested, but I hope the theory at least comes across if nothing else. I hope this helps!

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 PedroPat · Jun 11, 2017 at 06:20 PM 0
Share

Worked very well. I only had to adjust values to achieve the desire effect.

Thanks! God bless you.

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

107 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 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 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 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 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

Increasing Enemy Speed 4 Answers

Increase variable? 0 Answers

How can I keep a score counter of how many points the player has collected on the screen? 1 Answer

Anyway To Set A Specific Part Of Code To A Scene? 2 Answers

UI printing in multiple sections.(not supposed to) c#(Solved) 1 Answer


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