I could use some help rethinking my wall-jump implementation to work with non-convex colliders

So I’m working on a project with a wall-jump. The relevant part here is that when a player is close to a wall (or any piece of environmental geometry) and hit jump they should be pushed away from the wall. Right now I have a collider (trigger) on the player that maintains a list of whichever colliders it is currently intersecting. If there’s at least one collider in this list then I check the closest point on each collider and whichever is the closest I apply force on the player away from that point. This works well except I just started using ProBuilder to build out a level and realized Collider.ClosestPoint() doesn’t work on none-convex meshes. Scratching my head a bit on what to do if I can’t get the closest point.

Here’s the relevant part of my existing code:

// Init variables
float closestDistance = float.MaxValue;
Vector3 closestJumpPoint = Vector3.zero;
Collider closestCollider = null;

// Find the closest collision
List<Collider> nearbySurfaces = wallJumpCollisions.IntersectingTriggers();
foreach(Collider collider in nearbySurfaces)
{
        Vector3 jumpPoint = collider.ClosestPoint(transform.position); // Doesn't work with non-convex colliders
        float distance = Vector3.Distance(transform.position, jumpPoint);
        if (distance < closestDistance)
        {
                closestDistance = distance;
                closestJumpPoint = jumpPoint;
                closestCollider = collider;
        }
}

// Apply the walljump 'push'
Vector3 jumpDirection = transform.position - closestJumpPoint;
jumpDirection.y = 0; // Only use the horizontal direction
wallJumpMomentum.direction = jumpDirection.normalized;

You can do a short ray cast in eight key directions (think compass) and add together all the directions that hit something and then normalize and flip the summed direction to get the final jump direction.

I had worried that don’t a handful of raycasts would be imprecise because an object could get missed (for example if there’s a sharp corner in between the ‘spokes.’ I’ll play around with this method though and see if it works for me. Cheers!

Yes, you may have to increase the number of rays depending on your game and how its geometry is orientated. And you can improve your chances of hitting something by using a sphere cast instead of a ray cast. This is after all what we often use when checking for the presence of a ground below. You could also use the hit normal to influence the jump direction so that if there’s an angled wall to the immediate right of your character then your character will jump in the direction of the wall’s normal.