Problem with Vector3.Lerp

Right now I am trying to use Vector3.Lerp to slide in a panel as part of my menu. However currently it returns a weird value overtime I try to do it.
Here is my code using UnityEngine;

using System.Collections;
using UnityEngine.UI;

public class MenuAnimations : MonoBehaviour {

public int WhichPanel;
public GameObject Panels;
public Transform Panel1Vector;
public Transform Panel2Vector;
public Transform Panel3Vector;
public Transform Panel4Vector; 
public float speed;

public void MovePanel (int WhichPanel)
	switch (WhichPanel)
		case 0:
		Panels.transform.position = Vector3.Lerp(Panels.transform.position, Panel1Vector.position, speed);

		case 1:
		Panels.transform.position = Vector3.Lerp(Panels.transform.position, Panel2Vector.position, speed);

		case 2:
		Panels.transform.position = Vector3.Lerp(Panels.transform.position, Panel3Vector.position, speed);

		case 3:
		Panels.transform.position = Vector3.Lerp(Panels.transform.position, Panel4Vector.position, speed);


For example, the Panel1Vector should be (125, 0, -8) but for some reason if I press my button to make WhichPanel = 0, the GameObject Panels goes to (369.1433, 0, -289.4084).

Does anyone know how to fix this or what my problem is.

Thank you


Unless you’re varying the value of speed somewhere else, you’re not using Vector3.Lerp correctly. Lerp works by returning the interpolation of the line between the two given vectors based on the third parameter, which should be between 0 and 1.

When you pass 0 to the Lerp function, it returns the first Vector3 argument. When you pass 1, it returns the 2nd Vector argument. If you pass 0.5, it returns a Vector3 half way between the two. So, in order to get a smooth transition between two Vector3s, you have to vary the 3rd argument over time. For example:

Vector3 pos = Vector3.Lerp( vector1, vector2, (Time.deltaTime / HowManySecondsYouWantItToTake) );

As an alternative to Lerp, you could use Vector3.MoveTowards, which works in the way you are trying to use Lerp.

You could also do all of this using animations. If you have time, I recommend this playlist by 3dBuzz on youtube. It covers a lot of information on UI development in a clearly explained way.