• 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 TPAcompany · Nov 24, 2012 at 02:44 PM · healthbar

How to make Health bar ?

Hello ! I need Health bar for my game ! The picture bellow is the health bar ! How to make it decline when enemy hit ??? Please help !

219392861.|1=Month=November%202012=1alt text219392861.|1=Month=November%202012=1

health bar.jpg (6.4 kB)
Comment
Add comment · Show 4
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 whydoidoit · Nov 24, 2012 at 02:47 PM 0
Share

You really need to do some tutorials which will help you with many of the questions you are posing. This website is for specific detailed technical questions - please google for Unity Tutorials and do some of those first.

avatar image TPAcompany · Nov 24, 2012 at 02:58 PM 0
Share

im watching and posting !

avatar image landon912 · Nov 24, 2012 at 04:22 PM 0
Share

^^Thats good and all but first learn how to detect if the player is hit(there are tons of tuts on that), then if you dont know how to do the health bar(after looking for tuts) then ask.

avatar image TPAcompany · Nov 25, 2012 at 04:14 AM 0
Share

ok ! Thank you

3 Replies

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

Answer by Iceblitzyt · Nov 24, 2012 at 08:29 PM

This is what I'm used for a while before i made my GUI more dynamic(it's a c# script). It's a Modified script of the BurgZerg Arcade. Although you can find his good tutorials here: http://www.burgzergarcade.com/hack-slash-rpg-unity3d-game-engine-tutorial

First though you need to have 2 images, one of the gui like that and one separate bar for the health.

Here's the code:

 using UnityEngine;
 using System.Collections;
 
 public class health : MonoBehaviour {
         public GUIStyle PlayercurhealthplaceH;
     public GUIStyle Playercurhealthtext;
         public int MaxHealth = 320;
     public int CurHealth = 320;
     public float HealthBarLength;
 
     
         void start(){
         
     HealthBarLength = 200;    
         
     }
     
     void OnGUI(){
         //Heath bar
           
         //This is where you put the background image
          GUI.Box(new Rect((Screen.width /2) -339,(Screen.height /2) +300,200,20),"", PlayercurhealthplaceH);    
         //This is where you put the foreground image, the green bar by itself.
         GUI.Box(new Rect((Screen.width /2) -339,(Screen.height /2) +300,200/(MaxHealth/CurHealth),20), CurHealth + "/" + MaxHealth, Playercurhealthtext);
           
     }
     
         public void AdjustCurrentHealth(int Adj){
     
         CurHealth += Adj;
         
         if (CurHealth <0)
             CurHealth = 0;
         if(CurHealth > MaxHealth)
             CurHealth = MaxHealth;
         if(MaxHealth < 1)
             MaxHealth = 1;
             
         HealthBarLength = (200) * (CurHealth/(float)MaxHealth);
         
         
         
     
     }
     
     
     public void update() {
     
          AdjustCurrentHealth(0);
         
     
     
 }
 

Now you'll be expected to know how to use the inspector to add the images ect yourself. I hope this helped. If your still confused, keep your image, watch burgzerg's videos and eventually when your knowledge of coding goes up, he also goes into detail on how to add GUI, but practice coding first!

Comment
Add comment · Show 2 · 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 MultiDGaming · Nov 29, 2012 at 05:05 PM 0
Share

Iceblitzyt this looks like a great script but when i try to run it it gives a compiler error, can you help me out? thanks

avatar image Iceblitzyt · Dec 08, 2012 at 10:33 PM 0
Share

What error do you get? Is your script spelled the same as it is declared in the public class?

avatar image
0

Answer by behzad.robot · Nov 24, 2012 at 08:28 PM

Well About The Enemy Hitting You I agree with landon91235 go and watch some Tuts First But if i think i can give you a good advice about the health bar shrinking : Instead of using many pictures which is madness for sure(Creating one when the health is high ,a little bit less than high,a little more ,....)You Can Make it go Back I mean if you are using a GUITexture for example you can Makeit Go Back guiTexture.pixelInset = Rect (50, 50, 100, 100); something like guiTextire.pixelInset = Rect(--X,Y,100,100); Take a look at documentation about GUITexture I think Your Healthbar Should contain a GUITexture Containing Everything except that green thing and Another GUI TEXTURE containing that green thing showing the health well that's all i can say for you not having any specefic codes! Yes go and watch some tuts as landon91235 said!

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 difd 9999 · Nov 24, 2012 at 08:28 PM

I am still learning unity to but I can point you to the tornado twins YouTube vids it is Really helpful and will teach you how to make a health bar

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 difd 9999 · Nov 24, 2012 at 05:44 PM 0
Share

Ps. You need more than one picture

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

enemy set to be prefab 0 Answers

Scrollbar Thumb Scaling 0 Answers

Help With Enemy Health bars 3 Answers

Healthbar Ratio Question 1 Answer

How do I make overlapping Health Bars? 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