CharacterController2D icon indicating copy to clipboard operation
CharacterController2D copied to clipboard

Collision with RigidBody2D

Open frostblooded opened this issue 8 years ago • 2 comments

The enemies in my game are regular enemies, but don't collide with the character. As far as I can see, this is supposed to work like that because of the nature of the character controller. So how do I detect when I have collided with the enemy? I just want for rigid bodies that collide with the character (player) to get the event OnCollisionEnter2D. Is there a way to do that?

frostblooded avatar Aug 09 '17 16:08 frostblooded

same problem here. somebody can fix this?

akshinmustafayev avatar Sep 30 '17 17:09 akshinmustafayev

If you want collision like you have with the level, make sure that on your player's platform mask you have the enemy's layer selected and on the enemy's platform mask you have the player's level selected. This will trigger the onCollisionCollider event.

However, you probably want special behavior defined for colliding with an enemy, so you will want to use the OnTriggerEnter/Exit/Stay events. Do get the trigger to work properly, make sure that you have the enemy's layer selected on the player's trigger mask and the player's layer selected on the enemies trigger mask. This will trigger the "Trigger" events whenever the two players collide.

Side note, if you attach a collider to a child object of the object that contains the CharacterController2D script, it will send Trigger events but not collision events, this can be useful for various things.

The MovementController in the Demo scene of this repo shows you how to listen for the Collision and Trigger events.

Hopefully this helps.

P.S. I am a Unity N00B so take all this with a huge grain of salt.

gsgilman avatar Nov 08 '17 23:11 gsgilman