- Home /
Character is hitting the ground but not collisions are being detected.
I am trying to make a simple test 2D platformer game. My character has gravity and falls to the ground, and when he hits the ground he stops (which I would expect to be a collision).
Here's what I have to assure this collision happens:
I have this method in my player controller script:
private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Ground")) { isGrounded = true; Debug.Log("In OnCollisionEnter2D"); Debug.Log("isGrounded is now " + isGrounded.ToString()); } }
The character has a 2D box collider and a 2D rigidbody. The rigidbody is Dynamic, not Kinematic.
The "ground" is a tilemap with a Tile Map 2D collider (it does not have a rigidbody).
Both colliders are NOT triggers.
The character can jump, so the collisions should be happening everytime he falls back down.
Just in case I'm using the wrong method, I also have the two methods:
OnTriggerEnter2D(Collider2D collider)
OnControllerColliderHit(ControllerColliderHit hit)
I gave them the same debug statements. Neither of them detect a collision.
The OnCollisionEnter2D (and the other likewise methods) have 0 references and are "unused" according to Visual Studio. I don't know if that's a problem.
With all this, I expect the Debug.Log() statements to show in the console, but they never do. I would appreciate some expert help as to why the collisions aren't happening. If you need more insight into my project, I will gladly give it. Thanks for the help!
Answer by pmiglio · Mar 24 at 07:06 PM
I figured it out. I had the character's class inheriting from KinematicObject. As soon as I changed it to MonoBehaviour, it started detecting collisions (only OnCollisionEnter2D detected).
Sorry I didn't mention the part about KinematicObject. I figured it wouldn't be part of the issue.
Answer by ArmanDoesStuff · Mar 24 at 06:17 PM
I think if the floor collider isn't a trigger then it also needs to have a rigidbody2D. Just set it to kinematic.
I tried adding a rigidbody to the floor and making it kinematic. I still don't see any collisions.
Darn, then I'm not too sure. I can only suggest reducing it to its most base form (seeing if it collides at all with anything at all, without the CompareTag) and work from there.