- Home /
Character controller without Deltatime
Hi. I want to know is it necessary to use Time.deltatime
for FPS character controller
script? for example in this script :
void Update() { Vector3 forward = transform.TransformDirection(Vector3.forward); Vector3 right = transform.TransformDirection(Vector3.right);
float curSpeedX = walkingSpeed * Input.GetAxis("Vertical");
float curSpeedY = walkingSpeed* Input.GetAxis("Horizontal");
float movementDirectionY = moveDirection.y;
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
{
moveDirection.y = jumpSpeed;
}
else
{
moveDirection.y = movementDirectionY;
}
if (!characterController.isGrounded)
{
moveDirection.y -= gravity * Time.deltaTime;
}
characterController.Move(moveDirection * Time.deltaTime);
}
If we remove Time.deltaTime and reduce some values like "gravity", or "walkspeed" the script works fine.
But I don't understand why in all the examples that I have seen over the internet , Time.deltatime is used ? even in other scripts like mouselook.
Is it necessary to use Time.deltatime?
Answer by MrRutabaga · Aug 08, 2021 at 03:10 AM
Time.deltaTime is the number of seconds between the last frame and the current one.
I you don't use it, as you see, it works fine...
But not exactly.
Time.deltaTime is used to remove unpredictible change of speed (among other problems) due to FPS changes.
Imagine a character going at a speed of 5, meaning he's going 5 units a second. If you don't use deltaTime and going from 60 to 30 fps, your character will move 2.5 units a second.
That's really simple but it's the basic. Always use Time.deltaTime when doing physics and frame dependent movements.