Search code examples
3dgame-physicsstate-machinegodotgdscript

Troubles with FSM implementation for 3D Character Controller


was hoping to get some help with my finite state machine character controller I'm making in Godot.

https://github.com/jacku97/SM_Player

I believe my code is readable-enough that it should be easy enough to follow what's happening.

Current bugs I need help to fix:

  1. When sprinting, letting go of sprint, and then sprinting again: the character controller saves the players velocity from the last time the character was sprinting and forces them in that direction briefly before going into the intended direction

  2. Same as above but with the walking state

  3. When entering the jumping state, the machine gets stuck in a loop of exiting and entering the jumping state continuously making the player float into the air. This can be stopped by pressing any other input.

I tried switching the direction calculations to its own bespoke function that gets called during the physics process in the assumption that it would reset the direction back to 0. But it appears this is an issue with velocity being stored between state transitions.


Solution

  • Currently each state stores its own velocity. Thus, it is not carried away between behaviors states, and the state behavior state remembers the last velocity.

    Don't do that. Store the velocity on the player character itself. The behaviors states already manipulate the player character (i.e. they set variables and call methods on it)… So, the idea is that the velocity is just one more variable they can manipulate.

    Perhaps you are thinking that the velocity is a state, and thus it should be part of the behaviors states of the state machine. But the thing is that what you are creating are not simply states, they are behaviors.

    I argue elsewhere for a line separating anything physics related (which includes the velocity) from these behaviors. But your mileage may vary.