Blocking AI

I have a simple AI that follows a patroling path, when the character is facing you I want him to be able to not take damage and then when he turns around I want him to be able to take damage.

I have tried doing two different collision boxes on the same AI but that doesnt work. How can I achieve this? There is no animation to this character he just moves left to right moving left he is facing you and moving right his back is turned to you (If you dont jump over him that is) , he then goes to the end point, turns around an goes the other direction basically walking back an forth. (Side scroller game)

I just want his back to be attackable and his front to block your attacks.

Just so you can see an example of what I mean, here is a diagram of what I am explaining.

Bump, Any Ideas?

Well, it all depends on how your attack system works. Here are some ideas:

  1. If you want to do it with math :
    What you could do is take the Dot product
    of the attacker forward vector (or the attack direction) and the enemy forward vector.
    Dot Product | Unreal Engine Documentation
    Let’s talk about the dot product in game development | by BlueBubbleBee | Medium
    If the Dot product is positive, then they are looking in the same direction (aka player attacks would be from behind → negate damage). If the Dot product is negative, they are looking at opposing directions (aka player attacks from the front) → do damage.

This is very performant and cheap. Please note though that with the dot product alone you can only check how two objects are looking relative to each other (same direction, perpendicular or opposite directions), it does not tell you whether the player is actually behind the target. However, if your player can only attack forward, and thus the only time the dot product is positive when the player is attacking forward AND the enemy is looking in the same direction (aka away from the player) then using the dot product is probably sufficient.

I guess there is a mathematical solution to check whether A and B are looking at the same direction (Dot Product) AND B being positioned behind A, but I don’t have in my head right now how the second part (evaluation of position) could be done mathematically and then combined with the Dot product.

  1. Use line traces
    Give the enemy “armored side” hitboxes at their back, which have a different collision type than the normal enemy body (lets say your normal enemy hitbox has a collision object type “Pawn” and your amored side hitbox would have a new object type “Armored”). When the player attack hits the enemy, do a single line trace from the player weapon to the enemy center and select both “Pawn” and “Armored” as objects to trace for. If your line trace detects that it hit an “armored” hitbox as its first hit result → make attack not do any damage.

  2. Use overlap events
    When you do an attack, be it with bullets or a swinging sword, check for collision overlaps. If your first overlap detected is the main enemy body → you hit the enemy → do damage. If you hit a “armored” hitzone → you hit the armored part first → negate damage

Thank you for your response, I will look into this more!