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 19, 2014 at 06:35 PM · javascriptmathendless runner

Increase speed of vehicle - math question (javascript)

I have an endless runner game that I want to increase with speed as the game goes on. I want it to increase at a quicker rate at the beginning and a slower rate as time goes on. I figured a square root function would accomplish this but I realize it doesn't. For example:

  speed = 10
  function gameSpeed(){
             speed = Mathf.Sqrt(speed);
             Debug.Log(speed);
         }
         
         InvokeRepeating("gameSpeed",0,1);
 
    

When I run this it prints: 3.16, 1.77, 1.33, etc. It does what I want except it gets smaller and smaller.

I also thought of a square function such as:

 speed = 10
 function gameSpeed(){
     speed = Mathf.Pow(speed, 2);
     Debug.Log(speed);
 }
 
 InvokeRepeating("gameSpeed",0,1);

And when I run this one it prints: 100, 1000, 100000000, etc. This one is at least increasing, except it is increasing at a slower rate at the beginning and a faster rate as more time goes on, opposite of what I want.

I just need the parent function of the equation, I know how to modify it to increase at the rates I want.

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

3 Replies

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

Answer by byurocks23 · Mar 19, 2014 at 07:09 PM

I found my answer:

 speed = 10
 function gameSpeed(){
     speed += 100 / speed;
     Debug.Log(speed);
 }
  
 InvokeRepeating("gameSpeed",0,1);
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
avatar image
0

Answer by SirCrazyNugget · Mar 19, 2014 at 06:42 PM

Why not just use a variable that increases by timeDelta then multiple that by a constant to get the rate of increase you require?

 var speed = 10;
 function gameSpeed(){
     speed += Time.deltaTime * 0.1;
 }
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
avatar image
0

Answer by kyzk · Mar 19, 2014 at 08:24 PM

In the "speed = Mathf.Sqrt(speed)" your speed variable is recursively defined in terms of its previous value, so speed becomes smaller EVERY second. Instead you need to take the square root of the time elapsed since the game started, as well as the starting speed. Here is a plot of what you have. alt text

So instead you need to something like

      startSpeed = 10
      speed = 10
      time = 0                
      
      function gameSpeed(){
         speed = startSpeed + Mathf.Sqrt(time);      
         time++;         //Since this function is only called every second.
         Debug.Log(speed);
      }
      InvokeRepeating("gameSpeed",0,1);

Which will generate a speed plot that looks like alt text


untitled.png (11.8 kB)
des.png (12.3 kB)
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

21 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

Related Questions

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

Setting Scroll View Width GUILayout 1 Answer

Calculations Wrong? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

A counting script? 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