- Home /
How to see the two objects that collided
Ok, this is not as easy as it may seem I have this code right and it is a simple "OnCollisionStay2D(Collision2D col)" I find the collider that the object hit's tag but I was to find the object that collided.
void OnCollisionStay2D(Collision2D col)
{
if (col.transform.tag == "Platform")
{
isTouchPlatformBody = true;
Physics2D.IgnoreCollision(thisCol, col.transform.gameObject.GetComponent<Collider2D>(), true);
Physics2D.IgnoreCollision(thatCol, col.transform.gameObject.GetComponent<Collider2D>(), true);
}
}
Please help me, it may be hard to understand what I am asking for and that may be because I feel like crap (My brain has fried).
Thanks
Answer by Tomer-Barkan · Oct 01, 2015 at 01:27 PM
The OnCollisionXXXX() method belongs to a MonoBehaviour that is attached to a specific object. It will be called when this specific object collides with another object.
The tag for the other object can be accessed via the collider that is passed as a parameter to the collision event handler.
The tag for the original object can be accessed via the MonoBehaviour itself, since remember this method is within a context of a MonoBehaviour, attached to the original object.
void OnCollisionStay2D(Collision2D col) {
string otherTag = col.tag;
string myTag = this.tag;
....
}
So will this tell if it was the child collider that collided? Because if a child collider is touched it counts as the parent's collider was hit. Are far as I can see after reading that code it just gets the tag of the object that the script is attatched to not the actually child or parent that the collided object touched.
You didn't say anything about child colliders...
Anyway, in a collision, more than one of the child colliders may participate in the collision, so there is no one "this collider", ins$$anonymous$$d there is a list of contact points, and each contact point has a "this collider" and "that collider".
foreach(ContactPoint2D contact in col.contacts) {
string myTag = contact.collider.tag;
string otherTag = contact.otherCollider.tag;
// Do your magic here
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can't create an Collider2D array? 1 Answer
Best performance for mass object movement 1 Answer
How do I make an object move out of another object? 1 Answer