- Home /
Make 2D projectile collide only from the outside
Hello everyone, I have a simple yet mind-scratching issue. In my game, I have a 2D object that collides only with projectiles (as intended), but the issue is that when the player is "inside" the collider, the projectile collides as soon as it spawns, not allowing the player to shoot enemies that are in the distance.
I tried using platform effectors with no luck. To make sure I was configuring the effector correctly (to interact only with the projectile layer), I ended up watching a beginner video on how effectors work and it appears that I have been configuring them correctly.
Thanks in advance to whoever might have an idea for a simple but effective solution to this issue!
You could always just do Physics.IgnoreCollision(myPlayer.GetComponent<Collider>(), myProjectile.GetComponent<Collider>());
when you instantiate the projectile.
never$$anonymous$$d, I found a really easy solution. The collision issue is not between the projectile and the player, but the projectile and another object that acts as a active background component that the player can shoot. I'll post the solution as an answer, so I can hopefully help anyone that googles a similar issue in the future. Thanks for replying!
Don't forget in Edit > Project Settings > Physics you can deter$$anonymous$$e what objects layers can interact with what layers.
Answer by Andre_51X · Feb 09, 2019 at 05:18 PM
Ok, apparently I found a solution by myself. The solution is to switch the object's collider to a trigger when the player is inside the collision, like this the projectile won't collide, to then set the trigger to false when the player is outside the area. 2 colliders are involved: a child trigger collider that is set to a layer that collides with the player (so the parent knows when the player is over the interested object) and the parent's collider that is set to the layer that only projectiles can collide with, which will be the collider that alternates its trigger status.
This seems like a janky solution to a simple problem....
The issue is not that I don't want the projectiles to interact with the background object. I do want them to collide with the background object, but I don't want the projectiles to interact with this object when the player is standing in front of it. By default in Unity, when a collider instantiates inside another collider, a collision event happens. This is what I don't want to happen.
That makes sense, there would be a few ways to do it, but knowing what your intention is, I can't see anything wrong with your method, unless other objects still need to collide with the now-trigger object. In which case there are some alternatives that don't involve setting the object as a trigger.
Your answer
Follow this Question
Related Questions
Bullet Trail not destroying on collision 0 Answers
Problem with method Collider2D.isTouchingLayers() 4 Answers
How to select the trigger layer on a Box Collider 2D 2 Answers
How to make a little wiggle room for the collision? 1 Answer
Can I have a single rigidbody2d that acts as both collider and trigger? 1 Answer