Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by RealFleebs · Jul 20, 2018 at 10:21 PM · scripting problemscript.waypointartificial intelligencewaypoint system

AI Waypoint does not loop

Hi, so I have this script that I got from a tutorial that I got working just fine, I have it set to patrol around a certain place stopping at 6 different waypoints, basically I'm telling my NavMesh AI to walk to the first waypoint, then once it gets there continue to the next, and so on. Now this works perfectly, except it doesn't loop. I have the waypoints set up like a circle and I want the AI to go back to the first waypoint after reaching the last and loop over and over, and it doesn't. It gets to the last waypoint and then doesn't change direction and runs right into the wall and stays there until I restart the game. How do I get my code to go back to the first waypoint after getting to the last of an array of checkpoints??

My code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 using System;
 
 namespace UnityStandardAssets.Characters.ThirdPerson
 {
     public class basicAI : MonoBehaviour
     {
 
         public NavMeshAgent agent;
         public ThirdPersonCharacter character;
 
         public enum State
         {
             PATROL,
             CHASE
         }
 
         public State state;
         private bool alive;
 
         // Variables For Patrolling
         public GameObject[] waypoints;
         private int waypointInd = 0;
         public float patrolSpeed = 0.5f;
 
 
         // Variables for Chasing
         public float chaseSpeed = 1f;
         public GameObject target;
 
         // Use this for initialization
         void Start()
         {
             agent = GetComponent<NavMeshAgent>();
             character = GetComponent<ThirdPersonCharacter>();
 
             agent.updatePosition = true;
             agent.updateRotation = false;
 
             state = basicAI.State.PATROL;
 
             alive = true;
 
             StartCoroutine("FSM");
         }
 
         IEnumerator FSM()
         {
             while (alive)
             {
                 switch (state)
                 {
                     case State.PATROL:
                         Patrol();
                         break;
                     case State.CHASE:
                         Chase();
                         break;
                 }
                 yield return null;
             }
         }
 
         void Patrol()
         {
             agent.speed = patrolSpeed;
             if (Vector3.Distance (this.transform.position, waypoints[waypointInd].transform.position) >= 2)
             {
                 agent.SetDestination(waypoints[waypointInd].transform.position);
                 character.Move(agent.desiredVelocity, false, false);
             }
             else if (Vector3.Distance (this.transform.position, waypoints[waypointInd].transform.position) <= 2)
             {
                 waypointInd += 1;
                 if (waypointInd > waypoints.Length)
                 {
                     waypointInd = 0;
                 }
             }
             else
             {
                 character.Move(Vector3.zero, false, false);
             }
         }
 
         void Chase()
         {
             agent.speed = chaseSpeed;
             agent.SetDestination(target.transform.position);
             character.Move(agent.desiredVelocity, false, false);
         }
 
         private void OnTriggerEnter(Collider coll)
         {
             if (coll.tag == "Player")
             {
                 state = basicAI.State.CHASE;
                 target = coll.gameObject;
             }
         }
     }
 }
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 kalen_08 · Jul 20, 2018 at 10:35 PM

use:

 waypointInd = (waypointInd + 1) % waypoints.Length;

instead.

Overall I would make these changes.

     // your IEnumerator was getting called literaly every frame..
     // Coroutines run on there own time and therefore are a little
     // more expensive so it would be better to just call this in Update ()
     void Update ()
     {
         if (alive)
         {
             switch (state)
             {
                 case State.PATROL:
                     Patrol();
                     break;
                 case State.CHASE:
                     Chase();
                     break;
             }
         }
     }
 
     void Patrol()
     {
         agent.speed = patrolSpeed;
         var distanceToWaypoint = Vector3.Distance (transform.position, waypoints[waypointInd].transform.position);
         if (distanceToWaypoint < 2)
         {
             waypointInd = (waypointInd + 1) % waypoints.Length;    
             agent.SetDestination(waypoints[waypointInd].transform.position);
         }
 
         // your first block was mostly un necesary..
 
 
         // this third block is was never getting called
         // the 2 if statements you had covered all possibilities
         // therefore this was never getting called and is useless.
 //        else
 //        {
 //            character.Move(Vector3.zero, false, false);
 //        }
     }

let me know if this fixes it.

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

218 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

Related Questions

How can I add/remove squad members from the FormationSquare when changing the number of members value ? 1 Answer

script help 1 Answer

pick up object problem 0 Answers

Move to next animation with clicking the game object 0 Answers

How to stop movement script on void start and resume after. 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