- Home /
Sudden spike, delta time
I am making a game in which i multiply the SPEED with deltaTime. The value is mostly normal but at times it shoots up to 2x the normal value. Maybe its because of the frame rate? Normally the value is between 7.9 to 8.3 , but the spike takes the value up to 17.
Is there any way to fix it? I am doing this in the update() method.
Time.deltaTime is the time in seconds it took Unity to complete the last frame; 7.9 or 8.3 or 17 is way way out of whack. When you play your game, what's your FramesPerSecond when this happens and can you show the code for the relevant part where you are doing your calcs
7.9
means that the last frame took about 8
seconds to complete... What toaster are you running this on?
Here's the code
void Update () {
isPressed = false;
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))
{
isPressed = true;
xSpeed -= (SPEED_STEP*Time.deltaTime);
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
{
isPressed = true;
xSpeed += (SPEED_STEP*Time.deltaTime);
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow)){
// HERE IS THE PROBLE$$anonymous$$
print("jump speed : "+(jumpSpeed*Time.deltaTime));
ySpeed += (jumpSpeed*Time.deltaTime);
}
ySpeed -=(gravity*Time.deltaTime);
if ($$anonymous$$athf.Abs(xSpeed)<1&&! isPressed) {
xSpeed=0;
}
xSpeed *=hFriction;
checkCollisionX();
checkCollisionY();
}
This is the code, its basically for a platformer game. the value of SPEED_STEP = 40; jumpSpeed = 500; gravity = 15;
The FPS is between 73-88
I don't see any problems with it, but you could try to put the if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow)){
// HERE IS THE PROBLE$$anonymous$$
print("jump speed : "+(jumpSpeed*Time.deltaTime));
ySpeed += (jumpSpeed*Time.deltaTime);
}else if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.UpArrow)
ySpeed -=(gravity*Time.deltaTime);
But I don't think this will solve your problem. Why not use built in colliders and physics?
And for my answer:
ySpeed -=(gravity*Time.deltaTime);
if(ySpeed > maxValue) //for the positive values
ySpeed = maxValue;
else if(ySpeed < $$anonymous$$Value) //for the negative values
ySpeed = $$anonymous$$Value;
if ($$anonymous$$athf.Abs(xSpeed)<1&&! isPressed) {
xSpeed=0;
}
xSpeed *=hFriction;
checkCollisionX();
checkCollisionY();
There character controller has a problem according to this( http://www.gamasutra.com/blogs/YoannPignole/20131010/202080/The_hobbyist_coder_1_2D_platformer_controller.php ) and I want to give the character cartoony physics so thats why.
What if i use fixed Delta time, will it fix my problem? if so, any consequences ?
Answer by GTX Titan · Feb 01, 2014 at 10:01 AM
You could make an if statement to check if it's too much and then assign it a max value if it is. if(moveAmount > maxValue) moveAmount = maxValue
I am not sure what kind of functionality you need, but if you want it to move at the same speed you want this: moveAmount = speed*Time.deltaTime;
The spikes are caused by framerate since it is moving at the same speed but you just see less frames of it. As for the value jumping between 8-17 I'm guessing your speed value is somewhere around 300-1000.