• 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 /
  • Help Room /
avatar image
0
Question by RandomCowch · Aug 14, 2018 at 09:49 PM · scripting problemainavmeshnavmeshagentnavigation

ai patrol and chase question

I'm trying to make an enemy ai script where the enemy chooses one of random patrol points to move to and on arrival picks another one. If the raycast hits the player then it chases the player as long as it sees the player. When it stops seeing the player it goes to the players last known location and checks there, then should return to patrolling.


However, on start, the AI goes to the first point, then waits there and doesn't move, even if it sees the player. Clearly there's some sort of loop in my script but for the life of me i can't figure out where. Can someone help me out please?

My script:

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     using UnityEngine.AI;
     
     public class aiversion2 : MonoBehaviour
     {
             
         bool patrolling;
         public Transform[] moveSpots;
         private int randomSpot;
         public float startWaitTime;
         private float waitTime;
         public Transform target;
         public Transform targetShadow;
     
         RaycastHit sightRayforwards;
         RaycastHit sightRaybackwards;
         RaycastHit sightRayright;
         RaycastHit sightRayleft;
     
         void Patrol()
         {
             patrolling = false;
             startWaitTime = waitTime;
             randomSpot = Random.Range(1, moveSpots.Length);
             this.gameObject.GetComponent<NavMeshAgent>().SetDestination(moveSpots[randomSpot].position);
             new Ray(transform.position, transform.forward * 10);
             Debug.DrawRay(transform.position, transform.forward);
             Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out sightRayforwards);
             if (sightRayforwards.transform.CompareTag("Player"))
             {
                 Chase();
             }
             if (this.gameObject.GetComponent<NavMeshAgent>().remainingDistance < 0.5f)
             {
                 Update();
                 Patrol();
             }
         }
             
     
     
         void Chase()
         {
             this.gameObject.GetComponent<NavMeshAgent>().SetDestination(target.position);
             while (sightRayforwards.transform.CompareTag("Player"))
             {
                 Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out sightRayforwards);
                 return;
             }
             Check();
     
         }
     
         void Check()
         {
             this.gameObject.GetComponent<NavMeshAgent>().SetDestination(targetShadow.position);
             while (this.gameObject.GetComponent<NavMeshAgent>().remainingDistance > 0.2f)
             {
     
                 Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out sightRayforwards);
                 Physics.Raycast(transform.position, transform.TransformDirection(Vector3.back), out sightRaybackwards);
                 Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out sightRayright);
                 Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out sightRayleft);
                 if (sightRayforwards.transform.CompareTag("Player"))
                 {
                     Chase();
                 }
                 else if(sightRaybackwards.transform.CompareTag("Player"))
                 {
                     Chase();
                 }
                 else if (sightRayleft.transform.CompareTag("Player"))
                 {
                     Chase();
                 }
                 else if (sightRayright.transform.CompareTag("Player"))
                 {
                     Chase();
                 }
                 else
                 {
                     return;
                 }
             }
             if (waitTime == startWaitTime)
             {
                 Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out sightRayforwards);
                 Physics.Raycast(transform.position, transform.TransformDirection(Vector3.back), out sightRaybackwards);
                 Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out sightRayright);
                 Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out sightRayleft);
                 if (sightRayforwards.transform.CompareTag("Player"))
                 {
                     Chase();
                 }
                 else if (sightRaybackwards.transform.CompareTag("Player"))
                 {
                     Chase();
                 }
                 else if (sightRayleft.transform.CompareTag("Player"))
                 {
                     Chase();
                 }
                 else if (sightRayright.transform.CompareTag("Player"))
                 {
                     Chase();
                 }
                 else
                 {
                     waitTime -= Time.deltaTime;
                 }
                 if (waitTime <= 0)
                 {
                     waitTime = startWaitTime;
                 }
             }
             else
             {
                 waitTime = startWaitTime;
             }
             Patrol();
     
         }
     
         void Start ()
         {
             waitTime = startWaitTime;
             randomSpot = Random.Range(0, moveSpots.Length);
             patrolling = true;
             
     
     
         }
         
         void Update ()
         {
             if (patrolling == true)
             {
                 Patrol();
             }
     
         }
     }
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 NoDumbQuestion · Aug 15, 2018 at 02:21 AM 0
Share

Please use sphere cast ins$$anonymous$$d of raycast. Your code is a disaster.

The loop is in Patrol(). You call Patrol() inside Patrol().

It just loop around this.gameObject.GetComponent<Nav$$anonymous$$eshAgent>().SetDestination(moveSpots[randomSpot].position); because movespots only have 1 variable.

I'm amaze that Unity not crash

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

270 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Have players NavMeshAgent ignore Enemy NavMeshObstacle carving 0 Answers

NavMeshLink don't work in correctly in certain area 0 Answers

Need help with Nav Mesh Agent getting "stuck" at high speeds 0 Answers

NavMeshSurface or NavMeshAgent agentTypeId can't be changed on runtime 0 Answers

Problem with NavMesh 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