- Home /
How to prevent collisions between two objects
I need to prevent the OnCollisionEnter2D
method from being called for the duration of the game. I thought this code would work void OnCollisionEnter2D (Collision2D col) { Physics2D.IgnoreCollision(GetComponent<BoxCollider2D>(), transform.parent.GetComponent<Collider2D>()); print(col.transform.name);
} and the ignore collision would work every time the method was called, however, it always prints the name of its parent, which it is always inside. I just need to prevent collisions between the 2 objects, how do I do it?
What is the parent? what is the child? why is the child inside the parent and both of them got a rigidbody? What behaviour are you trying to achieve? You need to provide a complete description of what you are trying to do so that we can think of a way to help you.
Answer by thirtiesareoldies · Jun 09, 2020 at 02:55 AM
Just looking at your code, you say transform.parent.GetComponent()); when what I think you mean is transform.parent.GetComponent()); You could also try giving the child a kinematic rigidbody or having the two objects be on different collision layers. Let me know how that goes.
I don’t think changing the layers will work in my situation but I’ll try making the child a kinematic Rigidbody. Also I don’t think there’s a difference in the code you and I wrote. Typo?
I gave the child object a kinematic rigidbody and nothing changed. To be clear, all I need is to prevent the child and parent from registering collisions, but the child object will always be inside the parent.