Capsule cast test if enough room to climb up?

I’m casting a ray down from forward and above the players head to detect a ledge to hang on, but I need to perform a capsule cast to detect if there’s enough room for clearance in the gap above the ledge. I think I need to do a CapsuleCast (or is it OverlapCapsule? OverlapAll? OverlapSphere?) in the position where the raycast hits the ledge but the height of the player capsule. How would I do this? I’ve been playing around with:

``````if (Physics.CapsuleCast(hit.transform.position, capsule.height, capsule.radius, Vector3.down, out capsulehit, capsule.height))
``````

…but this isn’t quite right, what would the best way to do this be? Here’s an example of the test I’m doing for the ledge that works:

I’m going to assume your image is rotated 90 degrees and your player isn’t clinging to the ceiling, but that you are trying to make sure the ledge they are climbing onto is big enough for them to stand?

Usually this is done either by hand-annotating the parts of the level upon which you can stand (sometimes with extra geometry), or putting in extra invisible geometry to fill the gap where you cannot stand.

It’s more like this:

The capsule on the right is clinging, and the outline is the gap I need to test. Maybe something like this?

I would have thought this might have worked, but it did not:

``````            float distanceToPoints = capsule.height / 2 - capsule.radius;
Vector3 point1 = hit.point + capsule.center + Vector3.up * distanceToPoints;
Vector3 point2 = hit.point + capsule.center - Vector3.up * distanceToPoints;

if (Physics.CapsuleCast(point1, point2, radius, Vector3.up, capsule.height))
{
``````

Between the capsulecast start points being the centre of the capsule ends and trying to match the size to my player, I’m completely confused. Is there an easier way to do a capsule cast the size of my player to determine if there’s room to climb up?

I’m sorry I can’t really offer more help… I haven’t done a lot of “feeling around my scene” with raycasts and capsule casts. What little I have done seems fraught with peril, because there’s so many ways for geometry to go wrong.

That’s why I always favor the direct way of authoring the level with a separate collision / climbing mesh (invisible) so that you can control exactly which parts are traverseable. That’s actually how most commercial games do it in fact. Otherwise a last minute tweak to art could conceivably break the entire game and make it unshippable.

@Invertex helped me a great deal with this:

``````            var start = hit.point + (hit.normal * capsule.radius);
var end = hit.point + (hit.normal * (mover.colliderHeight - capsule.radius));
Just find a hitpoint with a raycast for the ledge and use it for `hit`