0
\$\begingroup\$

I'm trying to implement a score count-up that is synced with the tempo of some short music jingle, but I'm failing on calculating the math for it.

Let's say I have a total score of 2000 that I want to count-up on the screen to the player, starting from 0 up to 2000. There's a catch though - I want to have the count-up synced to a short music jingle that's played during the count-up.

Let's say the music jingle is 15 seconds long, in a specific BPM (beats per minute). As soon as the music jingle starts playing, the score starts counting up from 0, in a certain speed. What I would like to have is the score count-up reach 2000 just when the music jingle is ending (so in this case when 15 seconds has passed), so the count-up speed needs to be based on that music jingle's total time, which is 15 seconds. The same mechanic needs to work if I later on decide to change the music jingle to a 8 seconds jingle instead.

I can't really wrap my head around it and needs some assistance.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Is the music crucial here (eg. matching beats), or do you just need to know how to count up to a target value over a set duration? \$\endgroup\$
    – DMGregory
    Mar 21, 2023 at 19:12
  • \$\begingroup\$ I guess the music isn't really important when I think about it. It's rather the time/duration of the music that is important, and the end result of the score. So no, the count-up doesn't need to match the music's tempo. \$\endgroup\$ Mar 21, 2023 at 19:31

1 Answer 1

1
\$\begingroup\$

You can do something as simple as converting the time spent counting so far to a fraction out of the total counting duration (capped at 1 so you don't overshoot if the time keeps accumulating), then multiply your target value by that fraction.

progress = secondsSinceCountingStarted/durationSeconds;
progress = Min(progress, 1.0);
// Optional: you can apply an easing function if you
// want progress to be faster at the start, end, or middle.
// progress = Ease(progress);
currentCount = Round(progress * targetCount);

If you want to use an easing function as mentioned in the comment, it can be anything that maps f(0) = 0 and f(1) = 1, like the square function (eases in) or smoothstep (eases in & out).

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .