Having difficulty with Lerp

Hello all.

I’m a relatively new user and have been banging my head against this for a while. I am trying to get an object to lerp but it only ever moves once for the value of the float at the end of the lerp statement. Here is my code. (It’s for a sliding door.)

using UnityEngine;
using System.Collections;

public class VertUpDoor : MonoBehaviour {

	private Vector2 InitialPosition;			//  Records door's initial position.
	private Vector2 DestinationPosition;		//  Where the door will open to.


	void Start () {
	
		InitialPosition = new Vector2 (transform.position.x, transform.position.y);	//  Records the door's original position.
		DestinationPosition = new Vector2(transform.position.x, transform.position.y + 0.9f);  //  Provides the doors destination position.

	}    	

	void FixedUpdate () {
    
	}
    
	void OnTriggerStay2D (Collider2D col) {

		if (col.gameObject.tag == "DoorOpener")
		{
			transform.position = Vector2.Lerp(InitialPosition, DestinationPosition, 0.2f);
		}    
	}        
}

I’m assigning values to InitialPosition and DestinationPosition as I want to make a prefab as all doors will slide by +0.9f in the Y direction. The objects moving through the sliding doors have “DoorOpener” attached as tags.

I’ve looked through tutorials and the forums but cannot get it to work. I’m feeling really stupid at the moment and am hoping someone can help me.

Thank you.

It seems you didn’t get what is LERP in fact.

LERP means “Linear intERPolation”. With this mathematic tool, you are able to interpolate a value between two others using a linear function ( y = ax + b ) and a coefficient t between 0 and 1.

Supposing you are trying to get a position (C) between two others (A and B) (like your problem), you have the folowing drawing :

With the folowing line located in the OnTriggerStay2D function :

transform.position = Vector2.Lerp(InitialPosition, DestinationPosition, 0.2f);

The positions A and B doesn’t change, neither do your parameter. Thus, you have two options :

1°) Make the parameter t vary between 0 and 1 : The movement curve will be perfectly straight : the speed will be the same over time.

 // The slide movement will take SlideDuration seconds.
 public float SlideDuration = 5 ;
 private float timer ;

 void OnTriggerEnter2D (Collider2D col)
 { 
     if (col.gameObject.tag == "DoorOpener")
         timer = 0 ;   
 }

void OnTriggerStay2D (Collider2D col) {
 
     if (col.gameObject.tag == "DoorOpener")
     {
         timer += Time.deltaTime ;
         transform.position = Vector2.Lerp(InitialPosition, DestinationPosition, timer / SlideDuration  );
     }    
 }

2°) Change the position of your position A : The movement will decelerate over time

void OnTriggerStay2D (Collider2D col) {

     if (col.gameObject.tag == "DoorOpener")
     {
         transform.position = Vector2.Lerp(transform.position, DestinationPosition, 0.2 );
     }    
 }   

Sorry for bad drawings.

That’s because you don’t update the first lerp value. Try this:

transform.position = Vector2.Lerp(transform.position, DestinationPosition, 0.2f)

Lerp Works only In Update !!

you can change the vector to position from another metheod !!!