- Home /
Smoothing motion with vector3 lerp
Hi there.
Ok, so the answer is in here: Oscilating variable
None of the Vector3.Lerp, SmoothDamp and pingPong solved my problems.
I have a simple question about lerp. I tried to make my gameobjects float(from transform.position.y to transform.positiony-1 and back again), but cant get it to work using unty's reference code from the Vector3.lerp page. In my scene i have some instantiated cubes, theat i want to float to transform.position.y -1, but it just goes to -infinite on y axis. To fix that i tried to declare a new Vector3 of this object in the Start function. Problem with this is that objects with this script are jumping rapidly in one position. Why is that? I think i overlooked something very basic here, or im trying to do this with wrong kind of code. My code(basically the reference code):
var start: Vector3;
var end: Vector3;
function Start () {
start = transform.position;
end =Vector3(transform.position.x,transform.position.y-10,transform.position.z);
}
function Update () {
var distanceTo = start - end;
var smooth = 1.0;
transform.position = Vector3.Lerp(start,end, Time.deltaTime* smooth);
}
Answer by benni05 · Jun 25, 2011 at 04:03 PM
There is one crucial difference to the reference code: the latter one uses Time.time not Time.deltaTime.
You could also try this:
transform.position = Vector3.Lerp(start, end, Mathf.PingPong(Time.time, 1.0));
for a forth and back movement.
Ben
Oops, I missed that. I am so used to seeing Time.time there. Also, with the ping pong, you could also use $$anonymous$$athf.Sin(Time.time)/2+.5f
if you want a smoother transition.
Answer by ckfinite · Jun 25, 2011 at 04:03 PM
Set your smooth variable to a very small number, like .01. Why? Well, Vector3.Lerp runs from 0 to 1, between the two position where 0 is the start and 1 is the end. This means that if you feed just Time.time, in seconds, it will move that distance in 1 second, which is a shorter amount of time than most people think.
Answer by Woj_Gabel_FertileSky · Jun 25, 2011 at 04:20 PM
Ben 36: Thanks for the PingPong. I now dont have to use if's. And the Time.time did the trick.
Unfortunately its not lerping. It just going to -1 and back again, without smoothing. Same is with Vector3.SmoothDamp.
It all have the same problem of not lerping, or jumping in one place.
And when using code from unity reference i could drag object with script in the scene viewer, but now i cant. I hope that wont be a problem when i want to use force on those objects...
Please use comments ins$$anonymous$$d of creating new answers ;-) Apart from that, please try Vector3.SmoothDamp function, this might be the solution for you. But there the time parameter is Time.deltaTime again, be aware (it's optional).
Well, SmoothDamp does the same as first script - object just jump in one position. Tried all kind of values.
Its really weird that the basic function is so messed up when vectors are referenced from start function.
Your answer
Follow this Question
Related Questions
moving between two points over time 1 Answer
How to smooth my camera (Vector3.Lerp) 0 Answers
How would I smooth out a Quaternion? 2 Answers
smoothly change position of an Object 1 Answer
Issues with Lerps 2 Answers