Improve AI Behavior: Returning to Idle State
Table of Contents
- Introduction
- Setting up the Guard Spot
- Modifying the Behavior Tree
- Changing the Move Speed
- Adding the AI State Change
- Cleaning up the Behavior Tree
Introduction
In this episode of the AI Guard series, we will be expanding our Behavior Tree to improve the AI's behavior. We will ensure that if the guard doesn't find anything during the investigation, they will return to their original guard spot and idle state. We will also Create functions in the Behavior Tree to change the AI's move speed and state.
If You're interested in the series and would like to download the project files, you can become a member of the YouTube Channel or Patreon. Members will have access to premium channels in the Discord server and can download project files from the channel. However, if you don't want to become a member, you can still check out the Discord server, where there are a variety of free project downloads available. Let's get started with today's video!
Setting up the Guard Spot
To ensure that the guard can return to their original guard spot, we need to store and assign the guard spot as a variable. This variable will be added to the Blackboard, which is where we store information in the Behavior Tree. Additionally, we will add the guard spot as a variable in the guard character blueprint. This will allow us to assign the guard spot when spawning the guard or placing them in the level.
In the guard character blueprint, we will set the value of the guard spot on our Blackboard during the Begin Play event. This ensures that the guard spot is assigned correctly when the game starts. We will also add the guard spot to the level and assign it to the guard character.
Modifying the Behavior Tree
Next, we need to modify the behavior tree to make the guard return to their guard spot if they don't find anything during the investigation. We will replace the weight node with a task node to move the guard to their guard spot. After reaching the guard spot, we will add another task node to rotate the guard towards the correct direction.
We will also create a task in the Behavior Tree to change the AI's move speed. This will allow us to easily adjust the AI's movement speed from the Behavior Tree. We will use the Pawn interface to access the character's movement component and set the maximum walk speed.
Changing the Move Speed
Now that we have added the function to change the move speed in the Behavior Tree, we can adjust the AI's movement speed Based on different conditions. In the Behavior Tree, we will call the task to change the move speed and set it to a faster speed when the guard spots a player. After returning to their guard spot, we will set the move speed to a slower value to make the guard walk instead of run.
Adding the AI State Change
In addition to changing the move speed, we also want to be able to change the AI's state from the Behavior Tree. We will create an interface function in the AI controller to change the state and add it to the controller blueprint. Then, we will create a custom event in the AI controller blueprint to change the state to idle. This event will update the Current state on the Blackboard and reset the perceived times array. We will also call this event when the guard returns to their guard spot.
To implement the AI state change in the Behavior Tree, we will create a task node that calls the interface function to change the state. We will promote the new state as a variable to make it editable from the Behavior Tree. Then, we will call this task node after rotating towards the guard spot rotation in the Behavior Tree. This will ensure that the guard changes their state to idle after returning to their guard spot.
Cleaning up the Behavior Tree
Finally, we will clean up the Behavior Tree to improve its effectiveness. We will add a composite sequence at the beginning of the tree to check if the guard is idle and at their guard spot. If not, the guard will move towards their guard spot. We will then move the nodes that were previously disconnected to this sequence. This will ensure that the Behavior Tree skips unnecessary computations if the guard is already idle and at their guard spot.
Highlights
- Setting up the guard spot to allow the guard to return to their original position
- Modifying the Behavior Tree to make the guard return to their guard spot and rotate towards it
- Changing the guard's move speed based on different conditions
- Adding the ability to change the AI's state from the Behavior Tree
- Cleaning up the Behavior Tree to improve its effectiveness
FAQ
Q: Can I download the project files for this series?
A: Yes, project files are available for download to members of the YouTube channel or Patreon. Check the Discord server for additional free project downloads.
Q: How can I change the guard's move speed from the Behavior Tree?
A: Use the "Change Move Speed" task node and set the desired move speed as a variable.
Q: What happens if the guard doesn't find anything during the investigation?
A: The guard will return to their original guard spot and enter the idle state.
Q: How can I change the AI's state from the Behavior Tree?
A: Use the "AI Change State" task node and set the desired state as a variable.
Q: What improvements will be made in the next episode?
A: The next episode will focus on adding the awareness icon and implementing hearing perception and damage response.
Resources: