I have my own collision detection system, and part of this system is a ‘Mover’ class attached to an empty transform with many Collider2Ds as child objects.
I often need to check a collision to see if it has hit a Mover type, and access the Mover class; using GetComponentInParent each time would be inefficient so I’d like to cache the structure somehow.
So here are some things I’ve considered, but I don’t really have enough knowledge to make a decision, which is why I’m here! It’s possible I’m missing some obvious answer.
Idea 1:- Keep a single List that every Mover registers itself into when spawning. Then within each mover have a HashSet. Populate the HashSet when the Mover spawns using GetComponentsInChildren. When testing a raycast collision, iterate through the Mover list and check the Hashset for the hit.collider
Idea 2:- Keep a single Dictionary. Populate the HashSet when the Mover spawns using GetComponentsInChildren. When testing a raycast collision, search the dictionary using hit.collider as the key.
Alternatively I could place all my Mover objects into their own physics layer and query the layer - though I still need an efficient way to access the Mover class after confirmation.
(edit) Clarification of the transform structure,
Root
Mover
Collider2D
Collider2D
Mover <------. want this
Collider2D \
Collider2D <------ Hit this
Collider2D
Mover
Collider2D
etc