Vector2.Lerp sizes instantly

Hello, I’m trying to make an exp bar scale smoothly using lerp. When the player gains exp though the bar scales instantly even though I’m using lerp.

Here is a gif of the bar and the script
https://gyazo.com/13f94f3642f736cfe2908462b1f0b17d

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Exp : MonoBehaviour
{
    //Stats
    public float minExp;
    public float maxExp;
    public float exp;
    private float minWidth = 250;
    private float maxWidth = 1509;
    private float width;
    [SerializeField]
    private float lerpSpeed;
    //Components
    public RectTransform bar;

    void Start()
    {
        UpdateBar();
    }
    public void UpdateBar()
    {
        exp = Mathf.Clamp(exp, minExp, maxExp);
        width = (exp / maxExp) * (maxWidth - minWidth) + minWidth;
        bar.sizeDelta = Vector2.Lerp(bar.sizeDelta, new Vector2(width, bar.sizeDelta.y), lerpSpeed);
    }
}

Lerp will do whatever you tell it to do.

If you pass a value >= 1 in the third parameter, it will return the second parameter you give it immediately.

If you give it .5 it will give you a value midway between the first and second parameters in a single frame.

Read this and make sure you understand what the Lerp function actually does Unity - Scripting API: Vector2.Lerp

Basically you’re using Lerp in a bit of a funky and framerate dependent way.

1 Like

to run it from Start, could use coroutine
http://answers.unity.com/answers/1502200/view.html

1 Like

Thank you for your replies, the problem is solved!
https://gyazo.com/647567f42d3ad697d9cecff205435ff4