However, it doesn’t seem to ignore the collision. Here’s a video of what happens:
Is this as expected? Do I not understand what IgnoreCollision is supposed to do? How should I debug this? Both the ship and the missile have polygon collider 2ds. They also have RigidBody2D’s of body type Dynamic.
I can only assume that the GameObject that the above script is on has only a single collider i.e. the one returned in ‘GetComponent()’ (worth debugging this and ensuring it’s not null) and that the ‘Missile’ prefab also only has a single collider. Also, the above code assumes that the script is on the same GO as the collider and that the missle is the same.
Finally, both GO must be active/enabled. If not, collisions cannot be ignored. If you disable a GO then the setting is lost.
If you have two Collider2D on the same GameObject then it means they’re attached to the same Rigidbody2D therefore they’ll never contact each other anyway so your question doesn’t really make sense. Only colliders attached to different Rigidbody2D contact each other.
@SwixDevs I had two colliders on a single game object (the spaceship). GetComponent only returned the first one, not the second one. So it ignored the collision between the missile’s collider and the spaceship’s first collider. It didn’t ignore the collision between the missile’s collider and the spaceship’s second collider, since I never told it to ignore the collision.
use composite collider 2D on top of other colliders, make sure it si the first collider in the inspector. it will combine all other colliders into one.
i have the same problem
however the only 2 solutions is to detect the missile collision with the ship and call the function Physics2D.IgnoreCollision
where the collision-ed object collider should be col.collider in the function
this way will work like a magic but the most annoying thing is, it seemed to collide anyway in the first frame when the missile instantiated, unfortunately
another way is by using layers where you can use the function Physics2D.IgnoreLayerCollision
this will work pretty easy but you need to make Missile Layers and The ship Layer seperately in the scene
for example we have Missiles in the Layer 6 and the Player in the Layers 7 Physics.IgnoreLayerCollision(6,7) will work perfectly for you
here is my Code
public GameObject Player;
float speed;
void Awake()
{
Physics2D.IgnoreLayerCollision(8,9);
}
Because you get a collisions callback to tell you that the collisions has happened, not that it will happen. You’re asking for it to not collide after it already has.