The question is answered, right answer was accepted
deltaTime on fixedUpdate
Hey everyone, simple doubt here; I've been changing my code on movement's part but I got a weird glitch, so, my code is simple like this:
private void FixedUpdate() {
// Jumping.
if (Input.GetButtonDown("jump") && grounded)
rigid.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
}
Even though the code is already inside FixedUpdate, the height (or power, the float) keeps changing depending on the fps my game is running, only noticed this by recording the screen, which made the game lag a lot; What do you guys think?
[Edit] Update: Sometimes even when the game is not lagging, the character does high jumps, it keeps changing between low and high jumps, but whenever the framerate is low, it only does high jumps...
Answer by The_Three_Vs · Apr 01, 2020 at 12:32 AM
There are a couple things I can think of that might be causing this problem. 1: Getting Input in FixedUpdate instead of Update is causing issues https://forum.unity.com/threads/check-for-user-input-in-fixedupdate.214706/ . 2: Something about how you are updating grounded is affected by the frame rate, which would affect the amount of times force is applied to the rigidbody.
Hope this helps!
I tried using debug to verify what could be the problem and it seems the inputDown is being registered two times in one frame, and indeed, I'm using the input in FixedUpdate, gonna try to fix it now. :) Thank you so much!