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 Skalde · May 06, 2013 at 06:48 PM · enemybarhealth

Enemy healthbar script

Hello Unity Community, i've been lookin all over the web for an enemy healthbar that are not a GUI Texture or is only shown near the enemy.

the Healthbar should be above the enemy head within the game or placed on the screen as a GUI when near the enemy

Any suggestions on how? or where i might find a tutorial?

Heres the Enemy Health Javascript:

 #pragma strict
 
 var Health = 100;
 var Enemy : GameObject;
 var Die : AudioClip;
 //var DieAnimation : AnimationClip;
 
 function Update ()
 {
     if(Health <= 0)
     {
         Dead();
     }
 }
 
 function ApplyDammage (TheDammage : int)
 {
     Health -= TheDammage;
 }
 
 function Dead()
 {
     //animation.Play("DieAnimation");
     audio.PlayOneShot(Die);
     Destroy (gameObject, 1);
 }

Thanks in advance

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

2 Replies

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

Answer by Skalde · May 07, 2013 at 04:46 PM

I found an even more simple solution by using a plane like this:

     #pragma strict
     
     var HealthBar : Transform;
     private var MaxHealth : float = 100;
     private var MaxBar : float = 0.2;
     var Health = 100;
     var Enemy : GameObject;
     var Die : AudioClip;
     var EnemyDoll : Transform;
     //var DieAnimation : AnimationClip;
      
     function Update()
     {
     // --- Change Size Of Bar --- \\
     transform.localScale.z = (MaxBar) * (Health/MaxHealth);
      
     // --- Change Colour Of Bar --- \\
     if(transform.localScale.z == 0.5)
     {
     transform.renderer.material.color = Color.green;
     }
     if(transform.localScale.z <= 0.35)
     {
     transform.renderer.material.color = Color.yellow;
     }
     if(transform.localScale.z <= 0.1)
     {
     transform.renderer.material.color = Color.red;
     }
     if(Health <= 0)
     {
     Dead();
     }
 }
 function ApplyDammage (TheDammage : int)
 {
     Health -= TheDammage;
 }
 
 function Dead()
 {
     //animation.Play("DieAnimation");
     audio.PlayOneShot(Die);
     EnemyDoll.animation.Play("Enemy die");
     Destroy (Enemy, 1);
 }

But thanks anyways ^^

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
avatar image
0

Answer by SubatomicHero · May 07, 2013 at 12:20 PM

Would it make sense to make the GUI Bar you have as a child of the enemy in the hierarchy pane?

Position it where you want, then wherever the enemy moves, the bar will follow.

Comment
Add comment · Show 1 · 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 Skalde · May 07, 2013 at 02:40 PM 0
Share

Its not possible to get the GUI to follow the enemy just by parentin it.. thats why i need a good example of a script that makes it possible, but thanks for your answer :)

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

13 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

Related Questions

Multiple Cars not working 1 Answer

How can an enemy deplete players health? 0 Answers

Enemy Health Bar 1 Answer

[Solved] Enemy Won't Affect Player Health? 0 Answers

I made my code so that if i hit an enemy its health will go down but when i hit the enemy the health doesnt go down. please help 1 Answer


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