Astantos
Astantos

Reputation: 121

Unity Physics Collisions Happening Despite Being Disabled in Collision Matrix

I've got a weird issue in my game where a collision is happening DESPITE that specific interaction between the two layers being disabled in the Collision Matrix (images attached).

I have the following code used to debug as well:

    protected void OnCollisionEnter(Collision collision)
    {
        Debug.Log($"Collision between {LayerMask.LayerToName(gameObject.layer)} and {LayerMask.LayerToName(collision.gameObject.layer)}");
    }

The output is

Collision between PlayerProjectile and EntityMovement

but this should be disabled.

I have checked and double checked the layers on the objects in question, and they are correct/as intended.

My game object setup is probably a bit complex but when boiled down, this seems like it should be simple, yet for some reason it doesn't work.

What am I missing? enter image description here

Projectile with (collider-less) model as a child

My NPC/Entity

Upvotes: 0

Views: 294

Answers (3)

shingo
shingo

Reputation: 27307

What you want to check should be collision.collider.gameObject.layer. collision.collider is the collider colliding with your projectile, and collision.gameObject refers to the game object the rigidbody is attached on.

Sidenote: actually you are making compound colliders, so you'd better follow Unity's practice:

A compound collider is made of the following elements:

  • A parent GameObject that has a Rigidbody
  • Empty child GameObjects that contain colliders

Upvotes: 0

nobodyMore
nobodyMore

Reputation: 83

check the hierarchy. if the object you given the collider doesn't have a rigidbody attached, then it will go to the parents and looks for the rigidbodies. check if the layer set on the rigidbody in parent is what you expect it to be. if you want children have a different layer then you have three options

1- give all of the children rigidbodies

2- make an object in the same position of parents and make all of the colliders children of that. then write a code to fix the position of this object as the parent

3- make a parent for all the children under your rigidbody and give a rigidbody to that.

Upvotes: 0

Astantos
Astantos

Reputation: 121

I stumbled across an answer on the following site, posted by user hippocoder, which I have now confirmed through my own experimentation.

how does unity prioritize collisions on a hierarchy?

All child objects, if they have a collider but NO rigidbody of their own, will register their collision at the first rigidbody-attached collider in their (tree?) of parents. In my case, this means that the projectile wasn't colliding with the EntityMovement as such, it was colliding with what it should, but registering it on the SandCrab object (as opposed to, say, Head).

To fix this, I attached a rigidbody on each of the children, and set them to kinematic (since none of these rigidbody components will ever move themselves with physics) and now the collision reads correctly.

That being said, I have no idea if there is a better solution (seems like there should be . . .?) but this is what works

Upvotes: 0

Related Questions