The article directories
- 1. Enemy status analysis
- 2. Create enemy base states
- 3. Create two different states for the character
-
- 3.1 Patrol status
- Enemy 3.2 Abstract class state machine in Enemy
- 3.3 Adding the Attack Status
1. Enemy status analysis
Finite State Machine (IF-else, switch-case) Finite State Machine (if-else, Switch-case) Later, when a new state is added, a separate code can be created to inherit the most basic class and extend the functionality.
The current enemy has two states: 1. Patrol state: enter patrol state and patrol back and forth between point A and point B. Once Player or Bomb is found, switch to attack state 2. Once in attack mode, you can track game targets, filter attack modes, attack players, or blow out bombs.
2. Create enemy base states
Create EnemyBaseState script that does not need to be attached to any scene object, so does not inherit MonoBehaviour. Whether in patrol or attack state, there are two timing points, some basic functions need to be implemented when entering the state, and some methods need to be executed in each frame of Update when entering the state. When you get into a state you still need to call the Enemy and some parameters, so you need to pass Enemy as an argument into the method.
public abstract class EnemyBaseState
{
public abstract void EnterState(Enemy enemy);
public abstract void OnUpdate(Enemy enemy);
}
Copy the code
3. Create two different states for the character
3.1 Patrol status
Enter the patrol state at the beginning of the need to switch the target point, do not need to switch in Enemy Start, but in the patrol state script to switch. The same methods in Update are written in OnUpdate
using UnityEngine;
public class PatrolState : EnemyBaseState
{
public override void EnterState(Enemy enemy)
{
// When entering the state, switch the target point of patrol
enemy.SwitchPoint();
}
public override void OnUpdate(Enemy enemy)
{
// Each frame loops endlessly towards the target point and the target point of the current enemy
if (Mathf.Abs(enemy.transform.position.x - enemy.targetPonit.position.x) < 0.01 f)
enemy.SwitchPoint();
enemy.MoveToTarget();
}
Copy the code
The state machine function methods are written in two different classes, and the PatrolState is not mounted on the enemy.
Enemy 3.2 Abstract class state machine in Enemy
Add type variables to the state machine
///The current status of the enemy
private EnemyBaseState _currentState;
Copy the code
_currentState Indicates the patrol state and attack state.
Need to write a function to switch to which state. Here we need to get the patrol state and attack state of the object, pass this object into the TransitionToState() method so that the current state = the object passed in, and then call its own EnterState method to enter the state.
///Method of switching states
public void TransitionToState(EnemyBaseState state)
{
// Toggle state Current state = incoming state
_currentState = state;
// The enemy enters the current state after switching
_currentState.EnterState(this);
}
Copy the code
Get the object that needs to be passed the parameter. Here, get the patrol first, attack object is not written yet.
// Get the object in the patrol state
public PatrolState patrolState = new PatrolState();
Copy the code
Enemies should be on patrol at the beginning of the game, and patrol back and forth throughout the Update.
void Start()
{
// Start the game directly into the patrol state
TransitionToState(patrolState);
}
void Update()
{
// The current enemy performs the current state
_currentState.OnUpdate(this);
}
Copy the code
The type that defines a current state is an abstract class, which inherits subclasses with two different states. Use the TransitionToState method to switch between the two states, obtain the state variable assigned to the current state, and make it perform the function state method at the beginning of the game. And keep moving back and forth in the Update.
3.3 Adding the Attack Status
Using the abstract class, we’ve already added the patrol state to Enemy and the attack state in the same way.
public class AttackState : EnemyBaseState
{
public override void EnterState(Enemy enemy)
{
// TODO just entered attack mode
}
public override void OnUpdate(Enemy enemy)
{
// In TODO attack mode, always pay attention to the character's pursuit, attack and other methods}}Copy the code
Enemy also needs to have an instance of the attack state in Enemy.
// Get the object in the attack state
public AttackState attackState = new AttackState();
Copy the code
You can then implement the separate method in the state by simply switching between the two states using the TransitionToState() method. At the same time, infinite state expansion can be realized by using this method.