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.