I have a composite prefab that consist of multiple objects (sprites with collisions etc). I need to create a list of all collisions at some point, I have this script that partially works:
// this works! - we add all collisions that are on "top level" object
List<Collider2D> list = new List<Collider2D>(this.owner.GetComponents<Collider2D>());
// this doesn't
foreach (Transform sub in this.owner.GetComponents<Transform>())
{
foreach (Collider2D c in sub.GetComponents<Collider2D>())
{
if (!list.Contains(c))
list.Add(c);
}
}
foreach (Collider2D collision in list)
Physics2D.IgnoreCollision(this.collider2D, collision);
My problem is that I have a player that is made of multiple parts, like legs etc, now when I shoot from a weapon, bullet ignores all collisions of player who shot it (thanks to this script) but it doesn’t ignore collisions of these extra parts like hands or legs, so at certain angle, the player can shoot themselves.