Can't achieve framerate independent movement
Hello,
I can't get my movement to be framerate independent. Here's the code I using inside Update:
void Update ()
{
if (Input.GetKeyDown(KeyCode.Escape))
Application.Quit();
if (Input.anyKey)
{
velx ++;
}
else
{
velx *= drag;
}
velx = Mathf.Clamp(velx, -max_velx, max_velx);
Vector3 delta = new Vector3(velx * Time.deltaTime, 0, 0);
transform.Translate(delta);
}
When I change the quality of the graphics in the game.. the player speed changes considirably. I can't see where the problem might come from, since I'm scaling the velocity by the time spent on the frame.
Answer by hexagonius · Oct 17, 2015 at 03:22 PM
yes, but velx gets changed by constants and not by deltaTimed values. velx++ and velx *= drag are called as often as Update is ran.
OOO$$anonymous$$..thanks. I understand. But isn't there a way to cap the calling frequency of the Update method? I'm planning to run my game at 60FPS..And I don't care if the user's machine can't do that(he should buy another machine or lower the settings). I'm using velx++ ins$$anonymous$$d of velx+=Time.deltaTime is I don't want my movement equation to be quadratic in terms of time.. I think that's non intuitive when it comes to gameplay tweeking.. What do you think?
Use FixedUpdate for a fixed call interval ins$$anonymous$$d. What you mean with quadratic equation, I don't know.
If you do velx+=Time.deltaTime and then you do Translate(new Vector3(velx*Time.deltaTime,0,0)) now your position depends on Time.deltaTime².. this is what I mean by quadratic in terms of time.
I used FixedUpdate but I got some weird input, and the player wasn't responding as he should.
Your answer
Follow this Question
Related Questions
deltaTime on fixedUpdate 1 Answer
Unity Create with Code Challenge 1 0 Answers
CharacterController's Move frame-dependency 1 Answer
It is posible to use a vector2 with a math function? 0 Answers
Circular movement for the player 0 Answers