- Home /
Vector3.Lerp doesn't work!
So i have a code:
if (Input.GetKey (KeyCode.UpArrow))
{
float X = player.transform.position.x;
float Y = player.transform.position.y;
Vector3 Tunel_1 = new Vector3(X, Y, 0);
Vector3 Tunel_2 = new Vector3(X, Y, 5);
var position = Vector3.Lerp(Tunel_1, Tunel_2, Time.deltaTime * smooth);
player.transform.position = position;
And I don't know why does my lerp not work. Have you got any ideas?
Lerp works best with a t value that varies within the range 0 to 1. Time.deltaTime is a poor fit as it only varies when the framerate fluctuates.
So how can I write this code using this value?
public float T = 0.5;
player.transform.position = Vector3.Lerp(Tunel_1, Tunel_2, T * smooth);
?
Try this, it should demonstrate the concept:
float t = $$anonymous$$athf.Repeat(Time.time / 10.0f, 1.0f);
Debug.Log(t);
player.transform.position = Vector3.Lerp(Tunel_1, Tunel_2, t);
There are no compilation fails too, but lerp still doesn't work :/
Answer by Statement · Dec 23, 2013 at 11:34 AM
Lerp gives you an interpolated value between A and B.
In your case it was between (X, Y, 0) and (X, Y, 5).
I suspect you expected the object to go from (X, Y, 0) and (X, Y, 5) over time but what happens is that you are sampling one position between these two points and accept that as your new position. I suspect you expected that you could just feed back this value for the next iteration, but since you are discarding Z, so you are always moving between 0 to 5 in Z but you lose track of your progress. Each call to Lerp will cause the animation to start over.
Try this instead:
using UnityEngine;
public class LerpMove : MonoBehaviour
{
public GameObject player;
public float smooth = 2;
void Update ()
{
if (Input.GetKey (KeyCode.UpArrow))
MovePlayerTowardZ5 ();
}
void MovePlayerTowardZ5 ()
{
var from = player.transform.position;
var to = from;
to.z = 5;
var newPosition = Vector3.Lerp (from, to, Time.deltaTime * smooth);
player.transform.position = newPosition;
}
}
The expected behaviour of the snippet and best as I could interpret your intention is that when you hold down the up arrow key, player will move toward Z = 5, but since you are using Lerp, you will never actually reach Z = 5.
$$anonymous$$aybe usage of Lerp in this manner is widespread since it seems to give "smooth" movement which often is desirable, but I find it in most cases to be incorrect to use Lerp. Think of using lerp like this:
You have 10 miles to go to your destination. So you walk 50% of them and have 5 miles left to go for the next frame. The next frame you walk 50% of the remaining 5 miles and have 2.5 miles left to go for the next frame. Then 1.25. Then 0.625, 0.3125, 0.15625 and so on. If you keep dividing the distance you have left to go each time, you will never reach there (same goes for using Time.deltaTime). You will eventually get very very close, but you will never get to exactly Z = 5. It will just take longer and longer to cover less distance.
Consider using Vector3.SmoothDamp or Vector3.$$anonymous$$oveTowards ins$$anonymous$$d, which will get rid of that effect, if you agree that it is undesirable to use Lerp.
Your answer
Follow this Question
Related Questions
transform.position.y and x or z + 1? 0 Answers
Problem with transform 2 Answers
Help around an approach to a "Virtual Motion Table" in Unity 0 Answers
Instantiated prefab position 5 Answers