- Home /
score, Update, Fixed Update and screen refresh rate (fps)
Hello, I made my first game and I noticed I made a mistake, when counting the score I was incrementing 1 point for every update call, then I remembered Update depends on frame rate of the device, so it's kinda not constant. So I thought: solution 1: add 1 point every FixedUpdate so that it is constant, ( but this seems not good practice since it's said to use only rigidbody stuff on fixedupdate)
solution 2: count the time interval in each update call and add points , but I don't know exactly how to keep it around 1point per 0.02 sec , maybe do some kind of proportion?
Also, In a simple game that needs instant screen reaction to touch, should I increment fixed update to something lke 60calls/s and limit frame rate of screen to 60 also? or what value should I give to each one? To lessen small delays on touch/screen reaction
Answer by nt314p · Jul 31, 2017 at 11:21 PM
You should use Time.deltaTime.
https://docs.unity3d.com/ScriptReference/Time-deltaTime.html
Example code:
void Update () {
score += 60f * Time.deltaTime;
}
Please don't hesitate to ask any questions!
Remember, Time.deltaTime is a really small number (more specifically, it is "The time in seconds it took to complete the last frame.") For example, it might be 1/60 for 60 FPS.
Probably is the best solution, giving around 1 point per update as well! Do you know the average fps on a mobile or how can i check the fps I have while playing my game in the mobile phone? Also, do you have any opinion on the second question? about changing fixedupdate time from 0.02? @n314p
If you are using physics, changing your fixed update time will make the game feel smoother and responsive but try not to push it because it might start to lag.
As for the checking fps on a mobile device, ask another question and give me the link (its not good to answer a question within a question).
Also, if my answer is satisfactory, you know what to do.
Answer by Sergio7888 · Jul 31, 2017 at 11:22 PM
You can use a float to hold the nextTime and compare in the update function.
float nextTime;
float waitTime= .02f;
int score=0;
void Update(){
if(Time.time > nextTime ){
nextTime = Time.time + waitTime;
score++;
}
}
Hum the idea is not bad but I think that, for example, if update is called when Time.time is still more 0.005 than nextTime, you have to wait until next frame. Or being, if update is called before the 0.02 wait