I have a script attached to a player object in my game (only one instance of a player, so the script exists only once). Within the FixedUpdate() function, the game checks for input from the user. The input options are just left, right, down, and up. One of those being pressed should move the player 5 units in the direction pressed and about 90% of the time, this is exactly what happens. On occasion, though, it’ll move the player 10 units and I can’t for the life of me understand why.
I’m new to this so I may just not know “best practice” for player movement quite yet, but was hoping someone was familiar with the issue.
Input checks should generally be done in Update() instead of FixedUpdate(), as they run at different speeds. Update() runs every game frame and changes depending on framerate, where FixedUpdate runs at a constant 50fps to match up with physics updates. Inputs like GetKeyDown are only true for the one frame that the keypress is registered, and this may or may not match up with FixedUpdate cycle.
If your game is running at a higher framerate than 50fps, then the GetKeyDown may happen in between FixedUpdates and not register at all. If the game is lower framerate (or a temporary dip occurs), then two FixedUpdates could trigger for one game frame and GetKeyDown is seen twice.
If you are not using physics based movement, then simply moving your code to Update() should fix the issue. If you are using physics movement, then use Update to check input and set a bool, then react to that bool in FixedUpdate(). Something like this pseudocode: