Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by emersonmaxwell · Feb 14, 2021 at 03:23 AM · if-statementshealthbaranimationstatehealth-deductionhealth

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;
     }
 
    
 
 }

 


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by GDGames0302 · Feb 14, 2021 at 11:42 AM

Hi. Check this link(Unity Documentation): https://docs.unity3d.com/ScriptReference/Animator.GetCurrentAnimatorStateInfo.html

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image emersonmaxwell · Feb 14, 2021 at 10:23 PM 0
Share

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");
         }
     }
 }

 
avatar image GDGames0302 · Feb 15, 2021 at 05:51 AM 1
Share

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;
          }
      }
  }
 


avatar image AbandonedCrypt GDGames0302 · Feb 15, 2021 at 09:15 AM 1
Share

Avoid static properties!! Static classes and properties are extremely bad for scalability. Only stateless functions should use static!

avatar image GDGames0302 AbandonedCrypt · Feb 15, 2021 at 09:33 AM 0
Share

Ok. Thank you for letting me know.

avatar image emersonmaxwell GDGames0302 · Feb 15, 2021 at 11:45 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

115 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges