- Home /
2D Collision Matrix Not Working
Hello everyone!
So I'm currently working on a 2D top down shooter project where the player is supposed to remain within the confines of the map, but the enemies can go through the boundaries. My problem is that my attempts to ignore collisions between the 2D physics layers "Enemies" and "Bounds" have proven quite unsuccessful. I have double, triple, and quadruple checked the collision matrix in the physics 2D settings to make sure I did it right, and I'm 99.99% sure that I did it correctly. I even tried to call the Physics2D.IgnoreLayerCollision( 9 , 10 , true );
function, but to no avail. Any and all help would be appreciated!
Thanks collective!
-ezshack
Answer by KittenSnipes · Sep 27, 2018 at 11:42 PM
@EZS So I would do this instead:
public GameObject enemy;
public GameObject barrier;
void Start() {
Debug.LogError("Layer Of Enemies: " + enemy.layer);
Debug.LogError("Layer Of The Barriers: " + barrier.layer);
Physics2D.IgnoreLayerCollision(enemy.layer, barrier.layer, true);
}
This method usually guarantees you have the right layer and you only need one of each of the game object's. Oh and for debugging I would also keep the Debug.Log messages so you can check and see if your layers were correct. Hope this helps you fix it.
Well then you just need to get a reference towards the enemy that is spawned.
Answer by EZS · Oct 01, 2018 at 05:10 PM
@KittenSnipes Thank you for your help, it did end up helping me get where I was intending to go.
So I ended up solving the problem myself. After receiving very limited help on Unity Answers or Reddit, I eventually fiddled around enough that I got where I needed to be. I ended up needing to set the physics2d layers manually in my code then using the values I set them to in the IgnoreLayerCollision() function. I think that there is a bug with Unity's 2D physics layers. Does anyone think I should submit a bug report?
Very interesting. I'm making something similar and have three layers, Player, Projectile and Spaceobj. All 3 are supposed to collide according to the matrix, but the player doesn't react to Projectiles. However when I change the layer of Player to Spaceobj, it works. So it feels like the matrix is buggy somehow, but I might be missing something, as I'm basing my work on the $$anonymous$$ore$$anonymous$$ountains Topdownengine.
The Physics2D.IgnoreLayerCollision command changed the $$anonymous$$atrix, but not the effect of it. So what did you actually do?
Your answer
Follow this Question
Related Questions
Using OnTriggerEnter2D/OnTriggerExit2D with Time.timeScale set to 0 1 Answer
Collision stops working at certain situations. Help? 1 Answer
How to use contact data from simultaneous collisions? 3 Answers
Different platform effector 2d rotations for different colliders 1 Answer
how to make a 2d colider script detector,how mto make a simple colision detect2d script 1 Answer