Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 BankShotZombies · Oct 12, 2018 at 01:18 AM · updatehealthbarquickhealth-deductionhealth

Health bar goes down instantly.

Hello. I am trying to create a health bar, and when my enemy attacks, it goes down. But when I run it, once my mutant attacks, my health goes down instantly. I'm guessing it's because of Update, but when I added a boolean it did the same things. If someone can help that would be much appreciated. Please keep in mind that I am new to Unity. Here is my code:`using System.Collections; using UnityEngine; using UnityEngine.UI;

public class CharacterHealth : MonoBehaviour {

 public float CurrentHealth { get; set; }
 public float MaxHealth { get; set; }

 public Slider healthBar;

 public GameObject mutant;
 public bool hasMutantAttacked = false;

 void Start()
 {
     MaxHealth = 20f;
     // Resets health to full on game load  
     CurrentHealth = MaxHealth;

     healthBar.value = CalculateHealth();
 }

 void Update()
 {

     if(mutant.GetComponent<EnemyController>().MutantAudio.isPlaying && !hasMutantAttacked)
     {
         hasMutantAttacked = true;
     }

     if(hasMutantAttacked && mutant.GetComponent<EnemyController>().MutantAudio.isPlaying)
     {
         DealDamage(6);
         hasMutantAttacked = false;
     }

 }

 void DealDamage(float damageValue)
 {
     // Deduct the damage dealt from the character's health
     CurrentHealth -= damageValue;
     healthBar.value = CalculateHealth();
     // If the character is out of health, die!
     if (CurrentHealth <= 0)
         Die();
 }

 float CalculateHealth()
 {
     return CurrentHealth / MaxHealth;
 }

 void Die()
 {
     CurrentHealth = 0;
     Debug.Log("Dead");
 }

}`

Thanks!

Comment
Add comment · Show 3
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 FlightOfOne · Oct 12, 2018 at 01:37 AM 0
Share

The way you have it right now, as long as the audio is playing it will deal damage. Is this what you want?

If so keep in $$anonymous$$d that this operation happens very very fast, every frame. frame might only about 0.001 seconds depending on your computer. That's why it looks like it's going so fast, and it will look like as it is instant. try this: DealDamage(6 * Time.deltaTime); this will slow it down.

also an unrelated thing. Try to cache components ins$$anonymous$$d of getting component every frame. It's not good for performance. Then use that "enemyControler.$$anonymous$$utantAudio.isPlaying" ins$$anonymous$$d

 void Start()
 {
 EnemyController enemyController 
 =mutant.GetComponent<EnemyController>();
 }

avatar image BankShotZombies FlightOfOne · Oct 12, 2018 at 02:23 AM 0
Share

Thanks for the reply. I just want it to remove my health once when the enemy attacks once. I don't $$anonymous$$d how fast it goes down, as long as it goes down a certain amount. I really just want it to execute the line CurrentHealth -= damageValue; one time when the mutant attacks.

avatar image BankShotZombies FlightOfOne · Oct 12, 2018 at 02:45 AM 0
Share

Hello FlightCrazed. I was bored and was messing around in my script, and I actually came up with a script that got the effect I wanted. Sadly, I have no idea why it works. Can you explain to me why it works, and also maybe tell me any ways I could make my script better and get the same effect? Thanks. Also, I will make the EnemyController component later when I get the chance. Here is my new update method: void Update() { canDealDamage = !has$$anonymous$$utantAttacked; if(mutant.GetComponent<EnemyController>().$$anonymous$$utantAudio.isPlaying && !has$$anonymous$$utantAttacked) { has$$anonymous$$utantAttacked = true; } if(!mutant.GetComponent<EnemyController>().$$anonymous$$utantAudio.isPlaying) { has$$anonymous$$utantAttacked = false; } if(has$$anonymous$$utantAttacked == true && canDealDamage) { DealDamage(6); } }

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Zaeran · Oct 12, 2018 at 03:41 PM

Each update loop, you're setting hasMutantAttacked to true as long as its audio is playing, and then checking if it's true to apply damage. This means that whenever the audio is playing, it will apply damage every frame.

Ideally, you'd make DealDamage a public function, then have the mutant call the function in its attack code.

Comment
Add comment · 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

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

93 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

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

unity health bar not losing health 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