Search Unity

Question Capsule Collider Stuck On Edge

Discussion in 'Scripting' started by Payaso_Prince, Jan 17, 2022.

  1. Payaso_Prince

    Payaso_Prince

    Joined:
    Jul 17, 2017
    Posts:
    92
    Hello All!

    My Character Controller keeps getting stuck on edges
    due to the shape of the capsule collider.

    What I'm trying to accomplish is:

    Checking if a capsule collider with a smaller radius of 0.05 would be skinny enough to fall down.
    If so, change the radius to to 0.05 until grounded again.


    The blue DrawRay is the center of my Character Controllers end sphere.
    The red DrawRays represent the start and end sphere of the Physics.CheckCapsule that I'm checking for.

    My current problem is, my if statement is always true, even if I am standing on the ground and not at the edge of a cube. So, the radius is constantly changing from 0.09 > 0.05 continuously. :eek:

    The layer masking (3) I'm using is the ground.
    Here is the code I am using to accomplish this.


    Code (CSharp):
    1. Vector3 yOffset = new Vector3( 0, -0.1f, 0)
    2. if (!Physics.CheckCapsule(playerController.transform.TransformPoint(playerController.myController.center) , bottomPoint + yOffset, 0.05f, 3))
    3.             {
    4.                 playerController.myController.radius = 0.05f;
    5.             }
    6. if(grounded)
    7. playerController.myController.radius = 0.09f;
    Thanks so much for taking the time to read all of this!
    Any help would be immensely appreciated!
     
  2. Payaso_Prince

    Payaso_Prince

    Joined:
    Jul 17, 2017
    Posts:
    92
    Found an issue.

    I set the ground layer to 9 and replaced "3" with:

    LayerMask layermask = 1 << 9.

    Still not properly detecting collision :(
     
  3. Payaso_Prince

    Payaso_Prince

    Joined:
    Jul 17, 2017
    Posts:
    92
    I took a step back and realized the Physics.CheckCapsule isn't working for me at all.
    isGrounded is always false when im on the ground layer. I am setting it manually in the inspector too.

    Code (CSharp):
    1. float halfHeight = playerController.myController.height * 0.5f;
    2.             var bottomPoint = playerController.transform.TransformPoint(playerController.myController.center - Vector3.up * (halfHeight - playerController.myController.radius));
    3.            
    4.         bool isGrounded = Physics.CheckCapsule(playerController.transform.TransformPoint(playerController.myController.center), bottomPoint, 0.09f, layermask);
    5.             Debug.Log(isGrounded);
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,004
    Well to me none of your code currently makes much sense. First of all you grab the center of your player capsule and use that as the start pos of your testing capsule. So it starts in the center of your original capsule and not at the upper sphere center. So the capsule is shorter than the one of your player. Second you calculate the bottom sphere center which you use as bottom point. However when you use a smaller radius for the capsule, it means the capsule would be smaller than the player. By smaller I mean in height. Same bottom sphere center but smaller radius means it doesn't even reach as far down as the player does.

    I though you were planning to make the radius of the player smaller and the check capsule should check before you do that if it overlaps or not. However changing the radius of a capsule collider / CharacterController does not change the height of the capsule. That means changing the radius will change the upper and lower sphere centers as well. The sphere centers are always offset fróm the center by half the capsule's height minus the capsule's radius, but of course never smaller than 0. So when the height is smaller than the diameter (twice the radius) both sphere centers will be at the capsule center and the capsule is simply a sphere.
     
    bakinto and Payaso_Prince like this.