Objects not moving smoothly with Vector3.Lerp
So I'm trying to make a drawer of sorts that will pull a couple of prefabs from out of the frame. However I've been trying to get them to smoothly move from out of frame into frame and I can't seem to get it right.
I've been fiddling with the Vector.Lerp() but it doesn't seem to work like I thought it would.
After detecting if the "drawer handle" has been clicked I have the objects set to move with :
var smoothTime = 2;
var atomSpawner : GameObject;
var startPos : Vector3;
var endPos : Vector3;
function Update(){
if(drawerOpen == true){
if(hit.collider.tag == "slider"){
atomSpawner.transform.position = Vector3.Lerp(atomSpawner.transform.position, startPos, Time.deltaTime * smoothTime);
drawerOpen = false;
}
}
else{
if(hit.collider.tag == "slider"){
atomSpawner.transform.position = Vector3.Lerp(atomSpawner.transform.position, endPos, Time.deltaTime * smoothTime);
drawerOpen = true;
}
}
}
I've tried messing around with the smoothTime number and that doesn't seem to affect it all that much, I've also tried swapping out atomSpawner.transform.position with either endPos and startPos, which just cause it to go from endPos to startPos / startPos to endPos immedietly.
lerp should be used with start position, end position and current time in (0...1) area like: Vector3.Lerp(startPos,endPos,time);
with time being 0 or lower means object is in startPos, time being 1 or higher means object is in endPos. So you should first remember start time. Then current time $$anonymous$$us start time divided by smoothTime is what you supply to Lerp.
Answer by Jessespike · Feb 06, 2016 at 08:53 PM
http://docs.unity3d.com/ScriptReference/Mathf.Lerp.html
You need to pass a value that goes from 0 to 1 and you need a start and end position. Try something like:
float lerpTime = 0f;
...
lerpTime = Mathf.Clamp(lerpTime + Time.deltaTime * smoothTime, 0f, 1f);
atomSpawner.transform.position = Vector3.Lerp(startPos, endPos, lerpTime);
Hmm. In my experience, even when using Lerp 'the wrong way' the movement is relatively smooth as long as deltaTime is used.
$$anonymous$$aybe i'm having trouble reading this on mobile but to me it looks like the script would make drawerOpen
toggle after each Lerp call and make it alternate Lerping the object in and out in consecutive Update calls...
Well, glad if this helped the OP get the issue fixed :)
Answer by Ideka · Feb 07, 2016 at 12:41 AM
Vector3.Lerp doesn't take a "velocity" or "lerp amount," it takes a percentage, i.e. a float from 0 to 1. As the docs say:
When t = 0 returns a. When t = 1 returns b. When t = 0.5 returns the point midway between a and b.
Try Vector3.MoveTowards.
Your answer
Follow this Question
Related Questions
Is there a way to smoothly change a float number between two numbers when a key is pressed? 1 Answer
Change of Scene 1 Answer
Multiplayer 2 Answers
How to switch mouse with arrows ! 0 Answers