What is proper way to control a player character?(fsm? bt?)

Hi. I’m asking for advice about how to control my character. Especially while fsm(or bt?)is running.

I’m working on my personal project that is a 3D isometric view hack & slash game, like diablo 3.

I was dealing with dx9(and c++) before, and this is my first project with Unity. (new to c# too)

I was using simple fsm to controll player character and AIs. It didn’t work so good I was new to programming.

The most difficult part was handling input while the fsm is running on update. The code wasn’t look so good.

This is a sample of my fsm code. (Sorry it’s c++)

// declare:

class CState_Player_Idle : public CState_Player // <- this is abstract class
{
public:
   virtual           ~CState_Player_Idle() {}
public:
   virtual void   Ready(CCharacter* player);
   virtual void   Update(CCharacter* player);
   virtual void   Exit(CCharacter* player);
private:
   void             Handle_Input(CCharacter* player);
};

//implement:

void CState_Player_Idle::Ready(CCharacter * player)
{
   player->Setup_Animation(0);
   player->Set_Combat(false);
}

void CState_Player_Idle::Update(CCharacter * player) // < invoke this one every frame
{
   Handle_Input(player);
   player->Play_Animation();
}

void CState_Player_Idle::Exit(CCharacter * player)
{
}

void CState_Player_Idle::Handle_Input(CCharacter * player)
{
   if (Key_Pressing(DIK_W) || Key_Pressing(DIK_A) || Key_Pressing(DIK_S) || Key_Pressing(DIK_D))
   {
       player->Change_State(move_);
   }
   if (Key_Down(DIK_SPACE))
   {
       player->Change_State(combat_);
   }
}

// There were more states like move, combat, attack, knockdown, skills, roar(just a animation!), etc.

As you see, I had to make Handle_Input method in every state class I need to handle key input. I wondered if this was the proper way, but I never saw anyone else’s code, so I couldn’t know.

On the other hand, I heard about behavior tree when I was googling fsm, and I thought its idea is cool. But I couldn’t use it for my dx9 project, because my coding skill wasn’t good enough to use it.

I found some samples on the assetstore, but the codes are so complicated. So I tried to make my own, but I got stuck in handling key input with mecanim.

Finally, I realize I really really need to study more. Please give me some advice. I would also appreciate it if you tell me about the website or YouTube videos can help me learn more.

It does get hairy… one way to help break it down is to process the user input as sort of like a “data flow.”

You can model it as:

  • user inputs lead to a user intention

  • a user intention is judged to be valid or invalid based on state

  • if valid the state is processed and the intent is considered satisfied

  • if a state doesn’t care about an intent, it lets it go by to the next processor.

That way if you are wielding a sword, “attack” is a valid state.

But if the sword sees “walk left” it just ignores it and passes down to the next.

Unity also lets you plug lots of behaviors into an object, then have a single behavior that controls which one is enabled right now, very Component-oriented.

Finally out of all that you want to mimick everything with an actual animator state machine that you setup and drive accordingly.

Here was some discussion of partitioning logic between code and the animator state machine.

Thank you very much. I read your reply and article you linked, but I’m still confused.

I’m not sure if I understand your word properly.

If sword(you mean attack state in fsm, right?) ignore my input(walk left), then I have to change state to idle when attack animation is finished, and then idle state can handle input.

Did I get it right?

By the way, I also read the article from Gamasutra. It was interesting because the first and second approaches look very similar to what I did in dx9 project.

But I’m not sure what to get from the article. As author mentioned at the end of the article, I guess it’s hard to keep away from Mechanim when working on 3d characters.