Move an object to a point and stop

By OnTriggerEnter on one of the triggers, it should make Target(s) move from a point to another and stop. Here’s the code:

 for (ae = 0; ae < Target.Length; ae = ae + 1) {
                    Point_To_Move = Target[ae].position + MoveFromPos;
                    Target[ae].position = Vector3.Lerp (Target[ae].position, Point_To_Move, Speed * Time.deltaTime);
                }

Point_To_Move will give the end position.

What can I do to solve this? Do I need to put it under a loop? If so, how?

Tried using a function with a loop. But no results.

for (ae = 0; ae < Target.Length; ae = ae + 1) {
                    Point_To_Move = Target[ae].position + MoveFromPos;
                    MoveObjectLerp(Target[ae].transform, Target[ae].position, Point_To_Move, Speed);
 }

Later in the code:

function MoveObjectLerp (thisTransform : Transform, startPos : Vector3, endPos : Vector3, timem : float) {

    var z = 0.0;

    var rate = 1.0/timem;

    while (z < 1.0) {

        z += Time.deltaTime * rate;

        thisTransform.position = Vector3.Lerp(startPos, endPos, z);

        yield;

    }

}

Bump, as I made a thread like this before and haven’t got the answer yet.

Well what exactly is the problem?

The targets are not moving.

Try this…

function OnTriggerEnter(other : Collider){
	if(other.ComapreTag("Player")){
		moveToPosition = true;
	}
}


function Update(){
	if(moveToPosition){
		transform.position = Vector3.MoveTowards(transform.position, target.position, speed);
		///check pos distance to target (as a way to a void missing target and moving infinitely)
		var dist = Vector3.Distance(target.position, transform.position);
		if(dist < 0.1f){
			//at pos
			moveToPosition = false;
		}
	}
}
1 Like
function Update () {
	
	for (ae = 0; ae < Target.Length; ae = ae + 1) { //Target is an array
	
		Point_To_Move = Target[ae].position + MoveFromPos; //MoveFromPos is a Vector3
		//Distance = Mathf.Abs (Vector3.Distance (Point_To_Move, Target[ae].position));
		
		if (MoveBoolean) {
			
				Target[ae].position = Vector3.Lerp(Target[ae].position, Point_To_Move, Speed);
			
		}
	}
}

Done that. Nothing works.