Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Resolved how to continuously add an amount in x time

Discussion in 'Scripting' started by Nixel2013, Sep 30, 2022.

  1. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    139
    for example, my quantity is: 20, 60, 80, 100.

    How do I make these values add one to one continuously in 1 second?

    That is, how do I make "float Sum" add one by one in 1 second, 20 units in 1 second?
    That is, how do I make "float Sum" add one by one in 1 second, 60 units in 1 second?
    That is, how do I make "float Sum" add one by one in 1 second, 80 units in 1 second?
    That is, how do I make "float Sum" add one by one in 1 second, 100 units in 1 second?


    it is understood? :confused:
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Something like
    Code (CSharp):
    1. public int AmountPerSecond = 50; // as an example
    2. public int currentValue = 0;
    3. float timer = 0;
    4.  
    5. void Update() {
    6.   float interval = 1f / (float)AmountPerSecond;
    7.   timer += Time.deltaTime;
    8.   while (timer > interval) {
    9.     currentValue += 1;
    10.     timer -= interval;
    11.   }
    12. }
     
    Last edited: Oct 1, 2022
    mopthrow, Nixel2013 and Kurt-Dekker like this.
  3. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    139
    <3
     
  4. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    139
    and the opposite of that? : v

    that is, that 100 decreases one by one, in 1 second
     
  5. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,134
    Just set currentValue to 100, and decrease the value by 1 in the loop.
     
    Nixel2013 likes this.
  6. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    139
    yes yes :p my bad