I think this RaycastNavMesh() is a little cleaner and easier to follow. Tell me what you think…
private bool RaycastNavMesh(out Vector3 target)
{
if (Physics.Raycast(GetMouseRay(), out var rayHit))
{
if (NavMesh.SamplePosition(rayHit.point, out var navHit, maxNavMeshProjectionDistance, NavMesh.AllAreas))
{
target = navHit.position;
var navMeshPath = new NavMeshPath();
if (NavMesh.CalculatePath(transform.position, target, NavMesh.AllAreas, navMeshPath))
{
if (navMeshPath.status == NavMeshPathStatus.PathComplete)
{
if (GetPathLength(navMeshPath) <= maxPathLength)
{
return true;
}
}
}
}
}
target = new Vector3();
return false;
}