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
1
Question by SonOfTheDevil · May 25, 2021 at 06:45 PM · aipathfindingpathpathfollowingpatrol

How do I get my patrolling enemies to go back to the patrol point they had not gotten to because they chased the player?

I know, it's a very confusing question. Some of my code is from the Unity Manual, since I am not good with AI. I have been programming for about 2-3 years, it is a hobby; however, I am not very good in my own eyes. I know there will be areas that don't make sense, or will make you think "Why is he calling that multiple times when it only needs to be called once?" or "His code is not neat." Alright, sob story over.

Here's my issue:

  1. My enemy AI patrol 5 points around a wall (Just to test the AI)

  2. If the player gets close enough, the AI chases the player-and goes to its last known location if the player is out of sight or is in the Shadow Realm. (The AI ignore the player if they are in the shadow realm)

  3. After they are done chasing the player, they go to the patrolPoint that is past the original patrolPoint they were going towards before chasing.

alt text

I want the AI to:

  • Patrol a set path

  • Store the patrol point they had not gotten to because of chasing the player

  • After losing the player, the agent.destination is the Patrol point they had not gotten to because of having to chase the player.

alt text

Here is my Code:

 using System;
  using UnityEngine;
  using UnityEngine.UI;
  using UnityEngine.AI;
  using System.Collections;
  using System.Collections.Generic;
  
  public class Enemy : MonoBehaviour
  {
      // Public Variables (GameObjects, int, etc. Keep it clean...)
      public Transform[] patrolPoints;
      public ShadowRealm sr;
  
  
      // Patrol Variables
      NavMeshAgent agent;
      Transform target;
  
  
      // Private Variables (Same as Public but cannot be accessed outside of script)
      bool playerIsCloseEnough;
      int destPoint = 0;
      float distance;
  
  
      // Public Voids
      public void Start()
      {
          target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
          agent = GetComponent<NavMeshAgent>();
          agent.autoBraking = false;
          Patrol();
      }
  
      public void Update()
      {
          // Checks distance from player and this transform
          CheckDistance(); 
          
          distance = Vector3.Distance(this.transform.position, target.position);
          
          // if player is within the minimum distance
          if (distance <= 15) playerIsCloseEnough = true;
          if (distance >= 16) playerIsCloseEnough = false;
      }
      
      void CheckDistance()
      {   // If player is within minimum distance
          if (playerIsCloseEnough == true)
          {
              // if player is in Shadow Realm, player is not chased
              if (sr.isInShadowRealm == true) Patrol();
              if (sr.isInShadowRealm == false) Chase();
          }
          else Patrol();
      }
  
      void Chase()
      {
          agent.destination = target.transform.position;
          transform.LookAt(target.position);
          if (playerIsCloseEnough == false) Patrol();
      }
  
  
  
      void Patrol()
      {
          if (!agent.pathPending && playerIsCloseEnough && sr.isInShadowRealm == true && agent.remainingDistance < 0.5f) GoToNextPoint();
  
          // Choose the next destination point when the agent gets close to the current one.
          if (!agent.pathPending && playerIsCloseEnough == false && agent.remainingDistance < 0.5f) GoToNextPoint();
      }
      
      void GoToNextPoint()
      {
          // Returns if no points are set up
          if (patrolPoints.Length == 0) return;
  
          // Sets the agent to go to the current selected point
          agent.destination = patrolPoints[destPoint].position;
  
          // Choose the next point in the array as the destination, cycling to the start if necessary
          destPoint = (destPoint + 1) % patrolPoints.Length;
      }
  
      
  
  
      // Private Voids
  
  
  
  }


tempsnip.png (172.8 kB)
tempsnip2.png (175.0 kB)
Comment
Add comment · Show 4
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 CodesCove · May 25, 2021 at 07:34 PM 0
Share

Maybe you could roll back the destPoint after playerIsCloseEnough == false check. Basically just add destPoint--;

avatar image SonOfTheDevil CodesCove · May 26, 2021 at 03:22 PM 0
Share

I will definitely try that and I will update depending on how it goes! :)

avatar image SonOfTheDevil CodesCove · May 26, 2021 at 10:28 PM 0
Share

Has not worked so far. I have tried to place it in some different areas, has not worked yet

avatar image CodesCove SonOfTheDevil · May 27, 2021 at 04:50 PM 0
Share

like this?

 if (playerIsCloseEnough == false) { destPoint--; Patrol(); }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mevsvader · May 28, 2021 at 04:53 PM

didnt read all that but if u divide ur path into sections, for example, the corners like u have in the illustration, when a guard reaches that checkpoint you can save taht as the most recent checkpoint. if guard chased the player then stopped, move to most recent checkpoint then continue on the path.

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

197 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

Related Questions

How do I get my patrolling enemies to go back to the patrol point they had not gotten to because they chased the player? 0 Answers

AI teleporting 1 Answer

AI Patrol getting stuck on first waypoint 1 Answer

how do I add abilities(Scripts/GameObjects) to my path finding agent 1 Answer

What is the best AI system for pathfinding on a moving platform 2 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