How do I get two objects (both with rigidbody and colliders) to pass through each other (and only each other)?

Hi friends!
I am making a 2d side scrolling game. It is two player locally, and I obviously need both players to collide with their environment, so that they can’t walk through walls and such. This was easy to accomplish, but now I have hit a snag. The players, both having rigidbody and colliders, run into each other. I want them to be able to pass through each other so that the players can pass each other. How can I make it so that the players still collide with the floor and walls, but can pass through each other as if they didn’t have colliders or rigidbody? Any help is greatly appreciated!

This can be achieved with physics layers. Just put your players on different layers, then use Unity - Scripting API: Physics.IgnoreLayerCollision to tell Unity to not collide them with eachother.

Where you use OnCollisionEnter or OnTriggerEnter, just specify which items it can’t pass through using tags.

i.e

Tag “Wall” with “Wall” in Inspector

Then in player code…

var velocity : float = “Something”;

function OnTriggerEnter/OnCollisionEnter ( col : Collider )

{
if (GameObject.FindWithTag(“wall”)}
{
velocity = 0;
)

}

That example will mean that if it hits something with the tag “wall” then it can’t pass through it. Anything else it will pass through.

Hope this has been of some help and help you move in the right direction.