- Home /
Detect collider when same object contains more than on collider
At present working on 2D game. I have main character in which I have attached box collider and circle collider with rigid body component.
I want to handle collision for box collider and circle collider differently. But at present I can't able to find any way in which part of main character other obstacles are collided.
How to detect collider type in which collision happen?
I'd keep one collider on the player and assign the other one to an empty game object son of the player's transform.
You will need to assign a collision detector script to each one.
Answer by HarshadK · Apr 22, 2014 at 12:33 PM
You can detect which collider of player has collided with the object from the collision script on the object with which player is collided.
The psuedo code is:
void OnCollisionEnter(Collision2D col) {
if(col.gameObject.name == "player" && col.collider == "box") {
// Do something for box collider
}
else if(col.gameObject.name == "player" && col.collider == "circle") {
// Do something for circle collider
}
}
You have to attach this script to the object with which the player is colliding
@$$anonymous$$arzoa you should not have two same type of collider on a single object. In this case you need to use a child object with second collider on it.
Who says that? In which Unity version? Actually I have objects with several colliders of the same type, and they work fine.
I am not a man of faith so, why I should not have two of the same type? It is written in any place of Unity official documentation or so?
As told, I am using them and they are working perfectly. Adding one GameObject for each collider doesn't provide any advantage and several disadvantages.
You can have two colliders of the same type on an object if you want but then it would require some calculations to find the exact collider that was collided. If you do not need this feature to find exact collider then you can have multiple colliders of same type on same object.
The method to have child objects was in relation to the problem at hand and not a general rule of thumb.
Your answer
Follow this Question
Related Questions
Rigidbody stuck in colliders when moving and ricosheting. 0 Answers
OnCollisionEnter but have colliding object not move the object it collides with 1 Answer
2D:I have a movementController script on my character but it doesn't move forward 0 Answers
handling collision with two objects of the same tag 1 Answer
Collisions between two rigidbody's and scripting collisions problem 2 Answers