- Home /
The question is answered, right answer was accepted
Find colliders after collision?
I know it might sound a bit redundant but I need this for my current game.
Does anyone have any idea how can I return an array of colliders that well, collide?
Basically I have an object that collides with walls, at a certain point I want to know the number of walls it collides with.
FindObjectsOfType(Collider) doesn't seem to work as it returns all the objects in the scene that have a collider. I would need something like this that could work simply for when a tag is hit.
EDIT: To be more clear, I'm trying to find the colliders that I'm currently colliding with.
EDIT2: To be even more specific, I only need to count the colliders that I'm colliding with as I need an action to happen when a certain number of colliders of one tag and a number of colliders with another tag is reached.
Your question is not clear; what colliders are you trying to access? All colliders currently being collided with, or all colliders that have been collided with?
I'm not sure if I got your question but: On the script you have on the object add the OnCollisionEnter-$$anonymous$$ethod. It will be called each time your object starts collidiong with another collider.
If you want to count collisions you can simply increment a variable in this method.
If you want to save the collider for later use you can add the collider (which is passed as parameter in the OnCollisionEnter function) in a list you declared earlier.
That wouldn't work, as I said I want to know the number of colliders colliding with my object at the same time as this is going to occur a lot.
Answer by iwaldrop · Jun 25, 2013 at 07:47 PM
Keep a list of colliders. When OnCollisionEnter/OnTriggerEnter occurs, add that collider to the list. When OnCollisionExit/OnTriggerExit occurs, remove that collider from the list. You now have a list of all colliders that you are currently colliding with.
Not sure if it would work for multiple objects with colliders using the same tag.
O$$anonymous$$ then, thanks. I currently am unable to test this theory as I'm not at my build PC. I will come back here to mark your answer as correct if this works tomorrow ^_^.
Thanks for the quick replies!
Ok, it worked. I still have some $$anonymous$$or glitches like making sure each collider is counted only once, but I'll try to figure that out on my own :)
Answer by HunterKrech · Jun 25, 2013 at 07:51 PM
In unity script you would use something like this(Some what pseudo code, i don't know uniscript syntax)
var enemyColliders = new Array();
function OnCollisionEnter(collider : enemies)
{
enemyColliders.push(enemies.gameObject.collider);
}
Now you can push, pop, and shift colliders as necessary.
I do not need to push, pop or shift the colliders. I simply need to count them.
Well after you push them aka add, then you can count them by enemyColliders.length