Using NavMesh.Raycast to detect if AI should jump over a gap
In my side scroller game I want my AI characters to freely navigate between platforms, which often leads to jumping. To tell if AI should jump or not, I'm using NavMesh.RayCast to tell if there is gap between navmeshes that is connected with offmesh links. Now, this works well in most cases, but completely fails with navmeshes taht overlap in Y axis(there is walkable platform above another walkable platform). The problem is apparent in pictures below:
and a code snippet:
private bool TestJump(Vector3 currentPosition, Vector3 targetPosition)
{
NavMeshHit hit;
bool potentialJump = NavMesh.Raycast(currentPosition, targetPosition + new Vector3(0,0.4f,0), out hit, NavMesh.AllAreas);
if(hit.hit)
{
Debug.DrawLine(currentPosition, targetPosition, Color.yellow, 0);
}
else
{
Debug.DrawLine(hit.position, currentPosition, Color.red, 0);
}
return hit.hit;
}
private void PlotPath()
{
if (_pathCache != null)
{
var a = _pathCache.ToList();
for (int i = 0; i < a.Count()- 1; i++)
{
var v1 = a[i] ;
var v2 = a[i+1] ;
float t;
if (!TestJump(v1, v2))
{
Debug.DrawLine(v1, v2, Color.green, 0);
}
}
}
}
Am I missing something here, or navmesh raycasting is completely useless when it comes to multi level navmeshes?
Thanks.
Your answer
Follow this Question
Related Questions
Objects dissappear when baked with navmesh 0 Answers
navmesh agent not working after update to 2017.1 3 Answers
How can I enforce off mesh link planning to actually use the link start and end point? 0 Answers
lookAt doesnt work with navmeshagent 0 Answers
NavMeshAgent isn't chasing the player 0 Answers