Create Score increasing effect

I like to have two quantities for this:

int actualScore;
int displayedScore = -1;

When the game logic adds points, it does this:

actualScore += 12345;

Now over in the display logic you periodically:

  • adjust displayedScore towards actualScore (magic happens here!)

  • output displayedScore

Now… onto the magic.

Obviously if you count one point (or any constant amount) it is pretty uninteresting… a huge score add might take forever to get together.

Keeping it all as integers, here is the approach I like to use for computing how much to .MoveTowards() each iteration:

  • have a basic non-zero constant amount (for instance, 1)

  • have a fraction of the difference (say 1/5th, or one fifth)

Add the two above terms together and use that as the amount to move.

void Update()
{
   int difference = actualScore - displayedScore;

   if (difference != 0)
   {
     int constantTerm = 1;

     int proportionalTerm = difference / 5;

     int moveStep = Mathf.Abs( proportionalTerm) + constantTerm;

     displayedScore = (int)Mathf.MoveTowards( displayedScore, actualScore, moveStep);

     /// now use displayedScore to update your text output
   }
}

NOTE: the above is highly framerate-dependent. One way to address that is to put the above in a loop in a coroutine:

// run this once and once only, and it runs forever during score display.
// it can even live right on your score display UI scripts
IEnumerator MyAlwaysRunningScoreUpdater()
{
  while(true)
  {
    yield return new WaitForSeconds( 0.1f);

    // --- insert the guts of the Update() function code above right here---
  }
}
1 Like