Hello there,
I want to reload my level when remaining distance less then 1.
But sometimes when my enemy is still far from the target, the remaining distance becomes 0.
So is there a way to solve this problem ?
Thanks
Hello there,
I want to reload my level when remaining distance less then 1.
But sometimes when my enemy is still far from the target, the remaining distance becomes 0.
So is there a way to solve this problem ?
Thanks
Yes… Show your code! ![]()
(At least how do you calculate distance)
This is my code
public Transform target;
private NavMeshAgent navMeshAgent;
public float distanse;
void Update ()
{
navMeshAgent = GetComponent ();
navMeshAgent.destination = target.position;
distanse = navMeshAgent.remainingDistance;
if (distanse<=1)
Debug.Log(“Reload Level”);
}
I never used navMeshAgent before but as fas as I can understand everytime you set destination a new path would be recalculated and that could requires more than one frame. So remainingDistance can sometime be undefined (incorrect?).
Besides doing initialisation and setting path only when needed (possibly not every frame), you can try this:
if (distanse<=1 && !navMeshAgent.pathPending)
Debug.Log("Reload Level");
If this is not enough you can try checking pathStatus too.
Thank you!
I use this method now and it works
private bool isAtTargetLocation(NavMeshAgent navMeshAgent, Transform moveTarget, float minDistance)
{
float dist;
//-- If navMeshAgent is still looking for a path then use line test
if(navMeshAgent.pathPending)
{
dist = Vector3.Distance(transform.position, moveTarget.position);
}
else
{
dist = navMeshAgent.remainingDistance;
}
distanse = dist;
return dist <= minDistance;
}
This is not a good solution. It only takes into account “how the crow flies”, so if there’s a 200 meter wall between close objects, it wont take this into account.