Examples
Last updated
Last updated
The Logic add-on comes with two dedicated FSM examples.
This demo scene requires some setup to make sure it works properly.
Make sure you have two layers in your project. (Player & Obstacles)
Assign the Player layer to the player game object in the scene.
Assign the Obstacles layer to the Obstacles group and its children.
Open the Data Library of the demo scene and select the Agent Data. Assign the Player layer to the target layer and the Obstacles to the obstacles field.
The Agent AI Component is a component which is assigned on the enemy game object. It contains all relevant data and scene references we need for the FSM to work. This way we only need one scene component reference for the graph. All example AI nodes (Follow Waypoints, Follow Target, Vision) rely on the AgentAIComponent. The AgentAIComponent script is assigned on the Scene Components of the Logic Controller.
The AgentData is a simple DataObject which contains all Agent relevant data, such as walk speed, view distance and so forth. The AgentData is assigned on the AgentAIComponent script.
The FSM in this demo is very straight forward. As you can see we have three states (Patrol, Follow and Wait). When a FSM Controller gets started, it will always start with the first(top) state. On the Patrol state two FSM actions are being executed, the Follow Waypoints and Vision actions. Each actions has appropriate outputs. In the Patrol state we only wait for a target (Player) to be detected. If this happens the Switch State node is being called which changes the current state to the Follow state.
The follow state executes two FSM actions, Follow Target and Vision. The Follow Target action simply follows the target which is assigned on the AgentAIComponent in the scene. When target has been reached, it will call the target reached output. In this case we have a simple Debug.Log node. But logically for production we would rather switch to an attack state. On the Vision action we only wait for if the target is out of sight (Target Lost). If so, we then switch to a Wait state. Which then switches back to the Patrol state.
The Wait state does not have any FSM action nodes. Instead we're using an Action Split node which splits the FSM actions into three calls - OnEnter, OnUpdate and OnExit. As we want to execute the wait only once, we connect our nodes to the OnEnter output. First we stop the agent with the StopAgent node. Then we wait for a few seconds, before switching the current state from Wait back to Patrol.
The second demo is a very simply state machine which waits for an input for a certain time. It nicely shows the use of FSM action nodes in combination with conventional Logic nodes.