Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Catonyourhead · Oct 18, 2020 at 12:51 PM · navmeshnavmeshagentwhilefreezingwhile-loop

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;
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UNDERHILL · Oct 18, 2020 at 04:00 PM 0
Share

Why does the player end up off the navmesh? It can be updated in real time. https://www.youtube.com/watch?v=FkLJ45Pt-mY

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

146 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

How do I fix two NavMeshAgents fighting for the same position? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges