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 Coyroski · Nov 20, 2021 at 01:05 AM · navmeshagentpathfindingnpc

My NPC is not switching from running away state to random movement state.

Here's the code I wrote. The NPC does random movement with the RandomMovement() method. When the player gets behind the NPC and is detected by the Physics.SphereCast, the seenplayer bool gets set to true and the Runaway() method should run. The issue I'm having is that when the player is chasing the NPC and the player stops, the NPC should continue running away up to the runawayRadius distance which is further away that the spherecast rearViewDectionRange.

But the NPC just runs away to a up to a random distance and just stays there, it doesn't switch back to the RandomMovement() method . Unless I walk backwards away from the NPC it somehow starts the RandomMovement() method again.

 void FixedUpdate ()
 {
     Debug.Log(distance);
     NavMeshAgent agent = GetComponent<NavMeshAgent>();
     
     RaycastHit rHit;

     if (!seenPlayer)
     {
         RandomMovement();

         if (Physics.SphereCast(eyes.transform.position, sphereCastThickness, -transform.forward, out rHit, rearViewDetectionRange))
         {
             if (rHit.collider.tag == "Player")
             {
                 seenPlayer = true;
             }
         }
         else
             seenPlayer = false;
     }

     if (seenPlayer)
     {
         Runaway();
     }
 }

 void RandomMovement ()
 {
     seenPlayer = false;
     runningAway = false;
     
     distance = Vector3.Distance(player.position, transform.position);
     Vector3 randomDirection = Random.insideUnitSphere * walkRadius;
     randomDirection += transform.position;
     NavMeshHit hit;
     NavMesh.SamplePosition(randomDirection, out hit, walkRadius, 1);
     Vector3 finalPosition = hit.position;
     NavMeshAgent agent = GetComponent<NavMeshAgent>();
     if (!agent.pathPending)
     {
         if (agent.remainingDistance <= agent.stoppingDistance)
         {
             if (agent.hasPath || agent.velocity.sqrMagnitude == 0f)
             {
                 if (timerStart == false)
                 {
                     agent.speed = walkSpeed;
                     Debug.Log("Destination arrived.");
                     idleTime = Random.Range(0, 11);
                     timerStart = true;
                     agent.autoBraking = true;
                 }

                 if (timerStart == true)
                 {
                     idleTime -= Time.deltaTime;
                 }
                 if (idleTime <= 0f)
                 {
                     timerStart = false;
                     Debug.Log("Agent Idle complete.");
                     agent.CalculatePath(finalPosition, path);
                     agent.SetPath(path);
                 }
             }
         }
     }
 }
         

 void Runaway ()
 {
     if (seenPlayer)
     {
         runningAway = true;
         if (runningAway)
         {
             NavMeshAgent agent = GetComponent<NavMeshAgent>();
             distance = Vector3.Distance(player.position, transform.position);
             if (runawayRadius > distance)
             {
                 timerStart = false;
                 //agent.SetDestination(newPos);
                 //agent.SetDestination(newPos);
                 StartCoroutine(PathCalls());
             }
             if (runawayRadius <= distance)
             {
                 runningAway = false;
                 agent.speed = walkSpeed;
                 RandomMovement();
             }
         }
     }
 }

 Enumerator PathCalls ()
 {
     NavMeshAgent agent = GetComponent<NavMeshAgent>();
     agent.speed = runAwaySpeed;
     dirToPlayer = transform.position - player.transform.position;
     newPos = transform.position + dirToPlayer;
     agent.CalculatePath(newPos, path);
     agent.SetPath(path);
     agent.autoBraking = false;
     yield return new WaitForSeconds(timeBetweenPathCalls);
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Coyroski · Nov 20, 2021 at 02:18 AM

Okay it seems to be a problem with IEnumerator PathCalls(). I have a debug log there that shows that its being called but the code there isn't executing. I think theres a problem with my path finding code.

Comment
Add comment · Share
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

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

137 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

Related Questions

How do you set up navmesh agents to go to multiple destinations? 3 Answers

How to change a NavMeshAgent angular rotation speed, 0 Answers

Way to ignore specific navmeshobstacles for specific navmeshagent when creating path? 0 Answers

How to sample a point on a navMesh Agent path 0 Answers

Is it possible to assign a custom path to a NavMeshAgent? 0 Answers


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