NPC behavior: defend himself

Hi. Currently I am learning AI part of UE. I am using Dynamic Combat System, where I have one Example Character and one TreningDummy. I am trying to implement the responsiveness/reaction for NPC (like defence himself, attack or run away), only when main actor punch him or simply pulls out a weapon in front of him? Well, it was reccomented to me to create an event that adds the player character to the enemy list in the behavior component, each time when the actor will attack NPC. But this task seems to be difficalt for me :slight_smile: Maybe, someone have already implement such behaviour and can tell me which steps should I do to perform this task.
Thanks in advance.

Use event any damage inside the NPC that has the controller. Inside the controller you can use the actor update stimulus event. This will give you the actor that hit him.

After that you need to add a stimulus source inside of the AI perception as damage sensing. What’s nice is you can get the actor location and the hit location (maybe if you want police to show up at the scene of the crime) by right clicking the stimulus and clicking break structure pin. So if you want the npc to chase after getting damaged, you can store the actor and his location in the blackboard and call those variables in tasks during your behavior tree. Make sure when having the player damage someone that you use “apply damage” to hurt them. Also you need to add a stimulus source component to the player and add damage source. If I have time tomorrow I may make an example if you still need it. Hope this helps.

Thanks a lot for this explanations. I will try to follow all this steps. If you have time tomorrow, I will really appreciate some examples. Thanks again!

Here is the projectile setup I have in my current game I’m making. I apply damage and report a damage event with the instigator being the player character (his projectile). How you would do this is on the punch having a collision box where the collision component only overlaps when the punch action is active (stops you from accidentally overlapping). I added in the tag on my game in case the hit happens on an ally character it won’t hurt them. Another nice aspect of event hit is you can spawn an effect with the hit location, so if the punch has a shockwave you can spawn that niagara effect on hit.
Next you need to make the player character have a stimuli source like the image above.
Here we have the enemy character, damage his health float with the event any damage function. My branch here eventually plays a death animation on health reaching zero.

Make sure you add the AI damage sense config to the AI controller too. This tells the AI to look for perception updates when a damage event is reported.

Inside the event graph of the enemy AI controller now all you have to do is have its perception update to stimuli from the player character (the one punching in your case). Setup a CanSeePlayer boolean in the blackboard and then store this value from the stimulus.

In this example enemy when the AI can’t sense the player it will randomly patrol around. When it can see the player (in my case this is set true from hearing, seeing, and damage occurring) it will store the vector location of the player, move towards him, focus and shoot at him. Hope this helps.

1 Like

Wow, great, many thanks for this!! It’s really helpfull :+1:

1 Like