How to make ai stop moving after colliding with a trigger box and playing an animation?

Hi!

I’m an animator trying to do a project for uni, which means I don’t really know a bunch about Unreal and blueprints and things. But, I currently have an ai which uses a behaviour tree to chase the player and roam when they can’t see them.

I’ve also got a trigger box set up, in the level blueprint, which will make the ai play an animation once they collide which works.

But I’m stuck with making the ai stop following and looking at the player once they’ve hit the trigger box, I’ve tried to look for things that talk about this online but I can’t seem to find anything that gives a clear answer.
The plan for this character is to follow the player, once they can’t see the player they will walk around (which it does), but then once it reaches certain parts of the map then it will sort of forget about the player? And will interact with the environment through animations. So for example what I’m trying to do now is that once this character walks into the triggerbox at a campfire it will stop and do various animations to show them interacting with this. But i’m unsure how to merge these two parts together so the character will actually stop trying to find/follow the player.

1 Like

I am not sure I get your question completely but try using animation montage which resets ai after triggering.

Likely you’ll need a boolean that is set to true when the ai character overlaps the trigger box - and false when the overlap ends. Then link this with a blackboard entry on the behavior tree that has precedence over the other trees. So, when they overlap, the character starts playing their animation and you can set the other parts of the tree to false. Or, go back to roaming. Does that make sense? I could try to mock up what you’re working on to give a more clear answer when I have some more time. It’s been a while since I’ve touched behavior trees.

In theory it makes sense but I honestly have no clue how to go about doing that so if you could do a mock up that would be very helpful! I really don’t have a lot of experience with Unreal, or at least not with this stuff!

OK, so I had some trouble figuring this one out, but I think I got it. I’ll do my best to explain.

You’ll need to [create an anim montage][4] if you don’t already have one. Then go into the animation blueprint AnimGraph and add the slot:

I’m going to assume you used the behavior tree [quick start guide][1] from the Docs based on your screenshots and build off of that assumption.

In Blackboard, make a boolean, BoxTriggered in my case.

On Behavior Tree, make a sequence between Chase and Patrol. Give it a Blackboard Decorator for when the new boolean is set:

Then, make a new BTTask:

Have the task play the animation montage on the character, using the return duration for a delay before resetting the blackboard boolean and finishing execution of the task.
Remember to expose the blackboard key (eyeball icon) and set it on the behavior tree task as well.

Then, on the AI Controller, make a function to turn on the BoxTriggered blackboard boolean and turn off the CanSee blackboard boolean (or haslineofsight, whatever you named it):

So now we actually need to fire off the trigger event. I made a new blueprint called BP_MyBoxTrigger based off of the BoxTrigger class. It has no code in it. I just placed it in the world.

On the AI Character, on begin overlap, we check for BP_MyBoxTrigger and if that’s the overlapping class we get the owning controller, cast to our AI controller and fire off that function:

Phew!
Please mark my answer accepted if this has been helpful!
If you have any more questions, feel free to follow up.

Happy developing,

Reishi

Hello!

I followed your directions but it isn’t working? The sequence doesn’t activate when the ai steps on it and i’m unsure what is wrong with it as i followed the steps you posted?

What does the behavior tree look like when the AI steps on the trigger? Does the BTTask fire off?

Without knowing exactly where this is failing it’s going to be very difficult for me to help you. There could be many many factors at play here. This is when you can use blueprint visual debugging.

While the project is running you can observe the blueprints working. It’s useful to have two monitors so you can play the project on one screen and have the blueprint you’re observing on the other screen.

The execution wires will pulse as they are executed, indicating where the flow of logic is going. Sometimes you don’t have access to another screen or need to examine in more detail. That’s where breakpoints come in.

You can right click on any node and add a breakpoint - or select the node and press f9. This will stop the code at that location and bring up the blueprint when the project is running and that node is executed. Then you can “step” over nodes one by one, observing them as you go.

Start with the beginning of the logic chain - in this case, the trigger event. I would add a breakpoint on the event begin overlap. If there is no breakpoint, you know it’s not reaching this point in the code, which means that either the actor’s collision or overlap settings are not correct.

Next, I’d look at the box trigger function at the end of this chain. If this fails, casting is failing. Then the box trigger function itself… Then the BTTask… Keep going down the chain of logic until you notice something behaving incorrectly.

I know this sounds tedious, but again there’s little I can do to help without having access to your project.

Please let me know if you find out specifically what is failing so I can assist further.

Happy developing,

Reishi

I re-read the question several times, and I think this may help:

Then you can play your animation.

Sorry for the over-complicated explanation on how to do this in behavior tree.

It seems you want to just be able to do this in the level blueprint. That’s not as extensible, so you’d likely run into issues with this approach if you were to scale the project, but for what you’re asking I think this would work.

The ai perception would likely detect the character immediately after line of sight is set to false, making it true again, though…

This ended up not working as well, I had a mess about with the stop logic things and made it work that way instead! Thank you so much for taking your time and helping me with this though, I really appreciate it!!

Now when the trigger has been activated it stops the behaviour tree, plays the animations, waits, plays the idle animation, restarts the logic to follow the player again and then waits again. I had an issue that it was in a loop of the trigger being activated and the last delay sorted that out!