Unity 2D layer collision not working
I have two objects: "Player" is an object that has a box collider 2d and a rigidbody2d, and is set on the "player" layer. "Enemy" is an object that only has a box collider 2d, and is set on the "enemy" layer.
When the player moves, I want it to get stuck on the enemy if they touch, but the collision doesn't work, and they go through each other. I tried using this: https://docs.unity3d.com/Manual/LayerBasedCollision.html but it still isn't working. The layer collision matrix looks like this:
Is there anything I'm missing? Any help would be appreciated ^^
Note:player and enemy objects details are in comments
Answer by Lineweaver · May 02, 2019 at 05:26 PM
I figured it out! Turns out that the problem was the "kinametic" body type of the rigidbody of the player object. I changed it to "Dynamic" and set the gravity scale to zero. That way, the player collides with the enemy and still works as intended (moving around and dealing damage).
Answer by dawitabraham · Mar 11, 2020 at 09:29 PM
In my case, I mistakenly edited the Physics collision matrix instead of the Physics2D collision matrix!
Hello from the future! Thank you! saved me hours of endless searches, didn't notice separate screens lol
Answer by almoggoll · May 01, 2019 at 08:17 PM
@Lineweaver You have "Is Trigger" checked on your enemy object collider. That means the objects won't collide with the rigidbody and will only trigger the OnTriggerEnter method.
If that wasn't your intention, try unchecking that box and see what happens
Thank you for the reply, but the player object still goes through the enemy, even when both objects have "Is Trigger" unchecked..
Answer by NicBird · Mar 05, 2021 at 04:48 PM
I had the same issue and was stupid enough to mistake object tag for object layer. Once I set the layer on the object right now it works
Answer by codemaker2015 · Nov 13, 2021 at 04:53 PM
Ensure the following things are considered in your code,
All gameobject should contain collider attached and the player gameobject should contain the rigidbody component in it.
The collider size should change to the width and height of the component instead of default (1,1) values.
Uncheck isKinematic option in Rigidbody component if it's selected.
Your answer
Follow this Question
Related Questions
Problems with raycasting 2D. Probelmas com raycasting 2D. 0 Answers
Destroying a projectile on collision 1 Answer
Can't make the player attack an enemy 0 Answers
Applying a script to two objects not working 2 Answers
Placing and Breaking tiles in Unity 2D 0 Answers