Download

Help with 2d hit detection 4-26

Hello! I am working on a 2d platformer, and I am working on my enemy blueprint. I am trying to find out the best and most reliable way to detect for ‘hit on head’ impact. I have tried multiple things, and am currently iterating through a version of this;

Enemy is a paper2d character. It has a ‘box collision’ scaled proportionately to the sprite size placed above the capsule collider, and on the player bp (also a paper 2d character), there is a box collider on the ‘feet’. On the enemy I do an on overlap, check if the item is a foot collider and if so, kill.

I also have collision set on the capsule so if the player touches the collider, it causes damage to the player. The two collisions are interfering with each other (That is, when i jump on the head it kills the enemy, but it also triggers the capsule collision which hurts the player).

I am trying to find the most optimal way to allow the player to get hit when touching the enemy on any side except the head, and can’t seem to figure it out. I am trying to avoid using a line/sphere trace when the player is ‘not on the ground’ as I was told that traces on tick are super heavy in resources.

Any suggestions on what I could do to make it cleaner, and not cause so many issues?

FYI, I don’t like the look of ‘hit and stop’, so I made the two actors overlap each other, so theoretically the player could go ‘through’ the enemy, but since they get knocked back on hit, and the enemy dies when jumped on from the top, they shouldn’t ever go through each other.

Just a guess, but if i understand your problem correct, then your enemy only have one collision box for everything, which is currently placed on the head area, while your player have one on the feet just for this behaviour.

Then i would suggest, give your enemy one collision box for the main body, and one small for the head region, which is save to touch, for the players foot collision box.
If your players main body collision box comes in contact with the enemies main body collision box, you get an enemy kill and damage for player. However, if your players small foot collision box hits your enemies small head collision box, then only the enemy dies, but nothing happens to the player.

Also you can use line trace (box trace would be even better for this, since you want check for a box collision), since a) you can limit the range they check (they become resource heavy, if they have to check long distances), and b) since you only need one short ranged line/box trace for the players feet, the performance impact is neglibile, especially, if you limit the channels it has to test against. Many different line traces will become a problem, but one single short range trace is… just do it ^.^

The huge advantage of a line/box trace i see, is, they are way more accurate than overlap events, because they check every tick, while you could run into trouble, if you have multiple enemies close or overlapping each other, that your overlap event might not trigger correct, because it still registers A enemy in its effect area, not realizing, that it might not be the same enemy, but two different, that would require the event to happen.

In my case, i had replaced a ground check on the feet, which works similar to your collision box, just to check, if the player stands on solid ground, or not, because the overlap was unable to register the correct switches between different tilemaps or actors like platforms or trapdoors (that could be open or closed), that were not part of any tilemap. A small Box trace solved all this and made it very reliable and stable.