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 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;
}
}