- Home /
Lerp not working
I'm currently working on a menu. And I need to get it to slide. But my Lerp is working just like normal transform.position and I don't really know why. Heres the code I'm using! Any ideas?
var player : Transform;
private var fp : Vector2;
private var lp : Vector2;
var startp = Vector3(0,0,-5);
var pos1 = Vector3(5,0,-5);
function Start() { }
function Update() {
for (var touch : Touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fp = touch.position;
lp = touch.position;
}
if (touch.phase == TouchPhase.Moved )
{
lp = touch.position;
}
if(touch.phase == TouchPhase.Ended)
{
if((fp.x - lp.x) > 0.5) // left swipe
{
player.transform.position = Vector3.Lerp(startp, pos1, Time.deltaTime);
}
else if((fp.x - lp.x) < -0.5) // right swipe
{
player.transform.position = Vector3.Lerp(pos1, startp, Time.deltaTime);
}
}
}
}
Answer by rutter · May 21, 2014 at 12:51 AM
Oddly enough, I'm currently writing a blog post about this exact question. I'll drop the first section, here, since it's pretty much ready to go:
I see this sort of thing far too often:
transform.position = Vector3.Lerp(startPos, endPos, Time.deltaTime);
The person posting it is usually convinced that Vector3.Lerp
is "broken", but the real problem is that they're not using it correctly.
Lerp, short for "linear interpolation" does one very simple thing: given two values, x
and y
, it returns a value that is t
percent between them. If you expect the output to change, the arguments you pass in need to reflect that!
In the example above, it doesn't make sense to just pass in Time.deltaTime
, because that's only the time that passed during the most recent frame. If your game is running at a constant 50fps, that's always going to be 0.02
.
We'll get better results if we let that timer accumulate over multiple frames.
Something like this should get the point across:
public class LerpExample : MonoBehaviour {
float lerpTime = 1f;
float currentLerpTime;
float moveDistance = 10f;
Vector3 startPos;
Vector3 endPos;
protected void Start() {
startPos = transform.position;
endPos = transform.position + transform.up * moveDistance;
}
protected void Update() {
//reset when we press spacebar
if (Input.GetKeyDown(KeyCode.Space)) {
currentLerpTime = 0f;
}
//increment timer once per frame
currentLerpTime += Time.deltaTime;
if (currentLerpTime > lerpTime) {
currentLerpTime = lerpTime;
}
//lerp!
float perc = currentLerpTime / lerpTime;
transform.position = Vector3.Lerp(startPos, endPos, perc);
}
}
You could create an empty scene, drop in a cube, attach this script, and see it in action.
Notice the result if you change lerpTime
or moveDistance
.
That works yes, but when I try to convert It into the javascript and be affected only when i swipe, it still acts like normal transform.position
Thanks @rutter, this helped me to understand better Time.detalTime and fix a bug. Cheers
Hello @rutter I also have a problem with Lerp, but I understood it differently. In a line such as:
shopper.transform.forward = Vector3.Lerp(shopper.transform.forward, SceneIndex.instance.waypointTransforms[nextWayPoint].targetTransform.forward, 8f * Time.deltaTime
What will happen? Thanks
Your "shopper" object will rotate in a non linear fashion towards the forward direction of your target waypoint (the blue axis of that waypoint object).
What does it mean non linear?
If its written like that, will it be able to complete the transform.forward before moving on the the next function?
void Update(){
shopper.transform.forward = Vector3.Lerp(shopper.transform.forward, SceneIndex.instance.waypointTransforms[nextWayPoint].targetTransform.forward, 8f * Time.deltaTime)
ToReachPointState();
}