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
1
Question by Kroltan · Jan 01, 2012 at 12:32 PM · arraywaypointindexoutofrangeexception

Waypoint system: loop and back-forth operations

Well, i am still improving my waypoint system, now i made a script to hold all waypoints as a Vector3 array, and a script in the object that i want to follow the path. All is working fine when i make my object loop trough the waypoints, like this:

 0 -> 1 -> 2
 0 <------ 2

But now i want to the object to follow the waypoint path backwards, then forward again (patrol style), like this:

 0 -> 1 -> 2
 0 <- 1 <- 2

Here's my waypoint script, wich is attached to a gameobject. This script is used to obtain the waypoints and display some graphical stuff in the editor:

     Waypoint.js
 
 @script ExecuteInEditMode()
 #if (UNITY_EDITOR)
 var waypoints : Vector3[];
 @HideInInspector
 var length : int;
 function OnDrawGizmos () {
     for (a = 0; a <= waypoints.length-1; a++) {
         if (a == waypoints.length-1) {
             Gizmos.color = Color.red;
             Gizmos.DrawLine(transform.position + waypoints[a],transform.position + waypoints[0]);
         }else{
             Gizmos.color = Color.yellow;
             Gizmos.DrawLine(transform.position + waypoints[a],transform.position + waypoints[a+1]);
         }
         Gizmos.color = Color.blue;
         Gizmos.DrawSphere(transform.position + waypoints[a], 0.2);
     }
 }
 #endif
 function GetWaypoint(w : int) : Vector3{
     return transform.position + waypoints[w];
 }
 function Update() {
     length = waypoints.Length;
 }

And this is my navigation script, wich handles the position of the object wich it is attached:

     AnimatePosition.js
 var waypoints : Waypoint;
 var speed : float;
 var loop : boolean = false;
 private var direction : boolean = false;
 private var time : float;
 private var state : int = 0;
 function Start () {
     time = Time.time;
 }
 function Update () {
     if (state == waypoints.length) {
         if (loop == true) {
             state = 0;
         }else{
             direction = true;
         }
     }
     if (state == 0 && direction == true) {
         direction = false;
     }
     Debug.Log(direction);
     transform.position = Vector3.Lerp(transform.position, waypoints.GetWaypoint(state), (Time.time - time)*speed);
     if (Mathf.Round(transform.position.x * 100) / 100 == Mathf.Round(waypoints.GetWaypoint(state).x * 100) / 100 && Mathf.Round(transform.position.y * 100) / 100 == Mathf.Round(waypoints.GetWaypoint(state).y * 100) / 100 && Mathf.Round(transform.position.z * 100) / 100 == Mathf.Round(waypoints.GetWaypoint(state).z * 100) / 100) {
         if (direction == false) {
             state++;
         }else if (direction == true){
             state = state-1;
         }
         time = Time.time;
         Debug.Log(state);
     }
 }

This code works fine with the "Loop" option selected (`private var loop : boolean`), but when i deselect it (to the back-and-forth operation) it starts erroring out about the array index being out of range. Probably, like my other questions, it will be a stupidly trivial error i can't notice.

Comment
Add comment · Show 3
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 fafase · Jan 01, 2012 at 12:57 PM 1
Share

What if you simply create waypoint3 on the same spot as waypoint1. As 0,1,2,0 works you would go 0,1,2,3,0 and 3 equals 1.

Probably not the most professional way but that should work.

avatar image Kroltan · Jan 02, 2012 at 12:34 PM 0
Share

Nice solution. Despite not being very professional I can't think a case this idea would fail. $$anonymous$$an, now i'd like this to be a answer than a comment

avatar image fafase · Jan 02, 2012 at 01:40 PM 0
Share

Yep As you found a better way I won't post that as an answer, also I put it as comment because I found it lacking professionalism.

1 Reply

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

Answer by Kroltan · Jan 02, 2012 at 12:56 PM

Nevermind, i found the solution after further experimentation. I was forgetting to subtract the state when the direction was going true. Changed bit of code: (Right under the Update() declaration in AnimatePosition.js)

if (state == waypoints.length) {
    if (loop == true) {
        state = 0;
    }else{
        direction = true;
        state--;
    }
}

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

IndexOutOfRangeExeption: Array index is out of range 1 Answer

How to learn amount of objects in selected prefabs folder? 0 Answers

Index is Less Than Zero + Flawed Code 1 Answer

IndexOutofRangeException 1 Answer

Array Shift error 1 Answer


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