- Home /
Question by
emersonmaxwell · Jan 29, 2021 at 10:21 AM ·
animationanimatortime.deltatimeanimation controllerhealthbar
How would one make a Health Bar decrease if a player is in a specific animation state?
I've found many great tutorials on coding health bars but am specifically looking for how to connect a decreasing health bar to an animation state. Basically so that if my player character is in the "working" animation state, their stress meter would rise of fall overtime (using deltatime).
Thanks in advance.
Comment
Answer by logicandchaos · Jan 29, 2021 at 12:06 PM
if( anim_Animator.GetCurrentAnimatorStateInfo(0).IsName("AnimationName"))
health--;
Hi again @logicandchaos just came back to this after a while. I'm trying to modify your answer so that the player is constantly losing health and gains health over time if in a specific animation state. This is the where my code is at, I know it's very wrong but hopefully you can see what I'm trying to do.
Thanks in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthController : $$anonymous$$onoBehaviour
{
[SerializeField] public float playerHealth2;
[SerializeField] public float maximumhealth;
[SerializeField] public Image HealthImage2;
[SerializeField] public int health;
[SerializeField] public int dmg;
[SerializeField] private Animator Stickman;
IEnumerator Start()
{
//health lost over time
while (true)
{
yield return new WaitForSeconds(.25f);
playerHealth -= dmg;
UpdateHealth();
}
}
//health gained if in specific animation state
private update
{
if(anim_stickman.GetCurrentAnimatorStateInfo(0).IsName("Typing"));
{
yield return new WaitForSeconds(.25f);
playerHealth += health;
UpdateHealth();
}
}
//update healthbar image UI
private void UpdateHealth()
{
healthImage2.fillAmount = playerHealth2 / maximumHealth;
}
}