Use the timestamp i suggested, if two colliders from the same body get hit in approximatly the same time they count as one, you can modify the thereshold ro suite your use case
SparrowsNest’s method should work perfectly well. Here is another solution:
using UnityEngine;
public class MultiColliderCollisionTest : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D collision)
{
bool isFirstCollider = GetComponents<Collider2D>()[0] == collision.collider;
if(!isFirstCollider)
{
Debug.Log("Collision ignored because was not the first collider.");
return;
}
Debug.Log("Collision detected");
// react to collision
}
}
float lastHit;
void OnTriggerEnter(Collider col) {
if (Time.time - lastHit < .1f)
return;
lastHit = Time.time;
//do other stuff
}
you can change the .1 to what ever the difference you want, have it hardcoded like here, or what ever kind of variable you want.
This was placed on a physical weapon that called a damage function on the collider it hit, should also work from the other side.
one problem with this is if you collide with two different things at the same time only one will count, so maybe a an extra check to see if it’s from the same body will be needed, but I havn’t found a problem with not taking it into account in my use case.
I think it does what i really want and need to do. When you add this component to parent GameObject, it will combine all colliders from children GameObjects. So it will be One collider as i understand. I found a video describes how to use it. Thanks
I remember stumbling upon this some time ago, must have slipped my mind…
I wish there was a 3D equivalent for this, that’s probably why I forgot it existed.
Adding a bool definitely does work FYI. It gets called the first time and passes through the bool. On the second collision it doesn’t get past the bool. It doesn’t happen at the same time, one must be called before the other.