Event nodes example
This example shows how to use the event nodes to implement an agent that patrols an area and detects nearby enemies. The graph has two main branches: patrol and detection, and enemy detection. The agent follows waypoints on a patrol route and uses the event nodes to detect and respond to nearby enemies dynamically.
To create this behavior graph, follow these steps:
In the Project view, select Create > Behavior > Behavior Graph.
To open the behavior graph, double-click the created asset.
On the Blackboard, select + to add the following variables:
- Waypoints (GameObject): Define a list of waypoints for the agent to patrol.
- Sight (Float): Set the detection radius for spotting enemies.
- FoundObject (GameObject): Hold detected objects.
- Enemy (GameObject): Specify enemy targets.
- Enemy Detected (Event): Trigger enemy detection events.
Next, divide the behavior graph into branches. Branch 1 sets up the agent's patrol routine and enemy detection logic. It includes two sub-branches: patrol routine for waypoint movement and enemy detection events to monitor enemies. Follow these steps to configure each part.
Branch 1: Set up patrol and enemy detection
The first branch has two sub-branches: patrol logic and agent waits for the enemy spotted event.
- Add an On Start node and enable the Repeat option.
- Add a Cooldown node and set the cooldown Duration to
3
seconds in the Inspector. - Join the Cooldown node to the On Start node.
- Add a Run In Parallel node and set Parallel Mode to Until Any Succeed in the Inspector.
- Follow the instructions in the next two sections to create sub-branches for patrol routine and wait for the Enemy Spotted event.
Patrol sub-branch
Follow these steps:
- Add a Patrol node, for example, to move along waypoints.
- Join the Patrol node to the Run In Parallel node.
Enemy detection sub-branch
Follow these steps:
- Add a Wait for Event Message node.
- Join the Wait for Event Message node to the Run In Parallel node.
- Select the Event Channel link icon in the Wait for Event Message node to link it to the Enemy Detected event channel.
- Add a Log Message node and group it with the Wait For Event Message node.
- Set the message to
I've spotted the enemy! Alert!
in the Inspector.
Branch 2: Detect enemies
This branch triggers the Enemy Detected event when the agent detects an enemy.
Follow these steps to set up a process that triggers the Enemy Detected event when an enemy enters the detection radius:
Add a new On Start node and enable the Repeat option.
To add a Check Collisions in Radius node:
Join the Check Collisions in Radius node to the On Start node.
Assign Self for the Agent field and Sight for the Radius field.
To add a Conditional Guard node:
Group the Conditional Guard node with the Check Collisions in Radius node.
In the Inspector, select Assign Condition > Variable Conditions > Variable Comparision.
Select the link icon in the Variable field to link it with the FoundObject variable.
Select the link icon in the Comparison Value field to link it with the Enemy variable.
To add a Send Event Message node:
Group the Send Event Message node with the previous nodes.
On the Send Event Message node, select the link icon in the Event Channel field to link it with the Enemy Detected event channel variable.
Select the link icon in the Agent field to link it with a variable on the Blackboard and select Self.
Select the link icon in the Enemy field to link it with the Enemy variable.
Event nodes ensures prompt communication and coordination between different branches of the behavior graph.