- Home /
something wrong with vector3.Lerp.
I just made a simple code. What I try to do is that if I click on a button object, 'the Thing' moves from pointA to pointB smoothly. And then I click the button again? Yes, 'the thing' moves from pointB to pointA.
Just simple thing. I made code and applied on the button object. After all, something strange happened. I know 'Vector3.Lerp' make an object move smoothly. But, in this case, 'the Thing' just moved from pointA to pointB. Like teleport. Not smooth movement. Why is that happen? What I did wrong?
var pointA : Transform;
var pointB : Transform;
var looktarget : Transform;
var theThing : Transform;
var moveState = 0;
function OnMouseDown(){
if(moveState == 0){
moveState = 1;
theThing.transform.position = Vector3.Lerp(pointA.position,pointB.position,Time.time);
theThing.transform.LookAt(looktarget.position);
}
else if(moveState == 1){
moveState = 0;
theThing.transform.position = Vector3.Lerp(pointB.position,pointA.position,Time.time);
theThing.transform.LookAt(looktarget.position);
}
Debug.Log("moveState is "+moveState);
}
Answer by tigerfoot · Aug 20, 2012 at 02:18 AM
Time in Lerp function is clamped between 0 and 1. 0 means your object is at starting position and 1 means your object is at target position. Therefore you must create a variable which you will increment from 0 to 1 and put that in Lerp and not Time.time or Time.deltaTime.
If you put Time.deltaTime your object will be somewhere around middle between starting position and target position. It will never reach your goal, unless your FPS drops to 0 or 1 FPS.
THIS will explain it to you much better than me.
You were wrong, I was right. $$anonymous$$ultiplying a variable by Time.deltaTime makes it framerate independent.
However, using it alone as the third parameter in Lerp generally does not accomplish what you want. tigerfoot is pretty much correct here. Aside from the link posted, see this: http://answers.unity3d.com/questions/14288/can-someone-explain-how-using-timedeltatime-as-t-i.html
Answer by TheVectorHunter · Aug 19, 2012 at 08:41 PM
What you did wrong was you used 'Time.time'. 'Time.time' could be 100 seconds in by the time you use it which means it would immediately place you at your destination.
http://docs.unity3d.com/Documentation/ScriptReference/Time-time.html
What you need to use is this:
http://docs.unity3d.com/Documentation/ScriptReference/Time-deltaTime.html
Make a variable that is incremented/decremented and then multiplied by 'Time.deltaTime' so it moves by seconds not frames.
It can never be 100 because time in Lerp is clamped between 0 and 1.
And that is not the speed of movement. 0 means starting position and 1 means target position. Time.deltaTime will not get the object to reach target position.
That was not what I said read my answer thoroughly before you criticize it.
Thank you for your answer but i couldn't solve the problem...Yeah i already tried several ways include 'Time.deltaTime.'.... And there's another mystery happened..haha.. The object(theThing)'s speed was not changed but final position was changed a little bit...it means theThing teleported to the other position.(not pointB.) i think time/timedeltatime thing is not important. i wonder if 'vertor3.Lerp' dosen't work under 'function On$$anonymous$$ouseDown'.
Lmfao, I didn't notice that it was in On$$anonymous$$ouseDown()... that is a one time function/method call, it is only called once upon the user clicking the mouse.
To fix this problem you should have a coroutine setup that will be called upon the On$$anonymous$$ouseDown(), and will cause 'theThing' to move for however long you want the motion to take.
Your answer
Follow this Question
Related Questions
Move an object by clicking on another 3 Answers
Slerp / lerp not creating a smooth transition 2 Answers
How to move an object from point A to point B with one key press 3 Answers
Smoothing motion with vector3 lerp 3 Answers
interpolate a move variable 0 Answers