Hello, im stuck since this morning(in china) im trying to make my Health and shield bar Sliders move when there is damages. but i want to make the slider value moving smoothly with a lerp, but i cant find the proper way to do this, i feel im very close to, could someone check my script please? Code (csharp): using UnityEngine; using System.Collections; public class HealthBar : MonoBehaviour { public int CurHealth; public int Maxhealth; public int CurShield; public int Maxshield; GameObject CombatPanel; public float test; int OldCurHealth; int OldCurShield; //Bar Floats public float HealthFloat; public float ShieldFloat; public float EaseTime; public bool EaseBool=false; public void GetStats() { Maxhealth = CombatPanel.GetComponent<CombatManager>().MaxHealth; Maxshield = CombatPanel.GetComponent<CombatManager>().ShieldMax; CurHealth = CombatPanel.GetComponent<CombatManager>().Health; CurShield = CombatPanel.GetComponent<CombatManager>().ShieldValue; } public void GetOldStats() { OldCurHealth = CombatPanel.GetComponent<CombatManager>().Health; OldCurShield = CombatPanel.GetComponent<CombatManager>().ShieldValue; } void Start() { CombatPanel =GameObject.FindGameObjectWithTag("CombatPanel"); GetStats (); GetOldStats(); } public void HealthChange() { GetStats (); //set Sliders (animate) StartCoroutine("EaseBAR"); GameObject.Find ("SliderSB").GetComponent<UISlider>().sliderValue = ShieldFloat; GameObject.Find ("SliderHB").GetComponent<UISlider>().sliderValue = HealthFloat; //get old values at the end to compare GetOldStats (); } // Update is called once per frame void Update () { HealthChange (); if(EaseBool) { EaseTime=Time.deltaTime; if(EaseTime>1) { //stopeasing } } } void Curveanim() { //shield ShieldFloat = ((float)CurShield)/((float)Maxshield); //health HealthFloat = ((float)CurHealth)/((float)Maxhealth); //ease test = Mathf.Lerp(1.0f,3.0f, 6.0f);// bug here: test stay to 0 GameObject.Find ("SliderSB").GetComponent<UISlider>().sliderValue = Mathf.Lerp(((float)OldCurShield),ShieldFloat, EaseTime); GameObject.Find ("SliderHB").GetComponent<UISlider>().sliderValue = Mathf.Lerp(((float)OldCurHealth),HealthFloat, EaseTime); } IEnumerator EaseBAR() { EaseTime = 0; //shield ShieldFloat = ((float)CurShield)/((float)Maxshield); //health HealthFloat = ((float)CurHealth)/((float)Maxhealth); //ease test = Mathf.Lerp(1.0f,3.0f, Mathf.SmoothStep (0.0f,1.0f,EaseTime));// bug here: test stay to 0 GameObject.Find ("SliderSB").GetComponent<UISlider>().sliderValue = Mathf.Lerp(((float)OldCurShield),ShieldFloat, EaseTime); GameObject.Find ("SliderHB").GetComponent<UISlider>().sliderValue = Mathf.Lerp(((float)OldCurHealth),HealthFloat, EaseTime); yield return new WaitForSeconds(EaseTime); } } im not doing code since longtime so im pretty bad at this, but i dont like to stuck on my problems:-?
It looks like in the code bellow EaseTime is always 0. Try to change it to something more, for example 0.1f Code (csharp): IEnumerator EaseBAR() { EaseTime = 0; //shield ShieldFloat = ((float)CurShield)/((float)Maxshield); //health HealthFloat = ((float)CurHealth)/((float)Maxhealth); //ease test = Mathf.Lerp(1.0f,3.0f, Mathf.SmoothStep (0.0f,1.0f,EaseTime));// bug here: test stay to 0 GameObject.Find ("SliderSB").GetComponent<UISlider>().sliderValue = Mathf.Lerp(((float)OldCurShield),ShieldFloat, EaseTime); GameObject.Find ("SliderHB").GetComponent<UISlider>().sliderValue = Mathf.Lerp(((float)OldCurHealth),HealthFloat, EaseTime); yield return new WaitForSeconds(EaseTime); }
i changed the value but it doesnt work too, there is something that dont ease my float, i cant find where
Ok, the simpliest test. It should works well: Code (csharp): IEnumerator EaseBAR() { EaseTime = 0.1F; //ease test = Mathf.Lerp([B]test[/B], 3.0f, EaseTime);// bug here: test stay to 0 yield return new WaitForSeconds(EaseTime); } Mathf.Lerp have to recieve 3 variables: initialValue, targetValue and currentPosition (progress). You can use them in 2 ways: 1) test = Mathf.Lerp(test, 3.0f, EaseTime); in this case EaseTime can be unchangable variable, because initialValue always increase (it is "test" variable) 2) test = Mathf.Lerp(0.0f, 3.0f, EaseTime); in this case initialValue and targetValue is constant, and EaseTime must increase from 0.0f to 1.0f (EaseTime can not be unchangable).
thank you for the answer, the first case worked but i cant use it on my case because: my function work like this each time receive damages, I move my float from OldCurHealth to HealthFloat during Easetime seconds maybe its an other function ? i tried lerp and step but cant move it the way i want for exemple something like Code (csharp): myfloat = Easefunction(0,1,5 )//this will move my float from 0 to 1 in 5 seconds Code (csharp): IEnumerator EaseBAR() { //health HealthFloat = ((float)CurHealth)/((float)Maxhealth); //ease GameObject.Find ("SliderHB").GetComponent<UISlider>().sliderValue = Mathf.Lerp(((float)OldCurHealth),HealthFloat, EaseTime); //need to move the float "SliderValue" from "OCurHealth" to "HealthFloat" in xxseconds(Easetime) yield return new WaitForSeconds(EaseTime); }
I know it's old, but I'm just adding this here for everyone's reference. The couroutine is written wrong. It should be StartCoroutine(EaseBAR()); not StartCoroutine("EaseBAR");
In respect of starting the Coroutine both are identical, the only difference is in the available method you'd use to stop them. This seems to have been a bit of a pointless necro.