Hello, I am trying to control updates of NavmeshCut from my code. My use case is: I need to disable NavmeshCut on demand and calculate the path from that position.
I have this sample code which should disable NavmeshCut and get the nearest position on navmesh It draws two debug rays: one from source position of entity and the second one from the nearest position found on the navmesh:
using Pathfinding;
using UnityEngine;
public class NavmeshFinder : MonoBehaviour
{
private NavmeshCut cut;
// Start is called before the first frame update
void Start()
{
cut = GetComponent<NavmeshCut>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
cut.enabled = false;
// Schedule pending updates to be done as soon as the pathfinding threads
// are done with what they are currently doing.
AstarPath.active.navmeshUpdates.ForceUpdate();
// Block until the updates have finished
AstarPath.active.FlushGraphUpdates();
var constraint = new NNConstraint()
{
constrainWalkability = true,
graphMask = GraphMask.FromGraphIndex(0),
distanceMetric = DistanceMetric.ClosestAsSeenFromAbove()
};
var transformPosition = transform.position;
Debug.DrawRay(transformPosition, Vector3.up * 10, Color.green, float.PositiveInfinity);
var info = AstarPath.active.GetNearest(transformPosition + Vector3.up*0.1f, constraint);
var navmeshPosition = info.position;
Debug.DrawRay(navmeshPosition, Vector3.up * 10, Color.blue, float.PositiveInfinity);
cut.enabled = true;
}
}
}
The result looks like this:
When the NavmeshCut is not present both rays are on the same position. Am I doing something wrong? Is it possible to achive this behaviour?