Hi everybody! Thanks for taking a look at this. I have my character walking across a flat surface. It seems like while walking across this surface, she losing contact with the floor (you can see this when her wings outstretch). Can anybody give me some clarification on why she would be losing contact with the surface? This surface is a TileMapBoxCollider. Collision handling coming.
#Keeping my platformer character grounded
1 messages · Page 1 of 1 (latest)
Succubus.cs
...
void OnCollisionEnter2D(Collision2D collision) => HandleCollisions(collision, CollisionType.Enter);
void OnCollisionStay2D(Collision2D collision) => HandleCollisions(collision, CollisionType.Stay);
void OnCollisionExit2D(Collision2D collision) => HandleCollisions(collision, CollisionType.Exit);
void HandleCollisions(Collision2D collision, CollisionType collisionType)
{
bool grounded = false;
bool making_contact_with_surface = collision.gameObject.CompareTag("Surface");
bool is_falling;
bool is_landing;
if (making_contact_with_surface)
{
foreach (ContactPoint2D contact in collision.contacts)
// Check for collision on surface
{
if (Vector2.Dot(collision.GetContact(0).normal, Vector2.up) > 0.8f)
{
grounded = true;
break;
}
}
}
is_falling = collisionType == CollisionType.Exit && !grounded;
is_landing = collisionType == CollisionType.Enter && !grounded;
if (is_falling)
{
Airbourne(true);
}
else if (is_landing)
{
Airbourne(false);
LinearVelocityX(0);
}
}
oh. I just realized why defining my bools like this is a dumb idea
Why is it when I share code I suddenly see what dumb leaps in logic I have made? Not the time, brain
since you are relying on collision messages for ground checks instead of physics queries, what is likely happening is that your tilemap collider is not using a composite collider so when it inevitably hitches on the edge of a tile (which is a known issue with box2d, the 2d physics engine unity uses) it exits the collision for a frame. use a physics query like an OverlapCircle or something to check for ground rather than collision messages