- Home /
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
Answer by AeonIxion · Jan 18, 2013 at 09:44 AM
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.
Well, if I use either Slap() or RevokeSlap() alone it is always working perfect. Time.deltaTime*10 is always below 1 which is giving me a smooth animation to the endpoint until it reaches it. For Slap() endPoint.z = 0, for revokeslap() endPoint.z = 10. Now tell me how should I change my func to reach that point? If I use if(endPos.z - OBJECT.transform.localPosition.z < 0.1) it will never give me z=10 but 9,xx which I don't want.. I want a simple moveanimation from x,y,10 to x,y,0 and back to x,y,10 afterwards.. Also: "you could check if the distance of the two z's is less than 0.1" <--- The distance never is less than 0.1. In my program Slap() works fine and after z=0 its going to like 3.29 -> 3.28 -> 3.29 -> 3.28 etc looping.. and sometimes its jumping to 10 after some time, sometimes not..
If I use "1" ins$$anonymous$$d of Time.deltaTime*10 it is also jumping back and forward but in revokeClap() but reaching 10 at some point.
Another interesting debug: I used a debug on OBJECT.transform.localPosition.z after entering Slap(), RevokeSlap() and after the lerp has ended. Output for Slap(): Run1: 10 -> 9.17165 Run2: 9.17165 -> 8.411917 Run2: 8.411917 -> 7.715116
As you can see the z after the Lerp is the same as the initial of the next run which is good. Now RevokeLerp(): Run1 0.09610127 -> 1.737771 Run2 1.442329 -> 3.068373 Run3 2.443556 -> 4.257556 Run4 3.436008 -> 4.524055
As you can see the value after Lerp doesnt match the value at the beginning of the next run which leads to a misscalculation of the whole function..
Answer by gulty · Jan 18, 2013 at 06:27 PM
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.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Parsing Error CS8025 1 Answer
Creating Projectile - MoveTo/Lerp 0 Answers
Unity Gllitching Everything? 1 Answer