• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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 do you add +1 to a score counter after a destroy script (C#)? 1 Answer

Facebook score API not saving score for public user 1 Answer

score not increasing and staying at 1 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges