Best design for a snappy 2D platformer finite state machine?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By alibix
:warning: Old Version Published before Godot 3 was released.

So I’ve tried to make a platforming finite state machine.

It has 2 states:

  • idle
  • walking

And two transitions:

  • onMovementPressed
  • onMovementReleased

This is what it looks like with the awesome FSM plugin by Jakub Grzesik.

http://i.imgur.com/WIoMLTL.png

The character will transition from idle to walking if the left or right button is pressed. Using this code:

http://i.imgur.com/Gilnqqv.png

(the face left and face right are just to change the way the character is facing)

The character will transition back to idle if the left or right button is stopped being pressed:

http://i.imgur.com/XapNa2Y.png


(the transitions will happen if the transition function returns true)

This system works… okay. However, it is quite bad for quick movements. If I press left and right really quickly the character gets stuck in place or moonwalks in one direction (without turning around). Is there a better way to transition between these states that is snappy and glitch free?

sorry about blurry code

:bust_in_silhouette: Reply From: eons

If does not respond well to quick movements is because you never blocked/ignored the inputs while is moving.

Some extra check you may want are the release and press/pressed events when is too close to the landing point to stop,continue or reset movement (_input/_input_event could be better for this kind of mechanics).

Thank you! Also, I changed my player to be kinematic instead of rigid body. That made things a lot snappier.

alibix | 2017-06-26 22:03

In general, rigid bodies are not good unless you designed your game to respect the physics engine craziness, and generic physics engines don’t like grid or similar mechanics too much.

There is the custom integrator on Godot rigidbodies that allow more fine control but if you are not going to use rigid physics is better to avoid them.

eons | 2017-06-27 12:58

:bust_in_silhouette: Reply From: bootyman

How exactly is the movement carried out (physics based?)? Where exactly do you call these functions?

I figured it out. My player character was Rigidbody2D. When I switched to Kinematic2D it didn’t glitch.

alibix | 2017-06-26 22:17