Unity character control system (finite state machine idea)

Before entering the main text, you can first take a look at the basic skeleton of the finite state machine built in my last blog.
portal

At the end of the last blog, I talked a little bit about the issue of multi-state coexistence. Let me use this blog to write about the character control system and the issue of multi-state coexistence in as much detail as possible.

analyze

There are so many states that a character should have [idle, walking, running, standing, jumping, crouching, crouching and jumping], I can roughly divide them into three categories:

  • Mobile status

【Idle, walk, run】

  • attitude state

[Stand, jump, squat]

  • compound state

[Squat and walk, jump and walk]

It can be seen that multiple states may appear at the same time during character control (for example, squatting and jumping). However, according to the definition of a finite state machine, a finite state machine can only support the existence of one state at the same time, so this Sometimes, I will consider using two state machines to control the character, namely the movement state machine and the posture state machine. As for the composite state, it is a different combination of these two state machines.

implement

First define all state objects, and all objects inherit the interface IState
Idle (idle state), Walk (walking state), Run (running state), Stand (standing state), Crouch (crouching state), Jump (jumping state)

Next, two state machines are needed to control movement and posture respectively as follows:

    playerFsm1=new FSMController();
    playerFsm2=new FSMController();
    //状态机1 负责移动
    playerFsm1.AddState(new Idle());
    playerFsm1.AddState(new Walk());
    playerFsm1.AddState(new Run());
    
    //状态机2 负责姿态
    playerFsm2.AddState(new Stand());
    playerFsm2.AddState(new Jump());
    playerFsm2.AddState(new Crouch());

Two state machines assign initial states

    playerFsm1.ToState<Idle>();
    playerFsm2.ToState<Stand>();

Then, different status switches are performed according to different keyboard commands.

    if (Input.GetButtonDown("Jump"))
    {
        if (playerFsm2.IsCurrentState<Jump>() ||playerFsm2.IsCurrentState<Crouch>()) return;
        playerFsm2.ToState<Jump>();
    }else if (Input.GetKeyDown(KeyCode.C))
    {
        if (playerFsm2.IsCurrentState<Jump>())return;
        if (playerFsm2.IsCurrentState<Crouch>())playerFsm2.ToState<Stand>();
        else playerFsm2.ToState<Crouch>();
    }
    moveV3.x = Input.GetAxis("Horizontal");
    moveV3.z = Input.GetAxis("Vertical");
    if (moveV3!=default)
    {
        if (playerFsm1.IsCurrentState<Walk>()) return;
        playerFsm1.ToState<Walk>();
    }

If the character has other states, including attack, attack, etc., the state can be constructed similarly and the state switching relationship can be analyzed. Such a character control state machine is easier to maintain and expand later.

additional

The project has been uploaded. In addition to character status switching, the project functions also include keyboard control and joystick control. The perspective transfer has both mobile phone mode and PC mode.
Children's shoes that are interested can download and take a look at: Project address

Guess you like

Origin blog.csdn.net/weixin_42498461/article/details/123895783