- Home /
Time.DeltaTime doesn't seem to be correctly
I'm currently developing a game, where I have my player controller that can move.
In the Editor, the speed is normal as expected. When I'm building the game to a Standalone build, and runs it in Windowed Mode, it also runs fine, same speed and framerate independent.
However, if I suddenly go to fullscreen with the exported standalone build, all of a sudden everything seems alot faster, and not framerate indepedent at all.
For better understanding of the problem, download the build here: http://kevinjp.dk/games/lightsource/windows/LightSource_Alpha10.zip
It's worth noting that i'm using the Character Controller Component for my movement.
My movement source code:
private void Movement() {
//Z-Axis movement, forward and backwards. Adds current acceleration to the currentSpeedVertical.
currentSpeedVertical += acceleration * Input.GetAxis("Vertical");
currentSpeedVertical = Mathf.Clamp(currentSpeedVertical, -maxSpeed, maxSpeed);
//X-Axis movement, left and right. Adds current acceleration to the currentSpeedHorizontal.
currentSpeedHorizontal += acceleration * Input.GetAxis("Horizontal");
currentSpeedHorizontal = Mathf.Clamp(currentSpeedHorizontal, -maxSpeed, maxSpeed);
//Y-Axis movement, up and down. Adds current acceleration to the currentSpeedUpDown.
currentSpeedUpDown += Input.GetKey(KeyCode.Space) ? acceleration : 0;
currentSpeedUpDown -= Input.GetKey(KeyCode.LeftShift) ? acceleration : 0;
currentSpeedUpDown = Mathf.Clamp(currentSpeedUpDown, -maxSpeed, maxSpeed);
//Checks if player is not moving vertically, if that's the case, deaccelerate our values.
if(Input.GetAxis("Vertical") == 0 && currentSpeedVertical != 0) {
if(currentSpeedVertical > 0) currentSpeedVertical -= acceleration;
if(currentSpeedVertical < 0) currentSpeedVertical += acceleration;
if(Mathf.Abs(currentSpeedVertical) < acceleration) currentSpeedVertical = 0;
}
//Checks if player is not moving horizontally, if that's the case, deaccelerate our values.
if(Input.GetAxis("Horizontal") == 0 && currentSpeedHorizontal != 0) {
if(currentSpeedHorizontal > 0) currentSpeedHorizontal -= acceleration;
if(currentSpeedHorizontal < 0) currentSpeedHorizontal += acceleration;
if(Mathf.Abs(currentSpeedHorizontal) < acceleration) currentSpeedHorizontal = 0;
}
//Checks if player is not holding space or leftshift, if that's the case, deaccelerate our values.
if(Input.GetKey(KeyCode.Space) == false && Input.GetKey(KeyCode.LeftShift) == false && currentSpeedUpDown != 0) {
if(currentSpeedUpDown > 0) currentSpeedUpDown -= acceleration;
if(currentSpeedUpDown < 0) currentSpeedUpDown += acceleration;
if(Mathf.Abs(currentSpeedUpDown) < acceleration) currentSpeedUpDown = 0;
}
//Put it all together in a movement variable.
Vector3 movement = new Vector3(currentSpeedHorizontal, currentSpeedUpDown, currentSpeedVertical);
//Move the actual character controller with the correct values converted
//in a TransformDirection, and multiple that with deltaTime to make it framerate independent.
cc.Move(transform.TransformDirection(movement) * Time.deltaTime);
}
Answer by Hoeloe · Mar 05, 2014 at 03:57 PM
This is because, while you are applying the movement using Time.deltaTime, you are still accelerating it dependent on the framerate. Think about it like this: If your acceleration is 1, then since you are adding to currentSpeedUpDown without multiplying by Time.deltaTime, then in 60 frames, your speed will be 60. At 60 frames per second, this will take 1 second. At 30 frames per second, this will take 2 seconds - the acceleration is not framerate independent! Now you multiply that by Time.deltaTime to apply it, and that's all well and good, but your speed is still increasing dependent on the framerate, so naturally your acceleration will be faster, if not your overall speed.
What you really need to do is multiply the acceleration components by Time.deltaTime when you add them to the speed - this ensures that acceleration is also framerate independent, and it should fix your issue.
So what your saying is, that I should actually change the $$anonymous$$ovement Variable line to this:
Vector3 movement = new Vector3(currentSpeedHorizontal Time.deltaTime, currentSpeedUpDown Time.deltaTime, currentSpeedVertical * Time.deltaTime);
No, absolutely not. I'm saying you need to change lines like this:
currentSpeedUpDown += acceleration;
To this:
currentSpeedUpDown += acceleration * Time.deltaTime;
Time.deltaTime is the change in the computer's internal clock since the last update. You need to apply it to changes you've made to your values, not to the values themselves.
Think I defintely got it now. Ofcourse, after I added the Time.deltaTime where I am making changes to my currentSpeed variables, I'd have to change the acceleration values a little bit, but I guess that's to be expected?
It works now! I really appreciate your help man!
Yup, that's to be expected, but it should mean it's framerate independent.