How can I make an player react to(Detect) AI punches

Hey Guys! I’ve an AI character in my game which does a few things and when it locates the player, it starts running towards him and starts to attack him (melee). But the problem is that my player doesn’t react to these attacks.

Hands of the AI character goes through the player. How can I make him to react to the punches?

Do I need some physics settings? Or Do I need to implement a blueprint interface to convey a message between the two characters. I don’t exactly know

Please help.


You’ll need something firing overlap events. You can create capsule/sphere components and attach them to the hand bones of your enemy BP’s skeleton, configure the collision channels so that they generate overlap messages on the player’s Physics Asset/hitbox capsules.

Then you’ll need to do some tagging; for example, I just tag all relevant capsules as “hitbox” or “hurtbox”. Then you can either go to the character BP for your player, use OnComponentOverlap with his hitbox, do an “Actor Has Tag” to check if the overlap was fired with an enemy hurtbox, then run to a branch from that which sets off the hit reaction and damage stuff… OR you can fire OnComponentOverlap for the enemy’s hurtbox in his own BP, and then transmit the relevant functions to the player using Cast To to call functions. Either way works, and you’ll likely end up performing some Cast To’s either way to get relevant data (for example, my attack animations all have notifies which specify which “state” an attack is in both to tell the attacked character what KIND of attack he was hit with and also to specify that it was an attack and not, say, an overlap triggered by a walk animation or whatever. So while my damage recipience is triggered hitbox side on the enemy, I still have to cast to the Player Pawn to get that information via set variables, and then cast back later to report information relevant to the player like how to adjust his combo counter, etc).

That means, first I’ve to setup the Physics Asset for the character that I want to react? And then, I’ve to use animation notifies to detect if the player is hit or not. Have I got it right?

Well, you probably need a physics asset… But notifies aren’t what determine if contact is made. They just flag animation times as “if we overlap here, do this kind of damage” and “if we overlap here, do nothing.” It’s OnComponentOverlap messages that actually fire off hits.

You need some sort of assets to act as a “hitbox” or “hurtbox” for relevant characters that can perform these overlaps. PhysAssets work well for this. They don’t HAVE to be, though; you could just as easily manually place capsules in your BP Component list and set them as children of the skeletal mesh component. This is helpful if, like me, you have a scaled-up skeleton, since you can’t generate proper physics assets for them.

It also helps if you want to fine- tune your collision, as you can easily create hitboxes separate from your physics simulation (e.g. Maybe you have Ponytail bones to simulate physics on your player’s hair, but you don’t want him to take damage to the hair, only the skull. Maybe you only want melee attacks to register if they hit the torso, so that players don’t take damage if the very tail of the swing catches their foot). Plus you can easily view them in-game and fine-tune their position, which is tougher to do with physics assets.

Point is, melee occurs when one skeletal mesh or attached component (weapon, etc) crosses over another one thanks to animation. So you need to create components which can generate overlap events specific to each other, use the animations to determine how/whether those overlap events should have an effect, and then create logic which turns those events into damage and reactions.

I’ve attached Collision sphere on the Hand Sockets. Right now when the AI character hits the player. The player detects the overlap event and the debug message is displayed. However, there is still a problem that hands of the AI player kinda goes through the player. How can I fix this. Do I need to create a physics asset for the player to detect collision.

You pretty much can’t fix that. Animation-driven assets won’t respond to collision in that way.

You probably want to use a mixture of reaction animation and launching (so the player is bent or flung backward out of the way of the fist), which is what most games do to hide that overlap. If you make the overlap spheres a little bigger, you can fire that reaction soon enough to avoid visible clipping (or you can just cover it with lots of hit-reaction particle effects!)

You COULD try physassets with physics-animation blending to help shove the physics assets apart, or inverse kinematics to stop the hand short, though doing that is a real challenge, and there’s no easy way to make it work. I can’t even name any games where skeletons respond physically realistically to melee contact without any overlapping, except Overgrowth, and that game is basically just a tech demo for the ability to do that.

I did that and its working ok for now. But I still have one more question if you don’t mind. How can I detect whether the player is getting attacked from the front side or back side? So, I can play different reactions depending upon that. Or I should just make the player face the enemy if it is getting attacked?

Well, what I do is I find the look at rotation for the enemy to the player, and compare it to the current rotation using Delta Rot. If the absolute value of the yaw of the delta between them is less than 90° (i.e. the difference between the way the enemy is looking and the way he’d have to look to face the player is less than 90° in either direction) I register it as a “front” hit, otherwise it’s a “back”.

From there I rotate the enemy to either be facing perfectly toward or away from the enemy (I snap him to one of the two directions), and then I use that bool to tell the Animation selector whether to use the front or back animation (I have about two dozen animations, and each separate attack sets both a front and back anim Name var. When the attack’s damage is processed, I select either the front or back var based on the aforementioned rotation bool and then pass it to the anim BP.

Personally, I also use physics blending, and fire a trace on contact to determine the exact bone, location and normal of contact, and use that to add force to the bone of the semi-ragdolled enemy. So his reaction is based on him being launched, playing an appropriate anim, and partially ragdolling with force applied to the point of contact. That might be overkill for you, I do it because you’ll frequently hit 2-3 enemies with the same attack and it looks pretty silly having them all play the same exact animation (it’s the Musou effect)