- Home /
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");
}
}
}
}
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
Are all of the points at least 0.5f apart? $$anonymous$$ight be worth trying to drop the threshold down to a lower value.
I have all the Vector3 points over 100 units apart. I've tried lowering the value to .25 but i'll try lower
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.
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.
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
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!
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
Thanks for the explanation. Didn't even realize that made a difference!
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;
}
}
}
}
Your answer
Follow this Question
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