- Home /
Navmesh Pathfinding Freeze (may be a while loop thing)
I have a scary creature following my player around. He likes to teleport somewhere behind you when he can no longer pathfind to you. That's what the script below is designed to do. My intention is to have to have this script perform the following behavior: if he can't pathfind to you he teleports to an area behind you, waits a second and then chases you. I had to check his pathfinding using a simple "see if he's standing still for too long" check. He actually performs perfectly! but... If the player character spends too long off of mesh, the whole game freezes!!! I thought it had to do with the navmesh components, but I feel as though I could have just messed up this while loop somehow...
I have messed with these 100 lines for so many hours I can;'t even see them any more. And yes, I have isolated the cause of the freeze to this specific script. If you have any other advice, I do appreciate any. I'm new so thanks! Any help is appreciated!! Thank you in advance!
private Transform Player;
private NavMeshAgent Agent;
public float range = 10.0f;
private Vector3 WarpPoint;
private Vector3 Tracker;
private float Counter = 0;
bool waiting = false;
private void Awake()
{
Agent = GetComponent<NavMeshAgent>();
Agent.updateRotation = false;
print("starting");
Player = GameObject.Find("Player").transform;
print("Started");
}
private void Update()
{
//face direction mmoving
transform.rotation = Quaternion.LookRotation(Agent.velocity.normalized);
if (!waiting)
{
if (Tracker == transform.position)
{
Counter += 1*Time.deltaTime;
}
else { Counter = 0; }
Tracker = transform.position;
if (!Agent.pathPending)
{
if (Counter >= 3)
{
NavMeshPath path = new NavMeshPath();
if (RandomPoint(Player.transform.position, 24, out WarpPoint))
{
Agent.ResetPath();
waiting = true;
StartCoroutine(WaitChase());
}
if(NavMesh.CalculatePath(transform.position, Player.transform.position, NavMesh.AllAreas, path))
{
print("setting Destination");
Agent.SetDestination(Player.transform.position);
}
Counter = 0;
}
}
}
}
bool RandomPoint(Vector3 center, float range, out Vector3 result)
{
bool searching = true;
while(searching)
{
Vector3 randomPoint = center + Random.insideUnitSphere * range;
randomPoint.y = Player.transform.position.y;
NavMeshHit hit;
if (NavMesh.SamplePosition(randomPoint, out hit, 1.0f, NavMesh.AllAreas))
{
Vector3 toTarget = (hit.position - Player.position).normalized;
if (Vector3.Dot(toTarget, Player.transform.forward) < -.5)
{
result = hit.position;
searching = false;
return true;
}
else
{
Counter = 0;
searching = false;
}
}
}
result = transform.position;
return true;
}
IEnumerator WaitChase()
{
Agent.Warp(WarpPoint);
yield return new WaitForSecondsRealtime(1.7f);
waiting = false;
}
}
Why does the player end up off the navmesh? It can be updated in real time. https://www.youtube.com/watch?v=FkLJ45Pt-mY
Your answer

Follow this Question
Related Questions
Are CharacterControllers and NavMeshAgents meant to work together? If so, how? 2 Answers
How to control a NavMeshAgent's rotation ? 1 Answer
How to make whole navmesh agent avoid carves rather then center of it? 0 Answers
Precise distance calculation with high timeScale for NavMeshAgents? 1 Answer
Navmesh agent clearly not finding the most efficient route... 1 Answer