OnCollisionEnter2D vs OnTriggerEnter2D for Attack Detection

Hi all;
I’ve been using Unity for a long time, but I could say I’m relatively new to 2D.

The project I’m working on is a 2D project, and for attacks between enemies and player I used 2D collisions. However, recently it started to give issues, like if an enemy or player attacks against an obstacle, it pushes back. Attacks between enemies and player cause weird jerks because collider pushes them.

I played with collision layers, but now it became too much of a mess and I don’t really want to play with it anymore.

My question is, what do you use for attack detection?
Should I switch to triggers instead of colliders?

Regards, Tansel.

1 Like

Thanks for great replies :slight_smile:

1 Like

Hey there, you should definitely use triggers to solve your problem. Having the colliders as a solid will end up pushing things away from the solid (or towards in some cases).
I create a gameobject through code and attach a BoxCollider2D and set it to a trigger. As far as I’m aware I think anything that has to be hit must also have a RigidBody along with it’s collider, otherwise the OnTriggerEnter message does not fire.

You can then use collision layers to fine tune your hitboxes to only hit certain objects. Here is a link to a collision chart I found online to help me figure what will hit what http://answers.unity3d.com/storage/attachments/19729-proof.png.
As far as I have tested as well, kinematic RigidBodies do not return a trigger collision for some reason either.

Hope that helps a little bit :).

3 Likes

I use a boxcollider2d for the body of my characters, a circle collider for the head of my characters, and a boxcollider2d with istrigger for the hitbox of my character. Then I use a raycast to determine attacks and such - I’m really happy with the results :slight_smile:

@Aedous , thanks a lot.
Currently, my system works as intended, but some problems arise like pushing obstacles, and I’m at my limit of collision layer settings. Do you think if I change all my melee colliders to triggers and change OnColliderEnter2D to OnTriggerEnter2D have the same effect?

@Rostam24 , thanks, but I can’t use raycasting for complex melee attacks.

Well, I’ve switched all my OnCollisionEnter2D code to OnTriggerEnter2D.

Worst thing I had to handle was getting information Collision2D provides but Collider2D does not. ContactPoint2D array was so useful, but I had to use RaycastHit2D for that information. So, it’s as @Rostam24 said, raycasting gets me necessary info.

I’m happy with results.

Nice one glad it worked out for you, if Raycasts get to cumbersome especially if your trying to be specific, try triggers instead on the character and on the melee attack. Good luck to you!

Trigger Collider2D is good. You can have multiple colliders on one gameobject but you need to make sure they are on separate layers and stuff so just avoid that.
OnCollisionEnter2D & OnCollisionExit2D

I’m not sure how accurate it is, but the feedback I got that for fast paced movements and collisions, triggers give a better performance.

So I might suggest to use that.

@Aedous , that’s an interesting idea, but for now everything works fine for me, so I won’t touch that :smile:

1 Like