- Home /
2D collision matrix does not work in unity 2020
I am trying to make a system where when the player pushes on button he changes layer from "Player layer" to an "invicible layer" where the player can't collide with enemy or other objects. so i created the "enemy" and "the invicible" layers and disabled the collision between the two from the 2d collision matrix but the collision still happens. i tried to set the player to the invicible layerfrom the start to make sure there wasn't a problem with changing layers with the script but the collision probblem still persisted. I tried using various functions like Physics2D.IgnoreLayerCollision(10,11, true) and Physics2D.IgnoreCollision(enemy.GetComponent(), player.GetComponent()); but nothing changed. To better understand the problem i tried disabling (i tried with all the methods above) the collision between player and an object in the scene (because i have more control on the player movement) and i saw that the collision still happens when i move horizontally but if jump on the object the player ignores the collision for a split second but then gets glitched "inside" the object. All of this problems exist only in unity 2020, not in 2019 but i can't use the 2019 version because i need to import some packages that are available only in the 2020 version.
At this point i just don't know what to do and i am quite desperate, any help would be much appriciated
Answer by sztobar · Nov 19, 2020 at 01:25 PM
How do you detect a collision? Do you use
Physics.Raycast()
,collider.Cast()
or anything like that, or do you detect it usingOnCollisionEnter2D
orOnTriggerEnter2D
method call?Do the player and button have both rigidbody2d attached?
Are any of the colliders have
isTrigger
flag enabled on it?Do you have in code any calls to
IgnoreLayerCollision(10, 11, false)
or anything that can potentially enable collision between those two?
1 when i say the collision still happens i mean that the player still behaves like it's colliding with the box collider
2 yes they have rigidbody2d attached
3 no, none of the colliders is set to trigger
4 the IgnoreLayerCollision(10, 11, false) is called in an update method in a script attached to the player, i also tried calling it from both start and awake but nothing changed.
Also, thanks for trying to help, i hope to solve this problem asap
I'm not sure if you made a typo, or mistaken an argument. Notice that: When you call IgnoreLayerCollision(10, 11, false)
you enable collision between the layers. When you call IgnoreLayerCollision(10, 11, true)
you disable collision between the layers.
You wrote that IgnoreLayerCollision(10, 11, false)
is called in an update of the player. So you enable the collision on every frame.
Ah, sorry it was a typo, i checked in the script is setting it to true
Answer by Vishal0703 · Mar 22, 2021 at 11:25 AM
Are you sure you disabled the collision in Physics2D collision matrix and not in the Physics Collision matrix??? (I am assuming you are working on a 2D game here)... Was just having this issue and seems like I was disabling the collision in Physics collision... Disabling the same collision in Physics2D worked for me...
Answer by SteveHoward · Nov 19, 2020 at 01:52 PM
@testinapuzz I don't see the same problem in 2020.1. I created an obstacle in my game and set the layer to "test". I don't get a collision. Here is my collision matrix. Turning off collisions between test and Player. I didn't do the invincible layer but you said even if you change the layer to invincible it doesn't work.
[1]: /storage/temp/171260-project-settings-and-level1.png
i did the same thing but on my project doesn't work, maybe i will try reinstalling unity
Answer by atorstling · Aug 06, 2021 at 03:07 PM
I thought I had ran into this bug, but it turned out that I had enabled the "Use Collider Mask" option in a "Platform Effector 2D", which effectively overrode which layers this platform would collide with. That meant that it could create collisions between layers which weren't colliding according to the matrix. Bit of a facepalm, but yeah...
Your answer
Follow this Question
Related Questions
How to check if an object hits the ground hard enough then add explosive force around it (2D) 1 Answer
want to knock gameobject to the same direction as the projectile 1 Answer
Problem with 2D movement. My character is moving by himself between two points... :) 1 Answer
Unity 2D Lighting Effect 1 Answer
Why is there a gap between my player and the wall during wall slide? SOLVED 2 Answers