Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Is It Bad to Use an FSM for Player Movement in a 2D Platformer?

Discussion in 'Game Design' started by ordowerm, Dec 24, 2020.

  1. ordowerm

    ordowerm

    Joined:
    Mar 6, 2018
    Posts:
    6
    Hi, folks! I'm new to the forums (and software engineering and game design in general), so I apologize if this has been discussed ad nauseum. I've been looking at some beginner tutorials for 2D platformers, and something feels iffy about the way they're programmed--like they're designed haphazardly or hackily. I want a more organized, modular approach to player movement than what I've seen. I feel like an FSM is the right approach, but I'm not sure how to best implement it.

    I haven't had many opportunities to look at "good" game source, and I really distrust most of the tutorials I've seen, so I've been fiddling around, and I'd like some opinions. I've included some code below. Is this a bad design approach? If so, please explain why. If you can point me toward better alternatives, or a really good tutorial, or even some really good game source, I'd really appreciate it.

    Code (CSharp):
    1. public class PlayerMovement {
    2.  
    3.     //model player movement as a finite state machine
    4.     public enum PlayerStates {
    5.         STAND,
    6.         WALK,
    7.         JUMP,
    8.         FALL
    9.     }
    10.  
    11.     PlayerStates playerState = PlayerStates.STAND;
    12.  
    13.     void Stand() { ... }
    14.     void Jump(){ ... }
    15.     void Walk() { ... }
    16.     void Fall() { ... }
    17.  
    18.     void ExecuteState(){
    19.         switch(playerState) {
    20.             case PlayerStates.STAND:
    21.                 Stand();
    22.                 break;
    23.             case PlayerStates.JUMP:
    24.                 Jump();
    25.                 break;
    26.  
    27.               //and so on.
    28.         }
    29.     }
    30.  
    31.     private void Update(){
    32.            ExecuteState();
    33.     }
    34. }
    Thanks, folks. If you have any tips for forum etiquette, too, I'd really appreciate that, as well. I figure this topic has been discussed a bajillion times, but navigating all the debates on all the threads is pretty overwhelming. Thanks for your patience.
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,531
    Yes, an FSM is a good approach for a player controller.

    Opinions vary on FSM implementations, from simple switch statements like your example code, to vastly overengineered monstrosities. Go with the simplest implementation that works for you. Try to minimize coupling (interdependence between different scripts). It'll make it easier to refactor in the future if you need to.
     
  3. Steve_Stevens

    Steve_Stevens

    Joined:
    May 3, 2016
    Posts:
    35
    google state machine class. I prefer to use that for state machines. Using a class you get 3 stages of the state. OnEnter, InState, OnExit. Much more flexibility when changing states. Plus you can let the states do your heavy work!
     
    ordowerm likes this.
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,842
    I agree, a state machine is a fine way to control player movement. You can see some approaches to it (with sample code) in 2D Animation Methods in Unity.
     
    ordowerm and TonyLi like this.
  5. ordowerm

    ordowerm

    Joined:
    Mar 6, 2018
    Posts:
    6
    Thanks so much, folks! This is super-helpful.