- Home /
Using Time.deltaTime when incrementing a value
I'm incrementing an int variable (kind of a heat system) each time player jumps, keeps holding the jump key and when he moves around. I use addForce method to move the character. All this is happening inside a FixedUpdate function. Do I need to use Time.deltaTime when I'm incrementing or reducing the int value? How do I generally know when to use time.deltaTime?
Below there is a code example of the jump block I have in my character controller script.
if(Input.GetButton("Jump")) {
GetComponent<HeatController>().GainHeat(50); // Is time.deltaTime needed here?
rigidbody2D.AddForce(new Vector2(0f, jumpForce));
}
Answer by rutter · Feb 21, 2014 at 10:05 PM
Values that change over time should generally be expressed in units-per-second. If they aren't, they will implicitly depend on your frame rate, which can vary wildly.
Time.deltaTime
tends to be very small. For example, let's pretend that your game runs at exactly 50 FPS (it won't, but it makes the math easy). That gives you about 0.02 seconds per frame. Let's say you want to gain 100 heat per second, while whatever thing is running. Multiplying that 100 by 0.02 gives 2 heat per frame, running that for 50 frames gives 100 heat total.
This stuff gets more intuitive if you learn calculus. :)
This does bring us to one other problem, ints versus floats:
int x = 1f * 0.5f; //result is "truncated", x is now zero
float y = 1f * 0.5f; //result is correctly 0.5f
Integers don't ever store a fractional part of your numbers; they "truncate" or discard it completely. Values which need to store fractional parts should usually be floats. If you need to know more about this, most people first run into it with a concept called "integer division", which you can read up on.
How does Unity's FixedUpdate affect to this? I've heard it has something to do with framerate.
FixedUpdate is called every physics step, which is less frequent than every frame, this is useful for intergrating math with physics because modifying physics related stuff inside Update will cause weird things to happen.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134301im_/https://answers.unity.com/themes/thub/images/avi.jpg)