- Home /
How to show the change in slider value
I have a boss in my game with a slider as a health bar, and I what I want to have happen is when the player hits the boss, it shows the amount of damage (how much the slider value decreased) that has been done. For example, if the boss is at 45% health and the player hits him and does 15% damage, I want to show that value next to the boss health bar.
You can create 2 health slider images (one over another)....when the boss gets hit, immediately set the image on top (which is visible at all times)...to the new health value, but use a coroutine to wait for some time (maybe 1 second) before you lerp the second image to the real health value, this will create a really nice effect, but make sure to set it up so that if the boss is hurt again, both the top image, and the hidden damage indicator, are first immediately set to the current (before damage) value of health...and then the top layer is set immediately to the new damage value to create a convincing effect...if you need any clarification or help, tell me...hope this atleast points you in the right direction
@rage_co Thanks for the answer, this seems really cool, but I'm not sure how to do it. I was more thinking of showing the amount of damage done using a float that is displayed next to the healthbar, like in Bloodborne or Dark Souls.
hmm, can you share the damage taking bit of your code?
Answer by rage_co · Aug 08, 2021 at 05:30 AM
Well in that case it is really easy....First you need to create a text gameobject and place it close to the healthbar, (where you want to display the damage) make sure it's scaled properly to allow for multiple digits (you can customise it for color or anything, or even use Textmeshpro which is a free unity asset to create more customisable text)...these steps are very subjective and you will have to setup this yourself....
after that add this directive at the top of your script..
//if you are using the built in text, which i suppose you already have since you are using sliders with images
using UnityEngine.UI;
//if you are using TextMeshPro
using TMPro;
then create a reference to your text or textmeshpro in the script
//default
public Text deltaDamage;
//TextMeshPro
public TextMeshPro deltaDamage;
after that you can simply do this...
if(other.collider.tag == "Sword" )
{
if (PlayerAnimator.GetCurrentAnimatorStateInfo(0).IsName("hit"))
{
hit += 1;
float damage = 7f;
swordImpact.Play();
anim.SetBool("damage", true);
slider.value -= damage;
deltaDamage.text = damage.ToString();
ParticleSystem.Emit(100);
}
}
make sure to set the text to nothing in the start method...
void Start()
{
deltaDamage.text = "";
}
and it's that simple.... hope this helps
@rage_co Thanks! I didn't know it was that simple!
Your answer
Follow this Question
Related Questions
How to connect multiple sliders for health bar? 3 Answers
Script updates prefab but not instantiated clone of prefab 0 Answers
UI Slider value gets stucked in loop and Doesn't increase further 0 Answers
Triple Slider control - UI slider value control 1 Answer
Can't drag slider onto inspector 1 Answer