Hi everyone,
I’m fairly new to Unreal Engine, and I’m currently working on setting up a basic enemy AI using Behavior Trees. I want the enemy to detect the player and follow them around the level.
So far, I’ve done the following:
-
Created a basic NavMesh and placed it in the level.
-
Set up a basic AIController for the enemy character.
-
Created a simple Behavior Tree to move the enemy to a target location.
But I’m having trouble getting the enemy to continuously follow the player. I’ve read that you can use AI Perception to detect the player, but I’m unsure how to integrate this with the Behavior Tree so that the enemy keeps following the player once they’ve been detected.
Here are a few specific questions I have:
-
How do I properly use AI Perception to detect the player?
-
Once detected, how do I set up the Behavior Tree to make the enemy follow the player in real-time?
-
Do I need to use a specific blackboard key to store the player’s location for the AI to follow them?
Any advice or beginner-friendly tutorials would be greatly appreciated. Thanks in advance!
Hi Welcome!
There are many aspects to it however I suggest starting with a tutorial like this one, something nice not a hacky one.
In terms of your question in high level
- Think about states. Let’s say your AI logic have states of normal, alerted, agroed, dead. In each state AI should behave differently. Like in Agroed state it attacks all time, maybe hides a bit, if too much distance walks to you, if distance is right tries shoot, or maybe so close uses a shield ability etc. In short once AI is in agroed state (Once it detected an enemy) you don’t have to continuously detect it, it already knows that there is an enemy and acts on it. In further stages you can define what makes AI go out of this state? Does it follows to the enterance of the dungeon? Turns back and goes to its original place etc. etc. The point is start with the States (contexts) of what AI is in.
- Once a state is changed you can have a selector in behaviour tree and just that part of the behaviour tree runs. Like EnumState=Agroed->CheckDistance->MoveToTarget->TryShoot->EQS Strafe->MoveToLocation(Strafe)->CheckDistance->TryShoot ->CheckTargetIsValid? (not dead) etc. and you can loop in this state forever if you like. A better more advanced way would be using State BlackBoards this way logic is super extendable organised for bigger games and ai archetypes and its reusable however that would require quite a C++ knowledge. Start with small steps as Selectors in Behaviour Tree and Enums as state.
- Well once you have an attack target it is good to pass that to BB since you will do alot of things with it. Once you give the object(actor) reference of the attack target then you can query location, health of target etc on the behaviour (AI) Side
Start with that tutorial you will be fine and think around as designer advice
What states my ai has in my game in terms of the gameplay?
-Deploying
-Hybernating
-Sleeping
-Immobilized
-KockedBack
-Normal
-Alerted
-Agroed
-DBNO (Dead but not out, on the ground maybe looking for a healer? dying and making a suicide attack? )
-Dead
etc etc and the rest as you as in Agroed State defining the behaviour in simple terms.
Let me know if it helps.