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 /
avatar image
1
Question by 3kWikiGames · Feb 08, 2019 at 11:58 PM · 2dmovementenemy aiwaypointarray-out-of-range-except

2D Enemy AI issues...

So I have a script set up for my enemy to walk to a set of way points on the map I have set up through an array. I keep getting the error that the array index is out of bounds which I don't quite understand why. I would like for him to cycle through the way points in order back and forth but it only goes to the third way point then the error screen appears. Any help is greatly appreciated thank you!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Enemy : MonoBehaviour
 {
     public float speed1 = 3.0f;
     public float speed2 = 3.0f;
     public GameObject[] waypoints;
     private int waypointnum = 0;
     public float near = 5f;
     public GameObject player;
     private Rigidbody2D rb;
 
     private bool chase = false;
     private bool chaseend = false;
 
     private Vector2 Target { get { return player.transform.position; } }
     private Vector2 Position
     {
         get
         {
             return transform.position;
         }
         set
         {
             transform.position = value;
         }
     }
     private void Start()
     {
         rb = GetComponent<Rigidbody2D>();
         Position = waypoints[0].transform.position;
     }
     //SHOULD RESET POSITION AFTER CERTAIN AMOUNT OF TIME.
     //IF spotted speed should increase and decrease when reset.
     private void ChaseChar()
     {
         float dist = Vector2.Distance(Position, Target);
         if (dist < near)
         {
             float step = speed2 * Time.fixedDeltaTime;
             Position = Vector2.MoveTowards(Position, Target, step);
             rb.MovePosition(Position);
 
         }
         
     }
 
     private void FixedUpdate()
     {
         float dist = Vector2.Distance(Position, Target);
         waypointIndicator();
         
         if (dist < near)
         {
             chase = true;
         }
         else if(chase == true)
         {
             chase = false;
             chaseend = true;
         }
         else
         {
             float step = speed2 * Time.fixedDeltaTime;
             Position = Vector2.MoveTowards(Position, waypoints[waypointnum].transform.position, step);
             rb.MovePosition(Position);
         }
         
         if (chase == true)//HAS BEGUN CHASING, CHASES
         {
             ChaseChar();
         }
         else if (chaseend == true)
         {
             ////After time limit will reset back to original position.
         }
 
     }
 
     public void waypointIndicator()
     {
         //if increasing then add one to waypoint each time until reaching position
         // else if decreasing do same
         bool adding = true;
         bool subbing = false;
         bool moving = true;
 
         if(Position == new Vector2(waypoints[waypointnum].transform.position.x, waypoints[waypointnum].transform.position.y))
         {
             moving = false;
         }
         else { moving = true; }
 
         if (!moving)
         {
             if (adding)
             {
                 if (waypointnum < waypoints.Length-1)
                 {
                     waypointnum++;
                 }
                 else
                 {
                     waypointnum++;
                     adding = false;
                     subbing = true;
                 }
             }
             else if (subbing)
             {
                 if (waypointnum > 0)
                 {
                     waypointnum--;
                 }
                 else
                 {
                     waypointnum--;
                     adding = true;
                     subbing = false;
                 }
             }
         }
     }
         
 }
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 Crumpet · Feb 09, 2019 at 04:10 AM 0
Share

Just scrolled through real quick try line 106, seems to be adding without any checks, your code jumps back and forward a lot so would take time to read and figure out.

fixedupdate updates -> waypointIndicator() fires -> bool adding = true, its possible moving could be false. so as long as adding is true we just add a higher waypointnum since waypointnum++; is even in the else statement count which is pushing waypoints out of range I assume...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by 3kWikiGames · Feb 10, 2019 at 12:23 AM

"Sort of" fixed it, I just added the same waypoints to the waypoint array but in descending order, then added an if statement resetting the waypointnum back to zero when it gets back to the first point, creating a cycle for the enemy to walk through.

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

235 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

Related Questions

Enemy AI Movement To A Target(Player) In A 2D Game 3 Answers

How do I make an Enemy charge through a point? 1 Answer

moving object toward waypoint and alignment 0 Answers

Adjust custom PlayerControls to be used by enemies 1 Answer

Have 2D Sprite Follow the Mouse in Straight Line 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