Lerp bugging for- and backwards

Hey, I’m creating a little adventure and I have a problem with the Lerp/MoveTowards func. I have a slap function which moves an object to its direction witch a changed z coordinate. Afterwards its calling a revokeSlap func which is moving the object back to its place. Ill paste you some codesnippets:
On mouseclick i set: slap = true; inside the update func.

	if (slap){
		Slap();
	}

	if (!wait4Lerp && revoke){
		RevokeSlap();
	}

is also part of the update func

Now the functions itself:

function Slap(){
	wait4Lerp = true;	
	startPos = OBJECT.transform.localPosition;
	endPos = OBJECT.transform.localPosition;
	endPos.z = 0;
	OBJECT.transform.localPosition = Vector3.Lerp(
		startPos, 
		endPos, 
		Time.deltaTime*10
	);
	if(OBJECT.transform.localPosition == endPos) {
		wait4Lerp = false;
		if (!Overlap()){
			slap = false;
			revoke = true;
		}
	}		
}

Overlap is nothing interesting here…
This works fine. Moving OBJECT to its current position with the z=0 value.
If the position is reached I disable slap, wait4Lerp and set revoke to true to activate the function which returns to the original position:

function RevokeSlap(){
	startPos = OBJECT.transform.localPosition;
	endPos = OBJECT.transform.localPosition;
        endPos.z = 10;
	var test:float = OBJECT.transform.localPosition.z;
	Debug.Log(test);
	OBJECT.transform.localPosition = Vector3.Lerp(
		startPos,
		endPos,  
		Time.deltaTime*10
	);
	//Debug.Log(OBJECT.transform.localPosition.z);
	if(OBJECT.transform.localPosition == endPos) {
		revoke = false;
		revokeComplete = true;
	};

}

Now the Problem is that it mostly never reaches the endPos. It starts but in the middle it’s simply going for- and backwards very very quickly and in a very small range. It’s calling the RevokeSlap(); function over and over but never reaching the endPos. Sometimes after a few seconds of waiting it jumps to it and its working but still its just luck… What am I doing wrong? Same problem with moveTowards… Hope you can help me further…

Greets

The endpos is only reached if the 3rd parameter of Vector3.Lerp is 1. If it’s lower than 1 it will keep getting closer to the endpos, but never reach it. Unless the difference is so little that it will round to the endpos.

Instead of comparing the localposition and endpos, you could check if the distance of the two z’s is less than 0.1 for example.

Okay, I found a solution myself. yield WaitForEndOfFrame; did the trick. I noticed that it is setting slap=false and revoke = true more than once because the animation wasn’t yet finished. Therefor my if statement seemed to be setting variables even tho z wasn’t really 0. On the otherside the other lerp function already startet so they kinda overlapped.