Enemy AI - Interrupting Behavior with Actions (Long--Get Some Coffee)

Cr4shBarcode 5 months ago in Behavior updated 5 months ago 3

For this example, I'm going to be referencing the mobile game "Granny", a very simple game that has some mechanics that I'd like to copy but don't know how. There are time stamped links below to illustrate my points.

There are three things that the enemy AI in this game does that I'd like to copy:

1) While in Patrol Mode, AI can open closed doors and continue where Patrolling left off

Example at 2:02: 

The problem I have is as follows:

Right now I have a Patrol routine that lets the AI wander around the level. When the AI interacts with a door, the Patrol routine switches to an "Open Door" action I've created. Afterward, the AI wanders away to restart the Patrol from the beginning. It doesn't continue where it left off in its Patrol.

In the "Granny" example, the AI opens the door and continues the patrol where it left off, going into the room it just opened, etc. 

How do I achieve this? 

2) While Pursuing the Player, can A) open closed doors and Pursue Player and B) Struggle with a locked door for a few seconds before opening it and continue to pursue the Player

Example at 2:56:

The problem I have is as follows:

I asked this question several months ago and was given the suggestion to add a trigger to the door itself, to make it open automatically when the AI enters near it. However, I want the AI to struggle with it a bit, especially if it's locked, like in the above example, to give the Player a couple extra seconds of escape time.

Is there some way to pause and restart a behavior that perhaps I don't know about? 

These AI mechanics would be easier to achieve if there was a way for the AI to pause in the middle of a behavior (like Patrolling/Chasing), complete and Action (like Open Door or Struggle to Open Door) and continue the behavior where it left off. It would definitely solve the problem of the AI restarting a behavior from the beginning upon completion of the action. 

If the Enemy is pursuing the Player and the Player slams a door in its face, I want the Enemy to continue its pursuit once it gets the door open, not just open it and wander away in boredom.

Is there a way to do this?

3) Interrupt Patrol to Investigate Noises that the Player Makes (Dropped Items, etc)

Example at 2:58:

This one I flat-out don't know how to achieve, and I was hoping for some advice. I added a Global Variable with a Vector 3 input so that maybe dropped items can, on impact, add their location to the Global Variable and the AI can hurry to that location? 

Honestly I don't really know how to set that up. Maybe someone has some experience with this.

Thanks for reading this. I've been struggling with these things for awhile.

Unity version:
Game Creator version:

First of, let me say that it's indeed a good idea to allow to resume Actions from where they were left of. I'm opening a ticket and assess if this is something that could be added.

But focusing on what's possible at the moment, I think that struggling with opening doors should be part of the patrol. 

So imagine the enemy goes from A to B and from B to A (as long as she doesn't hear the player). If between A and B there's a door, you could have the following patrol actions:

- Character Move to door.

- Call Conditions door and wait

- Character Move to B

- Character move to door

- Call Conditions door and wait

- Character Move to A

- Restart Actions

Now the Conditions "door" should check if the door in front is closed (you can use a Local Variable Bool for this). If the door is closed, play the gesture "struggle" play some sound effects and open the door afterwards. Remember to flip the "is-closed" Local Variable to false.

If the door is not closed, simply do nothing and let the actions continue.

This will allow the granny to stop in front of the closed door, but if it's open, will simply cross it.

As for dropping items and making noise, this is something that's been long due and hope to work on it very soon. However, while the Noise and Hear system is not available, you can work around this.

Your initial idea is the way to go: Instead of storing the position of the object that fell/shattered/made noise, store it in a Global Variable of type Game Object. This Global Variable (we'll call it "noise-target") should be empty by default.

Every time an object makes noise, simply use the Game Object Variable Action and assign the object to the "noise-target" variable.

To let the granny listen to these noises, you can use a Trigger "On Variable Change" and listen for changes on this "noise-target" variable. Every time this trigger is called, means somewhere there was a noise. You can use the "Move Character To" action and use the target the "noise-target" object. This will send the granny investigate where the sound came from.

Hope this helps! :-)

Okay I think I was able to solve most of these problems, so I'm going to post here in case someone can use the information!

1. How to Make AI Use a Door

The Door needs an Enter Trigger and an Exit Trigger, both should be attached to a box collider with the trigger function enabled. Your AI should have a Tag, like "Character". The trick is to use the "On Tag Enter" Trigger to make the Door animate to the open position. The Exit Trigger is necessary so that the AI doesn't affect doors it's not in front of. Use Conditions to make sure the character isn't trying to open a Door that is already open. The other VERY IMPORTANT thing is that when the Character has "Can Use Navigation Mesh" enabled in its Advanced Parameters, it will walk through Doors on its way to somewhere else. Disabling this makes them stop at closed Doors long enough to open them, but then they will bump into and get trapped on walls. I'm currently trying to figure out how to toggle the NavMesh parameter via Action.


2. AI Patrol and Investigating Noises

Assuming you're working with the Behavior Module, I'll post my Behavior Tree below. It allows my enemy AI to Patrol, become distracted to investigate a noise, and chase the Player when it sees them. Additionally, it allows for a jumpscare sound to play when it first spots the Player.

To have a dropped object make noise, you need to Tag the object (ex. "Noisemaker"), and you need a Tag on the Floor (ex. "Floor"). You need a Global Variable (GameObject) that will store the name of the object that hits the ground. You need a Bool on the AI that we can toggle when it becomes distracted. Lastly, place a "On Collision Enter with Tag" Trigger on the object and in the "Store Self" area add the Global Variable you just made. Add an Action to the trigger that toggles the AI's "Distracted" Bool. The Behavior tree will make sure that the AI moves toward the Noisemaker Object when and if "Distracted" is true. Don't forget to toggle off the "Distracted" Bool when it's done investigating.