• 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 TunerGear · Nov 16, 2022 at 11:37 AM · ainavmeshagentpathfindingbooleanschase

Method call is not working correctly in AI code

Hey, I'm brand new into the world of programming and I've been stuck on t$$anonymous$$s problem for a w$$anonymous$$le now.

I'm trying to make an AI system that would cycle through specified waypoint, but then initiate chasing down the player once the player enters the trigger and then resume cycling through the waypoints once the player leaves the trigger.

I'm essentially trying to make a chase script and patrol script work together and I have succeeded to some extent. The problem is, when the AI goes to the first waypoint, it gets stuck on it and doesn't move on to the next unless the player walks in and out of the trigger.

I am using booleans to enable and disable in each function and the debug logs also do show them working as intended.

 using System.Collections;
 using System.Collections.Generic;
 using Unity.VisualScripting;
 using UnityEditor;
 using UnityEngine;
 using UnityEngine.AI;
 using UnityEngineInternal;
 
 public class AILocomotion : MonoBehaviour
 {
     public Transform playerTransform;
     NavMeshAgent agent;
     Animator animator;
     public float maxTime = 1.0f;
     public float maxDistance = 1.0f;
     float timer = 0.0f;
     bool moveTowards = false;
     bool followWaypoint = false;
     float deaccel= 0.5f;
     float calVelocity = 0.0f;
     public Transform[] points;
     private int destPoint = 0;
     public string animState;
 
     void Start()
     {
 
         agent = GetComponent<NavMeshAgent>();
         animator = GetComponent<Animator>();
         moveTowards = false;
         followWaypoint = true;  
         
     }
 
     // Update is called once per frame
     void Update()
     {
         if (moveTowards == true)
         {
             timer -= Time.deltaTime;
             if (timer < 0.0f)
             {
                 float sqDistance = (playerTransform.position - agent.destination).sqrMagnitude;
                 if (sqDistance > maxDistance * maxDistance)
                 {
                     agent.destination = playerTransform.position;
                 }
             }
             animator.SetFloat("Speed", agent.velocity.magnitude);
         }
         else if (!moveTowards && agent.velocity.magnitude>0.0f)
         {
             calVelocity = agent.velocity.magnitude;
             calVelocity -= Time.deltaTime * deaccel;
             animator.SetFloat("Speed", calVelocity);
         }
 
         
         //CHECKS IF BOTH CONDITIONS HAVE MET AND RUNS THE WAYPOINT FOLLOWING CODE
         if (followWaypoint == true && (!agent.pathPending && agent.remainingDistance < 1.0f))
         {
             GotoNextPoint();
         }
 
         Debug.LogError("Bool" + followWaypoint);
     }
 
     private void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Player"))
         {
             moveTowards = true;
             //DISABLES THE WAYPOINT FOLLOWING CODE TO RUN THE CHASE CODE INSTEAD
             followWaypoint = false;
         }
     }
 
     private void OnTriggerExit(Collider other)
     {
         if (other.gameObject.CompareTag("Player"))
         {
             moveTowards = false; 
             //RE-ENABLES THE WAYPOINT FOLLOWING CODE ONCE THE PLAYER LEAVES THE TRIGGER AREA
             followWaypoint = true;
         }
     }
 
     //THIS IS THE WAYPOINT FOLLOWING CODE
     void GotoNextPoint()
     {
         animator.SetFloat("Speed", agent.velocity.magnitude);
         // Returns if no points have been set up
         if (points.Length == 0)
             return;
 
         // Set the agent to go to the currently selected destination.
         agent.destination = points[destPoint].position;
 
         Debug.LogError("DestPoint = " + destPoint);
         // Choose the next point in the array as the destination.
         // cycling to the start if necessary.
         destPoint = Random.Range(0, points.Length);
     }
 }

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 Captain_Pineapple · Nov 17, 2022 at 08:52 AM

Hey there,

in general i cannot spot any obvious issue in your code but i guess we can still solve t$$anonymous$$s.


First up some improvement suggestions. Since you have different behaviours that you switch between (patrol - followplayer) it is a nice t$$anonymous$$ng to use enumerations here.

For exmaple like t$$anonymous$$s:

   public enum AiState { Idle, Patrol, FollowPlayer }
   public AiState currentState = AiState.Idle;

Then you don't need the 2 flags moveTowards and followPlayer anymore as you only have one state that you switch and check:

   if(currentState == AiState.FollowPlayer)
          //do player follow stuff

T$$anonymous$$s way your code becomes more readable as you always know what is checked for and you will have less space for errors as there is only one variable to maintain.


Regarding your actual question i can only guess that the issue is somewhere in t$$anonymous$$s check:

   (!agent.pathPending && agent.remainingDistance < 1.0f)

I am sadly not that familiar with the navmesh agent stuff so i am not sure if there is a particular better way to solve t$$anonymous$$s with the agent itself but i guess that if you'd remove the check for agent.pathPending and instead added

  if(Vector3.distance(agent.distination, transform.position) > 1)
        return;

To your GoToNextPoint function t$$anonymous$$s might fix the issue.

If not i'd suggest that you output what the values for remainingDistance and PathPending are so that you understand why t$$anonymous$$s condition is never met.

I am also not sure if the fact that you randomize the next destination point after you set the destination is a good idea. I'd suggest to switch t$$anonymous$$s to t$$anonymous$$s:

          // Choose the next point in the array as the destination.
          // cycling to the start if necessary.
          //WARNING: T$$anonymous$$s does NOT choose the NEXT Point in the list but a random one. Can $$anonymous$$t the same point twice.
          destPoint = Random.Range(0, points.Length);
          Debug.LogError("DestPoint = " + destPoint);
          // Set the agent to go to the currently selected destination.
          agent.destination = points[destPoint].position;
  

In addition i'd suggest to call t$$anonymous$$s function in your Start so that the general behaviour is started with the function that you use to go to the next point.

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.

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

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

NavMeshAgent not moving at given speed 0 Answers

NavMeshAgent isn't chasing the player 0 Answers

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

Farthest Reachable Destination 0 Answers

NavMesh Agent blocked path 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