how can i pass through an enemy but not to fall through the ground in unity 2d

I am going to make the player pass through the enemy when a collision happens. The player has rigibody and collider. if i tick is trigger. the player will fall through the ground. How can i avoid the player falling through the ground but it can allow the player pass through the enemy when the player collide with the enemy?
I am using the Sample project unity 2d plaformer to test.

Remove/disable the enemy collider.

If I remove or disable enemy collider ,enemy will fall through the ground
How can they only pass through each other?

Set the enemy collider to a trigger. Tick the “is Trigger” box on the collider component.

Hello there,

I recommend you check out this page about Collision Layers.

Basically, what it allows you to do is tell which objects get to collide with which.
In your case:
• Set your ground to “ground” (or leave it to default). It should collide with everything.

• Set your enemies to a new “enemies” layer, and make them collide with the ground.

• Set your player to a new “player” layer, and make them collide with the ground and the enemies.

Then, when you want your player to start going through enemies, set its layer through code to something like “playerGhost”. Once you want it to collide with enemies again, set it back to “player”.


I hope that helps!

Cheers,

~LegendBacon