Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by SnStarr · Jan 15, 2015 at 06:37 PM · aienemyenemy aipatrol

Enemy AI Patrol Points Array Index Out of Range?

I made a script to get my enemy to patrol between three different spawn points. It works, but when he reaches the third spawn point, even though he keeps going it gives me an Array Index out of Range error on line 31, the rotation to face the next patrol point?? Where did I screw up?

 public class EnemyAttack : MonoBehaviour 
 {
     public Transform[] patrolPoints;
 
     public Transform target;
     public int movementSpeed;
     public int rotatationSpeed;
 
     public float moveSpeed;
 
     private int currentPoint;
 
     void Start () 
     {
         transform.position = patrolPoints[0].position;
         currentPoint = 0;
         target = GameObject.FindGameObjectWithTag("Player").transform;
     }
     
     void Update () 
     {
         if(currentPoint >= patrolPoints.Length)
         {
             currentPoint = 0;
         }
 
         if(transform.position == patrolPoints[currentPoint].position)
         {
             currentPoint++;
         }
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(patrolPoints[currentPoint].transform.position - transform.position), rotatationSpeed * Time.deltaTime);
         transform.position = Vector3.MoveTowards(transform.position, patrolPoints[currentPoint].transform.position, movementSpeed * Time.deltaTime );
Comment
Add comment · Show 5
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 hypnoticmeteor · Jan 15, 2015 at 06:48 PM 0
Share
 //Should be less than equal ( <= )  not ( == )
     if(transform.position <= patrolPoint[currentPoint].position)
              {
                  currentPoint++;
              }
     
avatar image SnStarr · Jan 15, 2015 at 06:53 PM 0
Share

That doesn't help, it gives me Assets/Scripts/Enemy/EnemyAttack.cs(27,30): error CS0019: Operator <=' cannot be applied to operands of type UnityEngine.Vector3' and `UnityEngine.Vector3'

avatar image hypnoticmeteor · Jan 15, 2015 at 07:25 PM 1
Share

I thought you are checking distance. Sorry. What is happening here is that even though you have specified 3 positions. When you reach the third position the condition is true and increments the currentPoint to 4. Somewhere in this small time frame you are looking for patrolPoint[4] which does not exist. When the next frame starts currentPoint is reset to 0 and continues. Put a check condition and if the currentPoint is in limits then increment it.

avatar image SnStarr · Jan 15, 2015 at 07:39 PM 0
Share

not really sure what your saying. but thanks anyway

avatar image hypnoticmeteor · Jan 15, 2015 at 07:47 PM 0
Share

An array index out of range error is when you are trying to access an array position that does not exist. You have three positions but when you reach the 3rd position you are accessing the 4th position in the array which does not exist. So put a condition before incrementing the variable.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Mmmpies · Jan 15, 2015 at 07:53 PM

You've just got the logic a bit wrong I think, try this:

          if(transform.position == patrolPoints[currentPoint].position)
          {
              currentPoint++;
              
              if(currentPoint >= patrolPoints.Length)
              {
                  currentPoint = 0;
              }
          }
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
avatar image
1

Answer by haim96 · Jan 15, 2015 at 07:32 PM

this is the problem:

 if(currentPoint >= patrolPoints.Length)

say you have 4 points, "currentpoint" can be 0 to 3. but patrolPoints.Length= 4 because you have 4 element in your array.

you need to change it to:

  if(currentPoint >= patrolPoints.Length-1)


Comment
Add comment · Show 5 · 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
avatar image SnStarr · Jan 15, 2015 at 07:46 PM 0
Share

I have three points. and if I use patrolPoints.Length-1 it removes the index array out of range error but then he just moves back and forth between the first two points and wont move on to the third.

avatar image haim96 · Jan 15, 2015 at 07:49 PM 0
Share

if(currentPoint == patrolPoints.Length-1)

you right... this should do the trick...

avatar image SnStarr · Jan 15, 2015 at 07:58 PM 0
Share

if(currentPoint == patrolPoints.Length-1) still bounces him between the first two points...

avatar image haim96 · Jan 15, 2015 at 08:07 PM 0
Share

@$$anonymous$$mmpies should work better... check his solution.

avatar image Mmmpies · Jan 15, 2015 at 08:11 PM 0
Share

Cheers @haim96 very good of you :¬)

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

28 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

Related Questions

free roaming enemy ai 1 Answer

Different enemy types script design 1 Answer

Enemy keeps moving in one direction. Help pls! 2 Answers

How to I make FindGameObjectWithTag() not just find itself? 1 Answer

Enemy AI problems 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