Need help with Lerps!

Hi there!

I can’t seem to figure out how Lerps work exactly. I mean, I understand the concept, but what I’m trying to do isn’t working. I’m trying to get a platform to Lerp to a specific X value from its current X position. However, when running the code, it goes no where and Debug.Log(Output) is telling me that the X value is just staying the same for whatever reason.

I’ve attached a copy of the code that controls the platforms. If you don’t want to download it, I’ll put it here as well.

Code

public class PlatformController : MonoBehaviour
{
float Output = 0;
float CurrentXPos;

void Start()
{
CurrentXPos = transform.position.x;
}

void Update()
{
Vector3 temp = new Vector3(PlatformMovement(), transform.position.y, transform.position.z);
transform.position = temp;
Debug.Log(Output);
}

float PlatformMovement()
{
float accumulator = .001f;

Output = Mathf.Lerp(CurrentXPos, 8f, accumulator);

accumulator += .05f; //Constant speed

return Output;
}
}

Hopefully someone can help me! Thanks!

  • Joshua

3101121–234123–PlatformController.cs (667 Bytes)

It helps to use code tags instead of spoiler tags :slight_smile:

OK a couple things:

  1. accumulator is set to 0.001f every time you call PlatformMovement(). So you’re passing in the same time each time you try to lerp.

  2. Lerp’s time value should be between 0 to 1. At t=0, you get CurrentXPos. At t=1, you get 8f. There’s nothing in your code that stops it at 1.

1 Like

Well, it’s not moving because accumulator is a value between 0 and 1.
0 means you want to be at the start position. 1 means at the end position.

As your code is set, currentxpos is the start, and 8f is the end. You are telling it to travel .001f distance from currentxpos and return that value.

And @BlackPete beat me to it. But yeah. You need to increment the third value over time.

What you have will work if you just add the line

CurrentXPos = transform.position.x;

at the end of your Update method

Thanks everyone for your responses.

@BlackPete : I fixed your first note by setting the accumulator as a field, but I’m not sure what you mean by the second note you made. I know it has to be a value between 0 and 1, so how can I stop it at 1? Or even make it go back and forth?

public class PlatformController : MonoBehaviour
{
    float Output = 0;
    float CurrentXPos;
    float accumulator = .001f;

    void Start()
    {
        CurrentXPos = transform.position.x;
    }

    void Update()
    {
        PlatformMovement();
    }

    private void PlatformMovement()
    {
        Vector3 temp = new Vector3(Lerp(), transform.position.y, transform.position.z);
        transform.position = temp;
        Debug.Log(Output);
    }

    float Lerp()
    {
        Output = Mathf.Lerp(CurrentXPos, 8f, accumulator);

        accumulator += .05f; //Constant speed

        return Output;
    }
}

One of the following approaches could work:

accumulator = Mathf.Min(accumulator + 0.05f, 1f);

// or

accumulator = Mathf.PingPong(Time.time, 1f);

nb. Lerp is automatically clamped to (0,1) anyways.

Edit: if you want it to go backwards, as per one of your responses, check the value and if it’s >= 1, then maybe set a bool that says it’s going backwards. I dunno :wink:

@BlackPete Thanks, it seems to stop at 8 regardless if I use the Mathf.Min or just leave the Accumulator += value in.

However, both methods seem to yield an interesting result. The further to the left the platform spawns, the faster it goes, which I’m assuming has something to do with the percentages.

Edit: Also, the Mathf.PingPong did work, it’s just very fast.

If you are complaining about inconsistent speeds maybe you are looking to use MoveTowards instead.