Trying to have an object follow a set of Vector3 coordinates one at a time as a form of very specific pathfinding. I have a list of Vector3s and I’m trying to iterate through it but I’m causing an infinite loop or something that locks up unity. Any ideas? Thanks in advance.
All the loops are processed in a single frame, the reason the code you’ve posted causes an infinite loop means that the conditions are never met(transform.position == CARGOSTORAGE is that condition here). If they did what you would see is a long single frame that processes all the dest in MULE1_TO_CARGO and the next frame which shows the result.
What you have to do is to convert it into a coroutine and move to the next frame after each loop so you will see the results and the change in your objects position each frame.
Never ever compare two vectors for equality. This is not possible to reach exactly a target position.
Everything inside Update is executed within one frame. But what really you want is to move a step towards your target destination.
Following what @phodges and @sarper suggested, you need to rethink your method (or routine) as a single step, not the complete movement. Using simplified code, this will look like:
void Update()
{
// get the distance to the target (we use a square distance as it is faster to calculate)
float sqrDistance = (this.transform.position - targetPosition).sqrMagnitude;
if (sqrDistance < (minStep*minStep))
{
// Move the object to the target position, as we are very close
this.transform.position = targetPosition;
}
else
{
// Move a step
this.transform.position = Vector3.Lerp(this.transform.position, targetPosition, Time.deltaTime * moveSpeed);
}