For some reason the coroutine logic is still beyond me.
What I would like is an object to wait a random amount of time then look for a destination to go to. If it finds a destination, no longer search for a destination and instead head to the destination. When it get’s to the destination, clear the destination and start the random timer again.
I have this code:
#pragma strict
var flightSpeed : int;
var maxWaitTime : float = 5;
private var LC : levelController;
private var destination : GameObject;
private var oldPos : Vector3;
function Start () {
LC = GameObject.Find('levelController').GetComponent(levelController);
while(!destination)
{
yield WaitForSeconds(Random.Range(0, maxWaitTime));
destination = LC.getRandomCloseBuilding(Vector2(transform.position.x, transform.position.z));
oldPos = transform.position;
transform.renderer.material.color = Color.yellow;
}
}
function Update () {
if(destination)
{
transform.renderer.material.color = Color.blue;
if(Vector3.Distance(transform.position, destination.transform.position) > 0.1)
{
transform.position = Vector3.Lerp(transform.position, destination.transform.position, Time.deltaTime * flightSpeed);
}
else
{
destination = null;
}
}
}
My funny debugging shows me that the object turns yellow but never blue.
Thank you for your help!