- Home /
How make movement speed constant with FPS?
I am trying to code a movement similar to Flappy bird where when I click the button the player jumps.
However whenever the FPS changes the movement change, I understand this is because I am using update() to move the character.
I was taught that if you multiply a value with Time.deltaTime it wouldn't change. But alas it seems to persist, and there are times when the time.deltaTime value spikes.
What do I do in this scenario?
The code I am using basically gives the player a constant downward acceleration. This makes the velocity goes to the negative. When I want the player to jump, I give it a positive velocity so it flies upwards. Essentially, Flappybird.
void Update () {
transform.position = new Vector3(
transform.position.x,
transform.position.y + (flightDropVelocity * Time.deltaTime),
transform.position.z);
flightDropVelocity = flightDropVelocity + flightDropAcceleration;
}
public void Flight(){
flightDropVelocity = 250f;
}
I am not really using physics though? I thought you only use it when dealing with rigidbody stuff. I am just making a constant deceleration of values ish.
Unless you mean this will fix all issues
What kind of frame rates are you getting. I believe time.deltaTime does as mentioned as long as frame rate stays relatively consistent. If you are having huge spikes in fps it will be hard to keep it looking right.
If all else fails, you could make another vector 3 deter$$anonymous$$ing where you want it to go and go to it using -newlocation- = Vector3.$$anonymous$$oveTowards (-location-, -destination-, -speed- * Time.deltaTime);
Yeah it does get spikes at times. So I have no idea what to do, I am at a loss at the moment.
Answer by fholm · Mar 26, 2014 at 09:06 AM
You are applying the delta time at the wrong place, this is what you want most likely:
void Update () {
flightDropVelocity = flightDropVelocity + (flightDropAcceleration * Time.deltaTime);
transform.position = new Vector3(transform.position.x, transform.position.y + flightDropVelocity, transform.position.z);
}
I want it to move consistently without the framerate affecting it, so should I use the Time.deltaTime or not?
Yes you should use Time.deltaTime, the code you have is correct.
If so I am absolutely bewildered because it is not working as intended. Sigh. Thanks a lot anyway.
I mean, the logic you are using doesn't give you constant speed downwards/upwards it gives you constant acceleration, which is probably not what you want. It is not dependant on framerate tho.
Actually, I looked closer, this is probably what you want:
void Update () {
flightDropVelocity = flightDropVelocity + (flightDropAcceleration * Time.deltaTime);
transform.position = new Vector3(transform.position.x, transform.position.y + flightDropVelocity, transform.position.z);
}