Orilious
Orilious

Reputation: 329

unity - how two objects with rigidbody can pass through each other?

(I saw a question like mine, but my case is different)

I explain:

I have a 2d game(side view), where players going around and hit each other with weapons like sword or hammer. I set ground on a collision layer called "Ground" and players In "Player" layer. and the collision of the "Ground" and "Player" layer is checked. the ground has no rigid body but the players have(due to using gravity and etc). I want players don't collide with each other to have a smoother mechanic but I want to detect and receive messages when they pass through each other, for some porpuses that I need. Now, if I uncheck the "player" collision layer (In Collision2D settings) they can path through but I can't receive any messages due to Inactive collisions. and if I set the player's collider as trigger they pass through ground and fall. (please correct me if I'm wrong)

so what can I do?

Upvotes: 1

Views: 10099

Answers (3)

Orilious
Orilious

Reputation: 329

So after playing with what the Toaster said, I find the solution.

The ground collider must be in layer "Ground", the player with a collider with trigger checked in "Player" Layer, and we also need a third layer called for example "GroundCollider" and a child object for the player with a collider with trigger unchecked.

Ok. then in physics 2D settings "player" layer must check collision JUST with itself. and "Ground" layer and "GroundCollider" should check collision just with each other, not with itself or with any other layer.

with these settings, the player collides with ground and two players can check collision with each other and also can pass through each other.

Upvotes: 0

If both objects need to be solid (not trigger volumes) then you need to edit the physics layers.

  1. Change the two objects to be on separate layers (there are 32 layers and only the first five or so have names given by default) in the inspector panel dropdown.

Layers setting

This just happens to be from a project I'm working on, the layers at the top with red marks next to them are the default ones. At the bottom is "edit layers" where you can name and define new layers (up to 32 total).

  1. Go to Edit -> Project Settings -> Physics and change the physics collision mask so that the two layers you assigned your two objects are set to not interact:

Collision matrix

Uncheck any pair that should not collide (by default all layers collide with all layers).

Upvotes: 4

Basile Perrenoud
Basile Perrenoud

Reputation: 4110

Have your player game object have two children objects (one that you could call GroundCollider, on the you could call PlayerCollider)

On the first one, add a collider with onTrigger unchecked. On the second one, add a collider with onTrigger checked

Upvotes: 2

Related Questions