Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Player can jump on walls with tilemap colliders.

Discussion in '2D' started by Harvii, Sep 5, 2022.

  1. Harvii

    Harvii

    Joined:
    Apr 20, 2022
    Posts:
    6
    When I go up against a wall in my level which is a tilemap, I can jump on the wall. The tilemap has a composite collider and the player has a box collider. Jumping works fine on walls with box colliders.

    Code to check if grounded:
    Code (CSharp):
    1.     public bool IsGrounded() {
    2.         float extraHeightTest = 0.01f;
    3.         RaycastHit2D raycastHit = Physics2D.BoxCast(bc2D.bounds.center, bc2D.bounds.size, 0f, Vector2.down,extraHeightTest + extraHeightTest, collisionLayerMask);
    4.         Color rayColor;
    5.         if (raycastHit.collider != null) {
    6.             rayColor = Color.green;
    7.         } else {
    8.             rayColor = Color.red;
    9.         }
    10.         Debug.DrawRay(bc2D.bounds.center + new Vector3(bc2D.bounds.extents.x, 0), Vector2.down * (bc2D.bounds.extents.y + extraHeightTest), rayColor);
    11.         Debug.DrawRay(bc2D.bounds.center - new Vector3(bc2D.bounds.extents.x, 0), Vector2.down * (bc2D.bounds.extents.y + extraHeightTest), rayColor);
    12.         return raycastHit.collider != null;
    13.     }
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,353
    You dont really explain the issue, but if i had to guess, you dont want the player to be able to jump on walls?

    There are many ways to do this, but id say the easiest is to change the layer of the walls.

    Change the layer/tag (it looks like you are comparing layer in your raycast) on the walls to something that is not the same as the floor and will not be checked by the raycast for IsGrounded.

    You can also add a physics material 2D and change the friction of it to 0. Then apply that physics material to the wall objects (their collider or rigidbody component, i believe). This will prevent the player from getting "stuck" on the wall due to friction.
     
  3. Harvii

    Harvii

    Joined:
    Apr 20, 2022
    Posts:
    6
    Correct, I don't want the player to jump on the walls. The problem is that every ground tile and wall tile are in the same tilemap, which has a composite collider. Meaning I can't change the layer of individual "walls". It looks like the issue is with the box cast, because when I use the gizmo button, the draw rays are green when against a wall while mid air.