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
1
Question by NoTakeful · Jun 24, 2014 at 10:10 PM · timespeed upover

Adding 1 to a float over time to increase difficulty?

so I have an object that moves back and forth at a speed and will change directions at random times, but I want it to increase in speed over time and increase from 1 to 16 in 96 seconds (or 1 every 6 seconds). Here's my script:

 var blockSpeed : float = 1;
 
 function Awake()
 {
     rigidbody.AddForce(blockSpeed, 0, 0, ForceMode.Impulse);
 }
 
 Invoke( "NewFunDirection", Random.Range(0.5,0.8)  );
  
 function NewFunDirection()
  {
  rigidbody.velocity = -rigidbody.velocity;
  Invoke( "NewFunDirection", Random.Range(0.5,1.4)  );
  }
  

I tried something like this but i'm unaware of how to format it

 function Update(){
     yield WaitForSeconds (6);
     if (blockSpeed <= 16){
          blockSpeed =+1;
     }

When I use this last bit of coding Unity tells me it the + is an unexpected token.

Comment
Add comment · Show 1
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 MikeNewall · Jun 24, 2014 at 10:39 PM 0
Share

That code increases the speed every 6 seconds so the speed increases in steps. Is that what you want or do you want a smooth ramp in speed?

3 Replies

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

Answer by Bunny83 · Jun 25, 2014 at 12:39 AM

First of all you only apply a force in Awake. So changing "blockSpeed" afterwards doesn't do anything to the velocity of the rigidbody.

Apart from the fact that changing blockSpeed doesn't do anything, if you want to gradually increase the blockSpeed by 1 every 6 seconds you can do this:

 function Update()
 {
     if (blockSpeed <= 16)
     {
         blockSpeed += Time.deltaTime / 6;
     }
 }

This will add a small fraction each frame so it gradually increases. Without the "/6" it would increase by 1 every second. By dividing by 6 the increase is 6 times slower, so it takes 6 seconds to increase blockSpeed by 1.

ps: If you want to set the speed of your rigidbody you can do this:

 function Update()
 {
     if (blockSpeed <= 16)
     {
         blockSpeed += Time.deltaTime / 6;
         var dir = rigidbody.velocity.normalized;
         rigidbody.velocity = dir * blockSpeed;
     }
 }
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 NoTakeful · Jun 25, 2014 at 04:30 AM 0
Share

Thank you, works perfectly! And smoother than it would be automatically changing the speed every 6 seconds. Boneheaded mistake on my part not realizing it was in the awake function haha.

avatar image
1

Answer by Okari-Draconis · Jun 24, 2014 at 10:41 PM

Dont use update. From ur awake or start function do an InvokeRepeating(6,6,"increaseDifficulty"), make sure u have a function called increaseDifficulty() and put ur float++ in there

Comment
Add comment · Show 2 · 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 jefjohms · Jun 24, 2014 at 10:44 PM 0
Share

I totally missed the yield return in the Update() function. duh.

WaitForSeconds() can only be used in coroutines.

avatar image NoTakeful · Jun 25, 2014 at 12:16 AM 0
Share

Thank you, that is probably a better way to do it! Unity is giving me an error though because apparently InvokeRepeating is supposed to be (string, float, float) and not (int, int, string). Why a float if it's function is to wait for seconds?

avatar image
0

Answer by jefjohms · Jun 24, 2014 at 10:30 PM

put the '+' before the equals. This is shorthand for: blockSpeed = blockSpeed + 1; Your line says blockSpeed is equal to +1;

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 NoTakeful · Jun 24, 2014 at 10:37 PM 0
Share

I tried it, and it doesn't turn an error now, but it doesn't increase the float speed of the block.

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

24 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

Related Questions

Increase speed over time 0 Answers

Instantiate objects faster and faster over game time,Instantiate objects faster and faster over time 1 Answer

how do i control Time.delatTime 2 Answers

script not working :( 2 Answers

How would I decrease a variable over time based on distance? 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