- Home /
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.
Answer by starikcetin · Jul 02, 2015 at 07:23 AM
That's because you don't update the first lerp value. Try this:
transform.position = Vector2.Lerp(transform.position, DestinationPosition, 0.2f)
I was under the (incorrect) assumption that the starting position was held in memory until the lerp had completed itself. Now my doors are working perfectly.
Thank you for your help!
Answer by Hellium · Jul 02, 2015 at 07:38 AM
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.
Hey there. I knew about the deceleration method but not the constant method. Thank you for that!
Also, your pictures were clear and informative. Couldn't ask for more than that. :)
Use $$anonymous$$oveTowards if you want to keep the movement constant. Only the last iteration may be shorter.
Answer by Coffee-with-Venky · Jul 02, 2015 at 07:55 PM
Lerp Works only In Update !!
you can change the vector to position from another metheod !!!
Lerp is just a mathematical function. It will work anywhere, doesn't care about Update. You just have to vary one or more of the parameters over time.
Downvote for incorrect information. Lerp can work in custom methods too.
Your answer
Follow this Question
Related Questions
Diagonal Movement 1 Answer
Why doesnt Vector2.Lerp work? 1 Answer
How to get click position in Vector 2 ? 1 Answer
How can I find the velocity on a rigidbody which moves using Lerp? 1 Answer
Get angle between 2 Vector2's 5 Answers