- Home /
Two Polygon2D Collider do not Collide with Each Other (Solved)
I am making a game where objects fall from the up side to bottom and my character will collect them when they touch it. Both objects and character have polygon collider. Update "Also both have rigidbody2D"
The thing is the collision is working fine, objects can hit to my character. The problem is I can not use this collision in my code.
void OnCollisionEnter2D(Collision2D col){
if(col.gameObject.name == "character"){
caughtByCharacter ();
}
}
void OnTriggerEnter2D(Collider2D col){
if(col.gameObject.name == "character"){
caughtByCharacter ();
}
}
Both of the collision and trigger does not work. What is the problem? Is it because of polygon collider's rules?
**Thanks for all replies. When tanoshimi mentioned the character name I figure out my problem. Actually it is not about the gameobject name. Something happened because of my carelessness.
I have wrote that collision code inside the spawner not the objects falling. Sorry for bothering you all with it.**
It could be so many things:
A Debug outside the if statement Works ?
Is the script on the same object as the collider and rigidbody?
Check the object name, is it realy "character".
Have you tried with tags ?
In what speed this objects colide ? if its too fast, try to set interpolate.
Thanks for reply. I have solved the problem you can check my reply at tanoshimi's answer.
Answer by Desert-Tiger-Games · Jul 02, 2016 at 02:42 PM
Thanks for all replies. When tanoshimi mentioned the character name I figure out my problem. Actually it is not about the gameobject name. Something happened because of my carelessness.
I have wrote that collision code inside the spawner not the objects falling.
Answer by tanoshimi · Jul 01, 2016 at 06:35 PM
Collisions won't be registered unless at least one of the colliding objects has a rigidbody2d component.
Thanks for reply. Both of my objects and character have rigidbody2D.
And are they called "character"?. When debugging collision (or any callback) functions not working as expected, always start by placing a Debug.Log immediately inside the method to see whether it even gets called:
void OnCollisionEnter2D(Collision2D col){
Debug.Log(gameObject.name + " collided with " + col.gameObject.name);
}
void OnTriggerEnter2D(Collider2D col){
Debug.Log(gameObject.name + " triggered " + col.gameObject.name);
}
Thanks for reply. When you mention the character name I figure out my problem. Actually it is not about the gameobject name. Something happened because of my carelessness.
I have wrote that collision code inside the spawner not the objects falling.
Your answer
Follow this Question
Related Questions
How to make slider joint immovable by the player 1 Answer
My ground checker only works... sometimes 1 Answer
simulate gravity in a 2D game 3 Answers
How do I stop my sprite from jumping in the air? 1 Answer
How to check if an object hits the ground hard enough then add explosive force around it (2D) 1 Answer