Movement speed in Game view is different than that in the .exe

E: Originally posted in “Editor and General support”, I don’t know if this goes to “Scripting” so apologies in advance.

I have a simple code that gives a downward force to an object when I press a key.

Called in Update

if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S)) {
    rg.AddForce(new Vector2(0, -1 * RGspeed));
}

RGspeed is calculated : RGspeed = 100 * (Time.deltaTime + 3f);
so it floats at around 300 all the time.

The Rigidbody2D (“rg”) looks like this:

when I play the game in Game view I get to a set point in 3,16 seconds. (around 200 FPS)
when I play the game as a .exe I get to the same point in 7.84 seconds. (capped by Unity at 60FPS)

Why is there such a big difference ?

E2: After some testing I set RGspeed to: (300/delta time of 60 FPS) * Time.deltaTime
which made the time in editor 7,2 second and ingame 8,6 second. Butter but still it is not the same.
E3: Tried using Character Controller, but that would require a lot of new code and scene editing so I reverted back.

Thanks

Input.GetKey returns true while the key is held down. So you’re calling AddForce every frame the key is held down. If your frame rate is higher you’re going to call it more times each second.

If you just want to add the force once per keypress then use Input.GetKeyDown instead.

Otherwise you should scale the force by the elapsed time, e.g.

rg.AddForce(Vector2.down * Time.deltaTime);

Are you adding the force in Update or FixedUpdate? All physics should be in FixedUpdate. Time.deltaTime is not required in FixedUpdate.

I tried FixedUpdate and the difference shrunk to 1 second. It is still not perfect but much better than the original, Thanks.