The question is answered, right answer was accepted
How to Make Timer (Score) Count Up Faster?
I'm trying to increase the score counting speed slowly over time. Right now, all it does is add 1 second every actual second. I'd like it to slowly increase the speed in which it counts. So eventually, it'd be running at something like 2.3 score per actual second. So on and so forth.
public class Score : MonoBehaviour
{ private float score = 0.0f;
public Text scoreText;
void Update()
{
score += Time.deltaTime;
scoreText.text = ((int)score).ToString ();
}
}
EDIT: Here is the code I ended up using:
private float score = 0.0f;
public Text scoreText;
public float speedFactor = 1.0f;
void Update() { speedFactor += Time.deltaTime / 10;
score += Time.deltaTime * speedFactor;
scoreText.text = ((int)score).ToString ();
}
Answer by streeetwalker · Oct 13, 2020 at 06:14 AM
You need 2 more variables. You need a speed and a running score. You are going to multiply running score by a speed factor, say speedFactor, after you add Time.deltaTime to it. then add the result to score. Running score will need to be reset to zero after every second. Start with the speedFactor = 1;
For example:
runningScore += time.DeltaTime;
if( runningScore < 1.0f ){
runningScore *= speeFactor;
}else{
runningScore = 0;
}
Score+= runningScore;
You need to do this because you don't want the score to grow exponentially each pass.
You have decide how often to increase speedFactor and by how much - and how long it is before "eventually" happens.
Eventually speedfactor = 2.3, because as you state you want to add 2.3 every second, eventually.
So at this point you need to detail exactly how you want that to happen between your game start and eventually.
I think I get what you're saying. Just so we're clear here, what I'm trying to do is increase the score by 1 per second, but that 1 per second will change to 1.1 the next second, and 1.2 on the second after that. So for example, after 4 actual seconds, the score would be 4.6 because of 1.0 + 1.1 + 1.2 + 1.3
I'm sorry if I'm not making sense...
actually, I just edited the answer - it's a little more subtle than that. I don't think you want your score to grow exponentially, which is what will happen. So I think the sample code I posted fixes that.
OK, and if you want to increase every second on the second, then inside the else where the code sets runningScore back to 0, increase speedFactor by what ever increment you want. You will also want to put an if there (or use a clamp statement) to limit speedFactor to 2.3
Okay okay, I think I gotcha. I'll try this out tomorrow after work. I'll let you know how it goes. Thanks!
On further thought, I'm not sure my answer is correct yet.
Think about it. Every second you want to add an increasing value to the score, starting out at 1 per second and ending at 2.3 per second.
You do need a speedFactor and a running score, but the question is how to achieve the above.
The question is, do you need to add this in every frame, or only when each second is up?
$$anonymous$$ultiplying Time.deltaTime by a speedFactor will increase the rate of increase every frame, and you could do that directly:
speed += Time.deltaTime * speedFactor
without any of the extra code.
Then you just need to increase speedFactor and limit it.
If you want to increase it on the second, and not continuously, then you do need another variable to tell you when a second has expired.
thanks for all your help, you got me sent in the right direction. I ended up with this code:
private float score = 0.0f;
public Text scoreText;
public float speedFactor = 1.0f;
void Update()
{
speedFactor += Time.deltaTime / 10;
score += Time.deltaTime * speedFactor;
scoreText.text = ((int)score).ToString ();
}
Follow this Question
Related Questions
What exactly is affected by time scale ? 0 Answers
Why is my Unity Game crashing whenever Time Left is close to 0? 0 Answers
Using Time.deltaTime as a Timer 0 Answers
Time responding different when playing on Android Device (the APK, not Anroid Remote) 1 Answer
Android: Hiccups in simple scene - related to time 0 Answers