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 MiniForge · Aug 16, 2014 at 01:56 AM · c#vector3looploopingpatrol

C# List looping Vector3's

Short story-I have a box and I want it to patrol a list of Vector3's in order. I want it to go through each List value and then go back to the first list value once its done. However, it seems that my code gets stuck in my 'if' block of my if/else. The box spins in circles extremely fast and is stuck infinitely in the if statement. Any help would be greatly appreciated!

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class NpcPatrol : MonoBehaviour {
 
     public List<Vector3> patrolPoints;
 
     //current vector we are traveling to
     private Vector3 currentPoint;
     private float moveSpeed=6;
 
     //how big this array is and what position we are at for patrol
     private int listMax;
     private int curIndex=0;
 
     // Use this for initialization
     void Start () {
         //sets the size of array
         listMax = patrolPoints.Count;
         currentPoint = patrolPoints [0];
         print (listMax);
         print (curIndex);
     }
     
     // Update is called once per frame
     void Update () {
         //travel until you get to the point
         //look at the target
         transform.LookAt (currentPoint);
         //move forward towards the target
         transform.Translate (new Vector3 (0, 0, 1) * Time.deltaTime * moveSpeed);
 
 
         //check are we at the point
         if (Vector3.Distance (transform.position, currentPoint) < 0.5) {
             //at the point change the new objective by moving ahead in the list!
             if(curIndex<listMax){
                 curIndex=curIndex++;
                 currentPoint=patrolPoints[curIndex];
                 print(curIndex+" if");
             }
             else{
                 //else we need to go back to the start of the list
                 curIndex=0;
                 currentPoint=patrolPoints[curIndex];
                 print(curIndex+" else");
             }
         }
     }
 }


alt text

alt text

Added some screen shots to clarify. I am in a 3d world but moving the objects in only x and z. Y is constant to .5

screen shot 2014-08-16 at 12.16.11 am.png (22.6 kB)
screen shot 2014-08-16 at 12.16.17 am.png (15.4 kB)
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 Kiwasi · Aug 16, 2014 at 02:27 AM 0
Share

Are all of the points at least 0.5f apart? $$anonymous$$ight be worth trying to drop the threshold down to a lower value.

avatar image MiniForge · Aug 16, 2014 at 03:02 AM 0
Share

I have all the Vector3 points over 100 units apart. I've tried lowering the value to .25 but i'll try lower

avatar image matthewbauer · Aug 16, 2014 at 06:15 AM 1
Share

I have seen something like this in my code. Is the box moving in 3 dimensions or 2? I had a problem where the object was moving in 2 dimensions but I tested distance using 3. I set the .y value to 0 in both vectors before running Vector3.Distance and it was fixed.

avatar image Kiwasi · Aug 16, 2014 at 06:31 AM 0
Share

You might just be onto it @matthewbauer

Check the actual transform position against the target position. If the height is out by more then 0.5f then the target will never be reached.

avatar image MiniForge · Aug 16, 2014 at 07:23 AM 0
Share

I want to vectors to have .5 for the y so I'll try what you're saying @matthewbauer but with .5 for the coordinates

2 Replies

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

Answer by MiniForge · Aug 16, 2014 at 07:38 AM

After debugging my code and taking a closer look I decided to rewrite this line

  curIndex=curIndex++;

and replace it with

 curIndex=curIndex+1;

This fixed the issue. I was simply not updating the List position. Thank you so much for the ideas! I'll keep them in mind as I write more scripts!

Comment
Add comment · Show 2 · 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 Bunny83 · Aug 16, 2014 at 08:20 AM 3
Share

The pre and post increment operators often causes confusion. The post increment operator you're using increments the variable after it has been evaluated, but before the evaluated value is used, in your case the assignment operation.

Your line

     curIndex=curIndex++;

Actually does this

     int tmp = curIndex;      // evaluate
     curIndex = curIndex + 1; // increment
     
     curIndex = tmp;          // assignment

This is a very common gotcha

avatar image MiniForge · Aug 17, 2014 at 03:26 AM 0
Share

Thanks for the explanation. Didn't even realize that made a difference!

avatar image
1

Answer by UnityUser666 · Aug 16, 2014 at 09:53 AM

Sorry about that. Kid tested, mother approved.

 public class NpcPatrol : MonoBehaviour 
 {
     
     public List<GameObject> patrolPoints;
     
     //current vector we are traveling to
     public Vector3 currentPoint;
     public float moveSpeed = 0.5f;
     
     //how big this array is and what position we are at for patrol
     public int listMax;
     public int curIndex = 0;
         
     // Use this for initialization
     void Start () 
     {
         //sets the size of array
         listMax = patrolPoints.Count;
         currentPoint = patrolPoints[0].transform.position;
     }
         
     // Update is called once per frame
     void Update () 
     {
         //look at the target
         transform.LookAt(currentPoint);
 
         transform.Translate(Vector3.forward * (Time.deltaTime * moveSpeed));
 
         //check are we at the point
         if(Vector3.Distance(transform.position, currentPoint) < 0.5f)
         {
             Debug.Log("We hit a patrol point!");
 
             curIndex++;
             if(curIndex < listMax)
             {
                 currentPoint = patrolPoints[curIndex].transform.position;
             }
             else
             {
                 curIndex = 0;
                 currentPoint = patrolPoints[curIndex].transform.position;
             }
         }
     }
 }

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

25 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

Related Questions

Loop Coroutine For A Demo Mode 1 Answer

How can I constantly change the position of a target when an object reaches it. 0 Answers

Distribute terrain in zones 3 Answers

Nested Array? Populating a menu using a loop. 0 Answers

Saving and applying an array of Vector3 velocities on an array of GameObjects 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