2D Triggers detecting when overlapped

Hi,
I’m trying to make a simple fighting game, and I’ve become stuck on a OnCollision problem.

I have two sprites each with a rigidbody2D and a sprite renderer on the parent, and each with a child object containing the colliders (both sprites colliders are setup as triggers) and the movement / detect hit scripts.
The circle collider on character A is off by default, character B’s is always on.
When the character A moves next to character B and throws a punch, I turn on character A’s circle collider using the event trigger in the animator.
The collider that is turned on is inside character B’s collider and is not detected in the script - which is where the problem lies.

I tried using the OnTriggerEnter2D, stay and exit but none of those register a collision.
I thought the physics2D.istouching would do it, but that doesn’t seem to work either (or at least not for me).

This whole punch all worked fine until I realised I needed Character B to have a rigidbody as well and now it won’t work.

I’m just not sure exactly which bit I’ve got wrong.

Any help / push in the right direction would be greatly appreciated.

Thanks

Steve

When colliders collide and they both have rigidbodies, they activate “OnCollision” functions instead of “OnTrigger” functions. These functions give you a “Collision” rather than a “Collider”. Unity - Scripting API: MonoBehaviour.OnCollisionEnter(Collision)

Check out the 2 charts on the bottom of this manual page to see under what conditions Trigger and Collision functions get activated: Unity - Manual: Introduction to collision

Good luck!

1 Like

Thanks for your time, I’ll give that a try - I’m currently messing around with the idea of using Physics2D.OverlapCircleAll’ and an array.

Note that the “All” and “NonAlloc” suffix 2D physics queries are tentatively deprecated (you won’t get a warning in the console).

You should just use the main function name for any query. For instance, Physics2D.OverlapCircle provides all the overloads you need including accepting a generic list that will automatically resize. Note that all physics queries accept lists now so you can reuse it and have no GC waste.