- Home /
How To Make Player Health Increase if in a Certain Animation State
Greetings,
I am trying to modify my healthbar script so that the player will gain health over time if they are in a certain animation state. If they are not in that animations state the player will lose health over time . I have set it up so that the player constantly gains health over time and loses it on button click for testing. If anyone could give tips on how to modify this script that would be greatly appreciated.
Thanks in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthController : MonoBehaviour
{
public UnityEngine.Events.UnityEvent OnDeath;
[SerializeField] public float playerHealth;
[SerializeField] public float maxHealth;
[SerializeField] public Image healthImage;
[SerializeField] public int damage;
[SerializeField] public int Healthtaken;
//health constantly being added per second
IEnumerator Start()
{
while (true)
{
yield return new WaitForSeconds(.25f);
playerHealth += Healthtaken;
UpdateHealth();
}
}
//damage taken on button click
public void ButtonClick()
{
playerHealth -= damage;
UpdateHealth();
if(playerHealth < 0)
OnDeath.Invoke();
}
//updating healthbar image
private void UpdateHealth()
{
healthImage.fillAmount = playerHealth / maxHealth;
}
}
Answer by GDGames0302 · Feb 14, 2021 at 11:42 AM
Hi. Check this link(Unity Documentation): https://docs.unity3d.com/ScriptReference/Animator.GetCurrentAnimatorStateInfo.html
Thank you @GDCGames0302 that was a great help. I was able to come up with this script from it. Now I am just struggling to connect it to my healthbar code... will update once I figure out how to call that function from this code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationHealth : $$anonymous$$onoBehaviour
{
Animator m_Animator;
void Start()
{
//Get the Animator, which you attach to the GameObject you intend to animate.
m_Animator = gameObject.GetComponent<Animator>();
}
void Update()
{
//When entering the "Typing" state in the Animator, output the message in the console
if (m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Typing"))
{
Debug.Log("Typing");
}
}
}
Hi. In the AnimationHealth class, do the following(don't forget to assign the object that has the HealthAnimator script attached, to the empty slot):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationHealth : $$anonymous$$onoBehaviour
{
Animator m_Animator;
public HealthController healthController;
void Start()
{
//Get the Animator, which you attach to the GameObject you intend to animate.
m_Animator = gameObject.GetComponent<Animator>();
}
void Update()
{
//When entering the "Typing" state in the Animator, output the message in the console
if (m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Typing"))
{
healthController.playerHealth += Time.deltaTime * what you want;
}
}
}
Or, you can do this in a simpler way: make the playerHealth variable static, and you can access it directly:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationHealth : $$anonymous$$onoBehaviour
{
Animator m_Animator;
void Start()
{
//Get the Animator, which you attach to the GameObject you intend to animate.
m_Animator = gameObject.GetComponent<Animator>();
}
void Update()
{
//When entering the "Typing" state in the Animator, output the message in the console
if (m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Typing"))
{
HealthController.playerHealth += Time.deltaTime * what you want;
}
}
}
Avoid static properties!! Static classes and properties are extremely bad for scalability. Only stateless functions should use static!
Thanks for the answers ! $$anonymous$$y objects are at fixed locations so I ended up using box triggers to drive the change in health. If I get your animation state system working will update the post but your answer looks solid!
Your answer
Follow this Question
Related Questions
Player health dropping far to fast 1 Answer
how do u make diffrent chacters and give them commands 2 do things 2 Answers
Health bar goes down instantly. 1 Answer
How to make my health decrease? 1 Answer
Health Code, Damage and Healing 0 Answers