I feel like this should be simple, but I just cant get the desired outcome.
I am trying to move an object with a lerp over .5 seconds when a button is pressed, this is my script:
public GameObject player;
public GameObject reference;
float lerpTime = .5f;
float currentLerpTime;
Vector3 startPos;
Vector3 endPos;
bool click;
public void OnClick ()
{
click = true;
}
void Start ()
{
startPos = player.transform.position;
endPos = reference.transform.position;
}
void Update ()
{
if (click == true) {
currentLerpTime = 0f;
}
currentLerpTime += Time.deltaTime;
if (currentLerpTime > lerpTime) {
currentLerpTime = lerpTime;
}
float perc = currentLerpTime / lerpTime;
player.transform.position = Vector3.Lerp (startPos, endPos , perc);
}
}
All that happens is that when I play, my object slides forwards and then the buttons do nothing. I have also tried not using startPos and endPos and just using player.transform.position ect… in the lerp function. That works but the object teleports, and has no smooth movement.