I’m making a health bar, from the Brackey’s tutorial, I’ve tried lerp a million different ways and nothing, it moves at fractions, speed doesn’t change anything and I’m stumped, would appreciate some help:
using UnityEngine.UI;
public class HealthBar : MonoBehaviour
{
public Slider healthSlider;
public void SetMaxHealth(float health)
{
healthSlider.maxValue = health;
healthSlider.value = health;
}
public void SetHealth(float health)
{
healthSlider.value = health;
}
}
Well, you haven’t implemented any kind of a lerping function in there at all and you will likely want that to happen in the SetHealth
function.
private float targetHealth;
private float timeScale = 0;
private bool lerpingHealth = false;
public void SetHealth( float health )
{
targetHealth = health;
timeScale = 0;
if(!lerpingHealth)
StartCoroutine(LerpHealth());
}
private IEnumerator LerpHealth( )
{
float speed = 2;
float startHealth = healthSlider.value;
lerpingHealth = true;
while(timeScale < 1)
{
timeScale += Time.deltaTime * speed;
healthSlider.value = Mathf.Lerp(startHealth, targetHealth, timeScale);
}
lerpingHealth = false;
}
if anybody has still issues, it’s probably because there’s no yield return null
in the coroutine IEnumerator
method.
This way, it’s updated every frame. probably obvious, but just to be safe