Rukey4
1
Hello,
I’ve got a small portion of code which does move a GameObject to a random destination (Specified by the target in the array). It all works fine, I just want to know how to check when the “MoveTowards” has finished, so I can find a new destination or target. The last pseudo ‘if’ statement is what I’m trying to achieve. Thanks a lot!
#pragma strict
var target : Transform[];
var isMoving : boolean = false;
var speed : float = 5.0f;
var newTarget : Transform;
function Update()
{
if(isMoving == false)
{
newTarget = target[Random.Range(0,target.length)];
isMoving = true;
}
transform.position = Vector3.MoveTowards(transform.position, newTarget.position, speed * Time.deltaTime);
if(MoveTowards has finished)
{
isMoving = false;
}
}
if(Vector3.Distance(transform.position, newTarget.position) < 0.1f){
//It is within ~0.1f range, do stuff
}
Just check to see if transform.position equals newTarget.position