I’m implementing some 2D platform physics – trying to model gravity myself. Very basic. But I’m getting wildly different speeds in my Editor vs my iPhone. I though multiplying by Time.deltaTime was supposed to make it framerate independent? Am I doing it wrong?
Update () {
if (transform.position.y >= 0) {
velY -= gravityFactor;
targetPos.y = transform.position.y + velY * Time.deltaTime;
}
transform.position.y = targetPos.y;
}
You’re subtracting gravityFactor from velY every frame without modifying it by time.deltaTime, so that will make it framerate-dependent.
–Eric
try going through your code and filling in some values.
lets say gravityFactor = 10;
after 1 second at 1 fps VelY will be -10;
your height will be changed 10;
after 1 second at 10 fps velY will be -100;
your height will be changed:
1/10 of 10 +
1/10 of 20 +
1/10 of 30 +
1/10 of 40 (etc till 100)
Yup, thanks! Just needed to multiply gravFactor by Time.detlaTime.
For some reason I thought multiplying twice was unnecessary, but we’re dealing with acceleration here, so it does make sense on second thought.
I actually tried out this solution but was jumping through the roof and assumed it was wrong. I just had to go in and re-adjust my jumping velocities because they were set up for the frame-dependent editor game.