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
  4. Dismiss Notice

Question Checking for Ground with Boxcast- Platforms

Discussion in '2D' started by noahl_unity, Mar 21, 2021.

  1. noahl_unity

    noahl_unity

    Joined:
    Feb 17, 2020
    Posts:
    5
    Hello, there has been an issue I have been having in my game that has been plaguing me for a while. My character uses a 2D box cast to detect the ground underneath him, however if he jumps through a one-way platform there are a few frames of time where the box cast detects that he is on (or rather in) the ground and if you time it right he will be lodged in the ground. Granted, you aren't stuck you just have to jump again for it to be right, but its still not ideal for me.

    I've been doing some research and testing but I cannot for the life of me found the correct way for my player to not get stuck in a one-way platform. I've read up on Contact Filters (which I do not understand) and I've learned that creating stopgaps like detected the players y velocity won't be enough for it to be 100% fool proof. If anyone would be kind enough to help me out I'd appreciate it.

    Code (CSharp):
    1. public bool isGrounded() //checking ground
    2.     {
    3.         RaycastHit2D raycastHit = Physics2D.BoxCast(groundCheck.bounds.center, groundCheck.bounds.size, 0f, Vector2.down, groundLayers);
    4.         Color rayColor;
    5.         if (raycastHit.collider != null)
    6.         {
    7.             rayColor = Color.green;
    8.         }
    9.         else
    10.         {
    11.             rayColor = Color.red;
    12.         }
    13.         Debug.DrawRay(groundCheck.bounds.center + new Vector3(groundCheck.bounds.extents.x, 0), Vector2.down * (groundCheck.bounds.extents.y), rayColor);
    14.         Debug.DrawRay(groundCheck.bounds.center - new Vector3(groundCheck.bounds.extents.x, 0), Vector2.down * (groundCheck.bounds.extents.y), rayColor);
    15.         Debug.DrawRay(groundCheck.bounds.center - new Vector3(groundCheck.bounds.extents.x, groundCheck.bounds.extents.y), Vector2.right * (groundCheck.bounds.extents.x), rayColor);
    16.         return raycastHit.collider != null;
    17.     }
    My ground check is a boolean that gets called througout my code, so I don't exactly know how I can alter it to have more parameters as-is.
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    I would like to know where you read that, I use the y velocity and it hasn't been a problem, I needed it to check for fall damage as well. I also use edge colliders for my one way platforms instead of a box collider, it works fine. I also don't use box cast I use overlap box because its a cheaper operation.
     
  3. noahl_unity

    noahl_unity

    Joined:
    Feb 17, 2020
    Posts:
    5
    I can't find the unity forum post that said it atm (pretty sure it was dated around 2015), but it definitely won't work for me either way. I made it so that if the player lets go of the jump button their y velocity instantly becomes 0 and they fall, which means that if I time it perfectly you will get stuck anyways.

    I'll definitely think about switching to overlap box though!
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    Chris-Trueman likes this.