- Home /
Issues with Lerps
I want an object to be able to move between a few position and have a different rotation at each position. This is the script I am using:
var duration : float = 1.0; private var startPoint : Vector3; private var startTime : float; var move : boolean = false; function Start() { startTime = Time.time; } function Update () { if(move == true){ transform.position = Vector3.Lerp(transform, transform, (Time.time - startTime) / duration); transform.rotation = Quaternion.Lerp(transform, transform, (Time.time - startTime) / duration); } if (move == false){ transform.position = Vector3.Lerp(transform, transform, (Time.time - startTime) / duration); transform.rotation = Quaternion.Lerp(transform, transform, (Time.time - startTime) / duration); } }
The Issue is that when the move variable is toggled. The object appears directly at the specified transform without the motion in between. Shouldn't it smoothly transition to the specified transform? Any help would be very appreciated.
Please format your script correctly. And fix it, I'm pretty sure this does not compile:
transform.position = Vector3.Lerp(transform, transform, (Time.time - startTime) / duration);
Because transform is a Transform object not a Vector3.
Answer by meat5000 · Aug 05, 2013 at 07:24 PM
I know this is old but I read it so I figure someone else might.
transform.position = Vector3.Lerp works for me. I notice in your script transform.position = Vector3.Lerp(transform,transform etc where the 'from' and 'to' are both the same place, so no movement (so I assume you are snapping in objects in editor?)
Anyway, your issue: Lerp and Slerp can be treated like weighting in that its third field (where t usually goes) is between 0 and 1 only. As your duration is 1 and startTime is only called once in start() function it seems that t becomes greater than 1 in 1 second and hence your lerp will always Snap To the target (which is its starting position in your case?).
You need to normalise your t factor so that over a chosen duration of time your varibles scale up to 1. For this I like to use my own timers:
lerpTimer += Time.deltaTime; as for me its easier to get my head around. Then when the lerp is complete, reset your timer; lerpTimer = 0;
I had troubles and I read that I was using lerp incorrectly to begin. It does NOT perform a full smooth movement when called but RETURNS the SCALED DIFFERENCE between two factors every update/fixedupdate with which you are responsible for maintaining and changing the t factor to manipulate the transition yourself. Imagine t as a slider and you are responsible for moving the slider in your code, or as it is, providing a change between 0 and 1, and this decides the speed and smoothness of your transition.
extract of my game :
transform.position = Vector3.Slerp(cannonMoveFrom, cannonMoveTo, lerpTimer/spawnLerp); lerpTimer += Time.fixedDeltaTime;
here, spawnLerp is fixed value, say 10. So, when lerpTimer hits 10 seconds t = 1; in 5 seconds, 5/10 = 0.5 so lerp is halfway along and transform.position is instantaneously set to halfway between cannonMoveFrom and cannonMoveTo.
I was stumped for a while as I was calling lerp from within a timed if() function and of course the lerp calcaulation was only being performed once every 3 seconds, or whatever. I needed the lerp to happen seconds after an event so I used the if() to reset the timers and used the else() for the actual Lerp. Worked like a charm.
Hope this helps at least 1 person, with which I will be happy.
Answer by Owen-Reynolds · Mar 20, 2012 at 11:27 PM
Lerp is mostly useful for giving a "fast then slow" zooming camera move. For regular motion, easier to use:
transform.position = Vector3.MoveTowards(transform.position, target, moveSpeed);
Then all you have to do is set target
to where you want to go. You don't have to worry about startTime anymore. Instead of setting duration for how long it takes, adjust moveSpeed. You can still use move (T/F)
, but don't have to -- setting target to where you are now will automatically stop you.