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 Frix · Jul 28, 2016 at 03:37 PM · time

Add x amount in t seconds?

I feel a bit silly because I've done this before, but my mind is blank right now.

How do you add x amount to a variable in t seconds?

Say, 30 in 1 second. Or 70 in 1 second. Or 10 in 2 seconds. Or 150 in 2 seconds. So regardless of the number added, it'll scale in that same timeframe.

Edit: Hopefully this is a better explanation.

Say I have 100 points, and I gain a pick up. I want to add 500 points to my 100 points incremently over 1 second.

I then have 600 points and gain a pick up which gives me 1300 points - which would add incremently to my 600 over 1 second.

Sorry, my explanations are so bad but I hope that covers it! Thanks.

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
3
Best Answer

Answer by Bunny83 · Jul 28, 2016 at 04:54 PM

If you add something every frame you just have to multiply the desired "amount per sec" by Time.deltaTime. Of course that only works properly with floating point numbers since integers can only hold whole numbers so you can't add a fraction.

float someVar = 0;

void Update() { someVar += 50 * Time.deltaTime; }

This will add "50" every second in a continous way. So each frame you add a fraction of the whole number. At about 60fps after the first frame someVar would be "0.833". The second frame it would be "1.66" and after 60 frames it's about "50.0". After 120 frame (2 seconds) it's "100.0"

If you talk about integer variables you might want to look at InvokeRepeating like @Search said. However keep in mind that would increment the variable only once every second so after a second it jumps from 0 to 50 or whatever you add.

edit
So if you want to add a fix amout only once over a certain amount of time you're best of with a coroutine. You can create a "framework" which you can pass a delegate to do that:

 IEnumerator _RunForTime(float aDuration, System.Action aCallback)
 {
     float t = 0f;
     while(t < aDuration)
     {
         t += Time.deltaTime;
         aCallback();
         yield return null;
     }
 }
 public void RunForTime(float aDuration, System.Action aCallback)
 {
     StartCoroutine(_RunForTime(aDuration, aCallback));
 }

Now you can simply call RunForTime like this:

 RunForTime(2f,()=>someVar += 100*Time.deltaTime);

Execute that line once and it will add 100 to someVar over the time of 2 seconds. Note that the endresult could vary slighly due to framerate fluctuations.

If you want to get the amount as precise as possible you could do this:

 IEnumerator _AddOverTime(float aDuration, float aAmount, System.Action<float> aCallback)
 {
     float t = 0f;
     float step = aAmount / aDuration;
     while (t < aAmount)
     {
         float add = step * Time.deltaTime;
         if (add >= aAmount - t)
         {
             aCallback(aAmount - t);
             yield break;
         }
         aCallback(add);
         t += add;
         yield return null;
     }
 }
 public void AddOverTime(float aDuration, float aAmount, System.Action<float> aCallback)
 {
     StartCoroutine(_AddOverTime(aDuration, aAmount, aCallback));
 }

And use it like this:

 AddOverTime(2f, 500f, (a)=>someVar += a);

So the callback receives the proper amount that should be added. As soon as the internal counter reached the goal it's terminated.

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 Frix · Jul 28, 2016 at 08:01 PM 0
Share

Very very informative, thank you!

avatar image
2

Answer by EDevJogos · Jul 28, 2016 at 04:38 PM

I think InvokeRepeating(); is what you looking for.

https://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html

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 Frix · Jul 28, 2016 at 04:50 PM 0
Share

Not really - say I have 100 points, and I gain a pick up. I want to add 500 points to my 100 points incremently over 1 second.

I then have 600 points and gain a pick up which gives me 1300 points - which would add incremently to my 600 over 1 second.

I've done it before and it's a very simple and short algorithm, even just one or two lines, I just can't remember it.

Sorry, my explanations are so bad but I hope that covers it!

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

54 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

Related Questions

Time delay enemy respawn 3 Answers

Delay in time 0 Answers

Change Time.scale only on specific objects 1 Answer

How to get prefab's creation time and date? 2 Answers

Displaying C# Int Value to GUI 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