- Home /
onTriggerEnter2D - How to know BOTH colliders involved?
Hi,
I have just started using Unity 2D. What I am attempting to do is add a CircleCollider2D component to my player but with a large radius and use it as "aggro" detection. In other words when the player's CircleCollider2D collides with an enemy's BoxCollider2D it triggers its EnemyAIScript making the enemy begin chasing and shooting at the player.
Unfortunately, the only parameter that onTriggerEnter2D gives me is the OTHER collider.
Since the player also has a BoxCollider2D around his body and I have no way of determining which collider did the colliding, I suddenly have a dead player (since colliding with anything that "isTrigger", such as enemies, triggers death.)
Is there any way to find out the identity or type of the local collider? Obviously, I could do proximity checking lots of ways, but it seems a shame if I can't do it this way because it would be so easy and (I think) efficient. Any help or insight is appreciated. Thanks!
I know that the parameter is a reference to the other object. $$anonymous$$y question is HOW can I get the identity of the local collider that is involved in the collision, because it is not given in onTriggerEnter2D. Any method would be welcome.
Your writing is in one block which makes hard to read.
As for your answer, remove the circle collider and use a distance detection ins$$anonymous$$d. The efficiency won't get hurt as using Vector3.Distance is pretty fast and efficient.
What you are after is just micro-optimization which is not required at the beginning of a game, particularly when it does not change much or anything.
I did some testing and here is my "new to unity" attempt at answering this...
1) Create a child object of your character
2) Add the trigger collider to that object, not your character
3) Add the script to handle the OnTriggerEnter2D to that object, not your character Since there is only one collision object, you know that it is your trigger
4) Add a public variable in your script that represents your character object in the inspector, drag your character node into the public variable location for your child node's script component if you need access to your player. (to set any variables etc...)
That should do it.
I did a quick test with my project and this seems to work for me.
I did a little cleanup here. Let's just say on track, eh? :)
Answer by Kiwasi · Nov 27, 2014 at 11:42 PM
Easiest way to solve is put a child GameOObject with the appropriate separated colliders. Then put the OnCollisionXXX on a script on the child GameObject. Then you can simply use the name of the GameObject to figure out which one was activated.
that will only show one collider. He wants both. But regardless, what happens is that the child is going to combine with the parent to create one single collider, so it cannot be separated.
Your answer
Follow this Question
Related Questions
Testing for collisions prior to placing an object 0 Answers
Collision between capsule collider 0 Answers
Collision detection of fast rigidbodies 0 Answers