tl;dr -> You don't need it! Look at the bottom of this answer
Time.deltaTime
is the time passed since the last frame was rendered.
=> for e.g. 60 F/S this value will be around 0.017
=> you need 60 of it in order to reach a factor of 1
.
So in short by multiplying a value with Time.deltaTime
it converts it from value per frame
into value per second
.
Is it necessary
Depends on your use-case ... do you want a fix value per frame (frame-rate dependent) or do you rather want a value per second (frame-rate independent).
Of course there are things you want to do on a per frame base like e.g. interpolating position inputs from an eye-tracker in order to stabilize the input but in general frame-rate dependent is usually a bad thing.
For something like
transform.position += transform.forward * moveSpeed;
it means
- If you have a more powerful device => more frames can be rendered in the same time => You move faster
- If you have a slow device => less frames can be rendered in the same time => You move slower
where can I add Time.deltaTime
Well, it is a float
so you can add it wherever you want and can multiply something by a float
;)
In your specific use-case: YOU DON'T. The Rigidbody.velocity
rb.velocity = movePosition * moveSpeed;
already is an absolute velocity in Unity units per seconds, so there is no need to multiply by Time.deltaTime
for this!
Just as a counter example a use-case where you would need it would be using this instead
rb.MovePosition(rb.position + movePosition * moveSpeed * Time.deltaTime);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…