Using DOTween on a slider

Hi everybody!

So, since yesterday I try to make the health bar of my player going down smoothly (I have use a simple slider to do the health bar). Right now, when I take damage, the slider go directly to the new value of the health and I would like to see it gradually going from the life before the hit to the new health value.

I have look on internet to find a solution, and I have find the asset “DOTween”, from what people where saying, it’s seem to work. I have find it thanks to this post:
https://www.reddit.com/r/Unity2D/comments/4d2wk2/smoothly_changing_hp_bar/?st=j2m1idu3&sh=0474708a

So I have installed DOTween, I have changed my script to use it, here is my script right now:

//---PlayerHealth Script---\\
//---Author:MrPyxel---\\
//---Version:1.5---\\
//---Update:13/05/2017 01:30---\\


//-----Libraries-----\\
using System.Collections;
using UnityStandardAssets.Characters.FirstPerson;
using UnityEngine.UI;
using DG.Tweening;
using UnityEngine;

//-----Script Body-----\\
public class PlayerHealth : MonoBehaviour
{
    //-----Private Variables-----\\
    private bool isDead;
    private FirstPersonController firstPersonController;

    //-----Public Variables-----\\
    public int startHealth = 100;
    public int currentHealth;
    public Slider healthSlider;
    public float slidingTime = 1.5f;

    //-----Private Functions-----\\
    private void Awake()
    {
        currentHealth = startHealth;
        firstPersonController = GetComponent<FirstPersonController>();
    }

    private void Death()
    {
        isDead = true;
        firstPersonController.enabled = false;
    }

    //-----Public Functions-----\\
    public void TakeDamage(int amount)
    {
        currentHealth -= amount;
        DOTween.To(() => healthSlider.value, x => healthSlider.value = x, currentHealth, slidingTime);

        if (currentHealth <= 0 && !isDead)
        {
            currentHealth = 0;
            Death();
        }
    }

}

And even with the DOTween.To, it still do the same thing. So maybe I am not using the “DOTween” asset in the good way, if somebody can help me, it will be really apreciated!

Thanks for reading! :wink:

Looks to me like that should work. DOTween does have a built in shortcut for UI Sliders:

DOValue(float to, float duration, bool snapping = false);

//Changes the target's value to the given one.
//snapping If TRUE values will smoothly snap to integers.

So you should be able to write it like this:

healthSlider.DOValue(currentHealth, slidingTime);

Also, if DOTween isn’t set to play tweens automatically, you need to call .Play() to run them.

Thanks for your awnser, I have try this but I have the same result, but I haven’t the .Play() anywhere and I don’t understand where I have to place it, in the Start function?

I think by default, DOTween is set to Autoplay, so you shouldn’t need to add it. But if you’ve turned autoplay off, then you need to chain the Play() method like this:

healthSlider.DOValue(currentHealth, slidingTime).Play();

What isn’t working? Is the value just snapping, or is it not changing at all?

1 Like

Oh! I see the problem. “CurrentHealth” needs to be a float, not an int and you need to supply a float to your TakeDamage function, rather than an int:

  public float currentHealth;
public void TakeDamage(float amount)
    {
        currentHealth -= amount;
        DOTween.To(() => healthSlider.value, x => healthSlider.value = x, currentHealth, slidingTime);
        if (currentHealth <= 0 && !isDead)
        {
            currentHealth = 0;
            Death();
        }
    }
1 Like

I have changed the type of amount, startHealth and currentHealth to float, but the slider still jump from on value to the other.
Maybe it is my slider that is badly configured, this is the slider’s inspector:
3068966--230742--upload_2017-5-14_12-56-44.png

I maybe need to add a transition? I don’t really know, I begin with Unity.

I have made some test, and with DOTween.To if I try to move a simple float value simply I just past from the first float value to the second, it is quite strange. Here is the test I have made:

public class PlayerHealth : MonoBehaviour
{
    public float test1 = 100f;
    public float test2 = 50f;
    public float slidingTime = 1.5f;

    private void Update()
    {
        Debug.Log(test1);
    }

    public void TakeDamage(float amount)
    {
        DOTween.To(() => test1, x => test1 = x, test2, slidingTime);
    }
}

And in the console, test1 go from 100 to 50 in only on frame. Do somebody know why?

1 Like

That’s weird… maybe it got messed up during installation? If I recall, when you first install it, it’s supposed to pop up a window saying something about how it needs to check which version of Unity you’re using to install the correct files. Did you get that window? Maybe try deleting the DOTween folder and then adding it back in. Your code looks fine; I use DOTween and I’ve never had issues like this with it.

1 Like

Yes I have setup DOTween when I have import it. I will try to reinstall it, maybe it will work, I hope…

EDIT:
Shame on me…
Even if I was setting the slidingTime value at “1.5f” in my code, in the inspector it was set to 0… so the value was passing from one value to the other in 0 secondes… I feel so stupid right now. :face_with_spiral_eyes:

But still, I have learn one or two things thanks to this thread. :wink:
Thanks for your help guys!