Enemy/obstacle collision 2D

Hello,

I’m creating a 2D platformer where the goal is to kill enemies by jumping on their head (I know, not original lol). Anyway, my enemies are created, they’re walking around in my level. Now what rests is the collisions with my enemies.

![alt text][1]

examplespriteflipbook.png
(this is not how the actual flipbooks look)

I want to create a collision where, if I touch him from the front or back (red area) it damages me, and if I jump on the top (green area) I kill it.

Any suggestions would be greatle appreciated!

it comes down to how precise your stomping needs to be. If you don’t really need pixel perfect stomping, then placing some collision box would be the way to go.
So it comes to the idea of hit box and hurt box like in fighting games. your character will have hurt box all around and only have hit box around it’s feet, on the other hand, your enemy character will have hit box all around and only hurt box on its head area. so the only viable way for your character to stomp properly will be approaching from top of enemy, because otherwise your hurt box will over lap enemies’ hit box first.

But if you need pixel perfect stomping, you might need to develop some custom collision shape or fancy collision detection in order to avoid some traditional glitches that are present in almost all stomping games.
However, I don’t think that’s really necessary, since if you place your boxes properly, it wouldn’t really affect gameplay at all.

No I don’t need pixel perfect stomping. I just need a collision where if the character jumps on the head, they die/get hurt. However if they touch me, I lose a life. I’ve tried and tried, examples, tutorials etc. I just can’t seem to get it to work.

Anyone any ideas?

Hi Penhoat,

The way the original Super Mario Bros. handled it actually had nothing to do with the head, and only depended on the player velocity. If Mario was falling when he touched an enemy, then that was a ‘stomp’, but if Mario was level or raising (jumping up) then it counted as a ‘bad touch’ and he lost a life or his superpower.

This is pretty easy to implement, just define the collision shape for your enemies, and in the collision response, look at the player’s Z velocity. If it is less than 0 then stomp the enemy, otherwise if it’s >= 0 then the hero is jumping or standing and should take damage.

Cheers,

Ha, Mickaek everywhere to bring help on 2D Developer :slight_smile: , anyway as said you can based on Velocity simple, and Also check the Current X position of the Player depending on what you expect !

Hello, I’m sorry with the late response to your solution. Your solution sounds amazing and great, yet I have no idea how to make a blueprint like that, could you give me some help with it?

You could have the Hit event of the player trace downward for the enemy and kill said enemy if the trace returns an enemy. If it doesn’t hit, it could trace in any other direction (up, left, right) for the enemy and hurt the player if any of those return true.

All these solutions sound amazing, the thing is, I’d like to know how to actually make the blueprint for that. If anyone could help me with that?

This is untested, but this should be pretty close to what you’re looking for.

EDIT: To clarify, this code should exist within the player character. This is assuming that you want attacks to be instant death, and that all enemies are subclases of “pawn”. These particular things might change depending on how you’ve coded your game.

Compiling it works, however for it to work I’d need my “enemies” to work as well

What’s wrong with the enemies?

These are my blueprint and component of my “enemy”


It has no effect on him whatsoever

I have an idea. I’m at work so I don’t want to drill into what Ray posted at the moment. But another idea that might work for you, especially if you’re going to make a lot of enemies is this:

You can set up trigger volumes (which would be like a hit box) on the enemies head. When the character overlaps that trigger volume then you can destroy the actor (your enemy) (you may need to play a death animation > then destroy actor for immersion). I’ve also not tested this but it could be as simple as this:

Now, you’d want a specific box. See how it says “Trigger box copy” ? That’s because I made a trigger volume, named it trigger box (made a copy for this thread), and when my character walks through it the game will spawn an enemy. Instead of the game spawning an enemy, your game will destroy the actor. There are other options besides overlap, like hit, if you want a collision and not to just pass through it.

In the image above you’d place that in your character blueprint. As long as you only ever used your “kill box” (whatever you name your trigger volume) on enemies, then whenever the character/player overlaps with that box that enemy (actor) is going to die (be destroyed). You don’t want to use that box on other instances or things, because you might accidentally destroy whatever instance of whatever you applied it to if your character triggers that function (don’t put that same trigger volume on a platform or something, if your character landed on the kill box he’d destroy the platform and you’d fall down into nothingness or a kill box or whatever).

The target “other actor” feeds into the “destroy actor” because you want the OTHER actor (not your character) to be destroyed.

I can give this a test run later when I’m out of work.

edit; for quick testing you can use an “attach to” function to attach the trigger volume to a socket on your enemies head (You’d have to look at the skeleton and make a socket in the right spot). If you’re not sure how you’d get the trigger volume on your enemies head this a quick way to test it.

A couple possibilities here:
Your enemy sprite may not be blocking the visibility channel, and
Your enemy may not be a subclass of pawn. You will need to change the class defined in the “Class is child of” node in the player code to whatever the parent class of your enemy is.
EDIT: Also, turn on the debug (set the little drop down on the single line trace node to “persistent”) for the line trace and take a screenshot after you hit something. I may have just done the math wrong with those vectors!

I haven’t used Paper2d much, but you should be able to make a box that is a component of your player and enemy characters directly in the blueprint component screen. One way I solved this same problem ages ago with a platforming game I did was that I put an invisible collision box at the feet of the player that killed enemies when it collided with them. It was a kind of sloppy solution though, as it meant if you were moving fast enough horizontally or hit it at just the right angle, you could kill enemies without actually having hit the top of them. The raytracing used in my proposed solution is supposed to fix this issue by making sure that the enemy is indeed below the player.

Thanks a lot. I will be trying it all out, and will get back with the result

I’ve tried it out. The TriggerBox is attached to the mummy, I made the blueprint like you did. However, it doesn’t seem to do anything at all in the actual game :frowning:

I’d like to thank both of you. I’ve succeeded with the problem. Simply by trying and testing, and I’ve made it work.

I’ve made my normal damage collisions work as well as the kill collision by use of the trigger.

Thanks to both Ray Jones 321 and zlspradlin