Agents are getting stuck running against and not pathing around objects. My navmesh is baked and agents dont have any issue moving around randomly, but once they encounter an object thats in the way of the destination they get stuck. I’m trying to use just CalculatePath without using SetDestination, since its a huge performance issue.
Also looks like there is another issue where ChooseWaypoint() only works the first time and won’t be able to find a new path after that.
public Vector3 destination;
public void WanderUpdated()
{
float distanceToWanderPosition = Vector3.Distance(transform.position, destination);
if (distanceToWanderPosition <= 15)
{
ChooseWaypoint();
}
else
{
var step = 50.0f * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, destination, step);
Vector3 direction = (destination - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRotation, Time.deltaTime * 300.0f);
}
}
void ChooseWaypoint()
{
Vector3 randomDirection = Random.insideUnitSphere * 800;
randomDirection += transform.position;
NavMeshHit hit;
NavMesh.SamplePosition(randomDirection, out hit, 800, 1);
Vector3 finalPosition = hit.position;
NavMeshPath path = new NavMeshPath();
if (NavMesh.CalculatePath(transform.position, finalPosition, NavMesh.AllAreas, path))
{
destination = finalPosition;
}
}