Hi, I was using the navmesh for player movement and I wanted to restrict the path distance by some variable. Currently I’m getting the destination using the mouse cursor and the behavior I’m trying to figure out is that the path still updates while moving the cursor beyond the variable (max distance) but the path will only goes as far as the variable (max distance).
I got it to work some of the time but it seems like the pathing fails or the line renderer that fallows the path fails. I’m very confused by this stuff any help would be much appreciated.
void Update()
{
if (!inMotion)
{
GetPath();
}
if (Input.GetMouseButtonDown(0))
{
inMotion = true;
playerNav.isStopped = false;
if (playerNav.remainingDistance > maxMovementDistance)
{
playerNav.SetDestination(clampedDestination);
}
}
if (playerNav.remainingDistance <= playerNav.stoppingDistance)
{
inMotion = false;
}
UpdateAnimator();
}
private void GetPath()
{
movePath.SetPosition(0, gameObject.transform.position);
playerNav.SetDestination(GetDestination());
DrawPath(GetClampedPath());
playerNav.isStopped = true;
}
private NavMeshPath GetClampedPath()
{
NavMeshHit hit;
if (!playerNav.SamplePathPosition(NavMesh.AllAreas, maxMovementDistance, out hit))
{
NavMeshPath clampedPath = playerNav.path;
playerNav.CalculatePath(hit.position, clampedPath) ;
clampedDestination = hit.position;
return clampedPath;
}else{
return playerNav.path;
}
}
private void DrawPath(NavMeshPath path)
{
if (path.corners.Length < 2){return;}
movePath.positionCount = path.corners.Length;
for (int i = 1; i < path.corners.Length; i++)
{
movePath.SetPosition(i, path.corners*);*
}
}
private Vector3 GetDestination()
{
Ray cursorRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(cursorRay, out hit))
{
return hit.point;
}
return gameObject.transform.position;
}