I’m using CalculatePath to generate an initial path, then modifying the corners of the passed in NavmeshPath object, then I call SetPath on the NavmeshAgent with my modified path. I generated some debug sphere on the corners to make sure I’m getting my expected points, and I am, but after setting the new path, the agent isn’t using it. Is there some other way I should be modifying the path?
My code is below. The idea is to fix the cut corners of the calculated path.
NavMeshPath path = new NavMeshPath();
agent.SetDestination(target.position);
agent.CalculatePath(target.position, path);
for (int i = 0; i < path.corners.Length; i++)
{
RaycastHit hitInfo;
if(Physics.Raycast(path.corners[i] + Vector3.up * 3, Vector3.down, out hitInfo, 10, layerMask))
{
Mesh m = hitInfo.collider.gameObject.GetComponent<MeshFilter>().mesh;
Vector3 center = Vector3.zero;
for (int j = 0; j < m.vertexCount; j++)
{
center += m.vertices[j];
}
center /= ((float)m.vertexCount);
center = hitInfo.collider.gameObject.transform.TransformPoint(center);
if (Physics.Raycast(center + Vector3.up * 3, Vector3.down, out hitInfo, 10, layerMask))
{
path.corners[i] = hitInfo.point;
}
GameObject tmp = GameObject.CreatePrimitive(PrimitiveType.Sphere);
tmp.transform.position = path.corners[i];
}
}
agent.SetPath(path);