- Home /
Game behaves differently in build version
Hi there,
I have a little racing game that is coming along nicely but for some reason has just started playing up.
My car behaves perfectly in the preview window but when I build the game, acceleration and braking are ridiculously strong, yet the top speed is the same...
I am using Time.deltaTime... so I can't see why this would be happening... here is the code:
// If W key is pressed, accelerate
if (Input.GetKey("w"))
{
if (fSpeed < fMaxSpeed)
{
fSpeed += fAcceleration;
}
transform.Translate(0.0f, 0.0f, fSpeed * Time.deltaTime);
}
// If S key is pressed, brake
else if (Input.GetKey("s"))
{
if (fSpeed > 0.0f) // Brakes only work if car is moving
{
fSpeed -= fBrakeForce;
transform.Translate(0.0f, 0.0f, fSpeed * Time.deltaTime);
}
}
// If no input, slow down gradually
else
{
if (fSpeed > 0.0f) // So that car doesn't move in reverse when comes to stop
{
fSpeed -= fDecceleration;
transform.Translate(0.0f, 0.0f, fSpeed * Time.deltaTime);
}
}
Answer by coolthemanp6p · Dec 27, 2011 at 10:48 PM
Time.deltaTime is basicly FPS or Frames Per Second. The editor uses less fps, which when you build your FPS is higher. So it's fSpeed * Time.deltaTime. Imagine this your speed is 10 and first the editor's FPS is 20. 10 x 20 = 200. Now you build. 10 x 30 = 300. Just by the fps being 10 more your speed increases by 100. I would suggest bringing the speed down.
Time.deltaTime is not frame rate driven, but time driven.
Yeah and the speed is not the problem... it's the acceleration and braking... happens way too fast...
Oh, also... acceleration , deceleration and braking figures are already very low (0.2, 0.1 and 0.5). f$$anonymous$$axSpeed is set to 25.
Any help greatly appreciated!
Acceleration and deceleration follow a quadratic curve and can't be scaled linearly with the frame rate.
Time.deltaTime is NOT the FPS, it's the time passed between two frames (the delta). It can be calculated with 1/FPS.
If you multiply a value you use in a linear calculation with Time.deltaTime it becomes frame-independent. Now the value specifies the ammount per sec.
Example: value == 5
value*Time.deltaTime at 1 FPS(deltaTime == 1/1 == 1)
== 5
value*Time.deltaTime at 10 FPS(deltaTime == 1/10 == 0.1)
== 0.5
At a higher FPS the value per frame will be lower to get the same value after 1sec. The second example has a frame rate of 10 so it's executed 10 times in one sec. 0.5 * 10 == 5.
Accelerated movement won't work since a quadratic function can't be scaled linearly. You should use $$anonymous$$onoBehaviour.FixedUpdate for such calculations since the calling-rate will be fixed (corrected) to match the desired physics-frame-rate.
The explanation of @coolthemanp6p is wrong. Usually for all linear movement / change just use Update with Time.deltaTime. For all physics or physics-like calculations use FixedUpdate with Time.deltaTime. Don't forget the deltaTime in FixedUpdate to have all your values time-based ins$$anonymous$$d of (physics-)frame based.
Don't change your physics - framerate or it will have an impact on the calculations again.