I'm developing a simple 2d platform game with Unity 2d. I have a problem managining my "Jump" action for the player.
I would like the player to Jump only when he has his feet on a "surface": terrain, platform, crate etc.
I'm considering using colliders: when my player collides with a surface, set my bool canJump = true
.
When he jumps, I set canJump = false
(he is midair).
My terrain has a Rigidbody2d
is Kinematic
and a Box Collider 2d
.
My Player has a Rigidbody2d
is Dynamic
and a Capsule Collider 2d
(or a Polygon Collider.. )
The events OnCollisionEnter2d()
and OnTriggerEnter2d()
are never raised when Player collides with terrain.
Does anyone know what's wrong?
Thanks
EDIT: Now i'm using this to check if "something" is under my feet:
RaycastHit2D hit = Physics2D.Raycast(rb.transform.position, -Vector2.up, 0.1f);
if (hit)
canJump = true;
It doesn't works! hit is always true! I'm getting mad!
Physics2D.CircleCast
usually works better (often) when checking if a player is grounded. \$\endgroup\$transform.position =
ortransform.Translate()
this can effectively "teleport" the object from one side of the trigger boundary to the other, so the physics engine never sees the contact happen and never fires the OnCollisionEnter2D/OnTriggerEnter2D events. Moving the object via the Rigidbody2D usually solves this. There could also be issues with the object hierarchy, where the collider that receives the collision event doesn't pass it on to your script, so showing your player character's setup will help. \$\endgroup\$