• 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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Second NavMesh Surface refuses to cooperate: Failed to create agent because it is not close enough to the NavMesh 0 Answers

Problem with NavMesh 0 Answers

Navigatin Mesh agent works for ~10 seconds, then starts moving sporadically 0 Answers

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges