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

Question Cylinder Rigidbody Ground Check

Discussion in 'Physics' started by LordCola, Oct 31, 2022.

  1. LordCola

    LordCola

    Joined:
    Oct 28, 2020
    Posts:
    8
    Hi everyone,

    I have a question on how to properly check if a Rigidbody is grounded. I know there are many answers to this topic out there but I feel like they don't address my specific concerns.
    I want to represent the player characters physics collision box with a cylinder. To check if the player is grounded people often recommend a raycast from the center of the player strait down. This has the problem that if the player is only just barely standing on a platform with their center already over the edge, the raycast would tell me that the player is not grounded, even though he is. Fortunately, there are other casts, like the box or capsule cast, but they all have their own problems.
    Either I use a boxcast that is (in a top-down view) fully within the bounds of the player cylinder. This way I have the same problems as with the raycast where the player can be standing on the edge of a platform but the boxcast will tell me the player is not grounded or I can make the boxcast so big that (again from a top-down view) the whole circumference of the player cylinder is within the bounds of the boxcast, but this will give me false positives when the player is standing next to a wall in an angle that the corners of the box cast are within a wall. Now if the player jumps the boxcast will constantly hit the wall telling me the player is grounded.

    Alternatively, I could use a capsulecast. Here I won't have the problems of alignment as the circumference of the casulecast and the player cylinder can be the same but now I will have inconsistencies in the time it takes to detect if the player is grounded. Since the bottom of a casulecast is a hemisphere, the capsule cast will detect that a player is grounded way sooner if the player is fully on a platform rather than just on the edge. But I am trying to make movement focused game with very tight movement. So inconsistencies in the time it takes to detect groundedness are not acceptable.

    Unfortunately, there appears to be no such thing as a cylindercast, which is exactly what I would need. So my question is how to detect that the palayer is grounded without any false positives or false negatives that a boxcast would create and without the timing inconsistencies that a capsulecast causes?
     
  2. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    You don't say what type of character controller you are using, but I would not choose any collider shape for a player that has a hard edge at ground level, it will stick very easily on any horizontal surface deviations. Slopes will either stop the player or jack them up in the air as the leading edge meets the slopes: This is why most people use a capsule shape.

    I do not understand your comments about why you do not want to use capsule cast, the cast takes place instantaneously so there is no timing issue:

    I believe that you should be able to Physics.SphereCast down a distance so that half the sphere would penetrate a horizontal plane at the base of the character. You can then get the hitInfo to see which point hit the ground first and how offset it is from the centre of the character. By evaluating the vertical component of hitInfo you know if the character is truly grounded (the distance will be <= ground plane + tolerance). The nature of the shape of the sphere will also mean this gives you the nearest point to the centre of your character - the only place this should fail will be if you have a small step down next to an edge such that the base of the sphere contacts the base of the step before the side of the sphere contact the upper edge, giving a false ungrounded result. If you have to cover this edge case then you can add multiple raycasts on the occasion that you get a reported contact beyond your ground plane distance, but near the centre of the character. Surely easier to select your terrain and sphere size to avoid this situation.
     
  3. LordCola

    LordCola

    Joined:
    Oct 28, 2020
    Posts:
    8
    I am writing a custom character controller on a dynamic rigidbody.

    So my thinking was that if the bottom surface of my player collider was flat then if I just used the spherecasts boolean return value to test for being grounded, then detecting ground would possibly happen some frames earlier than when standing fully on a surface compared to standing on a surface only barely with just the edge of the player collider. (when falling)
    I didn't even consider that I could look at the cast hit to check the distance to my player. But you raise some valid points for using a capsule collider so I wouldn't even have the problems with a flat bottom surface.