Search code examples
unity-game-enginegame-physics

How to have a pick able power up that has gravity and is not pushed by player?


Im having trouble with colliders and rigid bodies in my game.

I have a powerup that when a player touches gives the player a weapon upgrade, this is simply achieved with onTriggerEnter2D() and setting the collider on the powerup as a trigger. But the problem with that is the trigger does not even detect the ground collider on which the player is running and it just falls through the ground.

When I disable the trigger on the collider, the powerup, when spawned, falls to the ground and stays on it as normal, but then the player can't pick it up as it is just pushed around rather than being run through.

I have a similar problem with my enemies that should kill the player on touch. When I set the collider on them as triggers then they kill the player as expected, but fall through the ground since they have trigger colliders. But when I remove the trigger option, the enemies run on the ground as normal but never touch the player as the colliders of the enemy and that of the player push each other away and the gameObjects never actually touch.

How to solve this?

Thanks


Solution

  • You can use the method OnCollisionEnter2D(Collision) to detect collisions with non-trigger colliders, instead of OnTriggerEnter2D(), and it will still allow you to have your objects collide with the floor and not fall through.