Search Unity

Question Why is my 2D character getting pushed down 0.544207 in Y when uncrouching below a passable platform?

Discussion in 'Physics' started by Reverend-Speed, May 23, 2023.

  1. Reverend-Speed

    Reverend-Speed

    Joined:
    Mar 28, 2011
    Posts:
    284
    I have a 2D platformer (using OpenGameArt sprites), where the character needs to be able to crouch AND be able to press crouch and jump in order to drop down 'thin' platforms.

    I'm using two Tilemaps (Midground and OneWayPlatforms) on two respective different layers (Ground and OneWayPlatforms).

    When dropping down, I use Physics2D.IgnoreCollision to temporarily ignore the OneWayPlatforms TilemapCollider2D.

    When standing up from a crouch, I BoxCollider2D cast to make sure nothing on layer 'Ground' is above the character's head before the character stands up.

    However, when standing back up from a crouch while standing on a OneWayPlatform with a OneWayPlatform above my character's head, instead of standing up on the lower platform while the upper platform is now at the character's waist (expected behaviour), my character is pushed 0.5442 units down until their vertical halfway point (waist) is now level with the lower platform - effectively, they're pushed halfway down through the lower OneWayPlatform.



    I've tried adding some Y velocity and/or teleporting up a few centimetres (transform.position += (Vector3)Vector2.up * 0.5) coming out of the crouch but either it causes the character to fall down through the platform or the character just ends up getting stuck in the platform as before.

    Can anybody suggest to me why this is happening? This is really baking my noodle.

    https://hatebin.com/dqthvrylfo - Basic V movement (includes 'crouch' code)
    https://hatebin.com/gbkfqinmqz - Specific drop-down code