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;