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 primus88 · Jun 05, 2013 at 11:21 AM · followhealthbarfloating

Yet another health bar request

Hello everyone,

For the last month I searched and tried to implement a health bar in my game. I had no luck. Even though there are a million tutorials out there, I just didn't found the correct one. Trust me, I tried about 10-15 code variants.

I need this :

Floating health bar (a bar with color red for example, not a text showing a number) over each of my spawned units (follows the unit, is not set in an area of the screen). Would be nice to have the code in C#, if not I will try to adapt a JS code.

All I need is this magical piece of code which eludes me for so long. I even bought NGUIText and Health Bar scripts from the Asset Store. No luck as well..

Comment
Add comment · Show 1
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 primus88 · Jun 05, 2013 at 01:54 PM 0
Share

Thanks everyone for the help. Got some nice tips and feedback here, even more concerning the fact that my question could've make me look like a lazy bum.

In case anyone stumbles like me I will provide the code that help me out as soon as I am back home !

2 Replies

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

Answer by Scribe · Jun 05, 2013 at 12:59 PM

This is not a place to ask for code to be written for you.

You should try and code something yourself but this is how I would go about it:

  1. Either you need to make an Array/List of all your 'units' or you should instantiate each unit with the same script attached to it.

  2. Once you have an object that needs a health bar, find its position (or a point above its position) in screen space using Camera.WorldToScreenPoint.

  3. Once you have a point in screen space you can draw a rectangle using GUI.DrawTexture.

  4. Scale the Texture based on the health of the unit

You should probably learn to code this if you wish to make a successful game,

A much better use of this site is to try and code it yourself and ask specific questions on sections of code

Scribe

EDIT: Attempt at code

 int maxHealth;
 int currentHealth;
 Vector3 ScreenPos;
 Texture2D MyTex;
 int MaxTextureWidth;
 
 
 void Update () {
     ScreenPos = Camera.main.WorldToScreenPoint(transform.position+Vector3.up);
 }
 
 void OnGUI () {
     GUI.DrawTexture(Rect(ScreenPos.x-((MaxTextureWidth/2.0)*((float)currentHealth/(float)maxHealth)), ScreenPos.y, MaxTextureWidth*((float)currentHealth/(float)maxHealth), 20), MyTex)
 }

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 primus88 · Jun 05, 2013 at 01:03 PM 0
Share

I agree with you that begging for code is not a very nice thing from my part, but I assure you I tried learning and search lots of references (I even used the google custom engine to search for the specific health bar articles in forum and in answers).

Frankly I know how to code all the elements you just pointed out, the problem is binding them together. I failed at this every time.

Is it possible to provide some pseudo code to see how to bind them, what functions to use etc ?

avatar image Scribe · Jun 05, 2013 at 01:24 PM 0
Share

some probably very incorrect code but maybe it will help:


int maxHealth;

int currentHealth;

Vector3 ScreenPos;

Texture2D $$anonymous$$yTex;

int $$anonymous$$axTextureWidth;

void Update () {

ScreenPos = Camera.main.WorldToScreenPoint(transform.position+Vector3.up);

}

void OnGUI () {

GUI.DrawTexture(Rect(ScreenPos.x-(($$anonymous$$axTextureWidth/2.0)*((float)currentHealth/(float)maxHealth)), ScreenPos.y, $$anonymous$$axTextureWidth*((float)currentHealth/(float)maxHealth), 20), $$anonymous$$yTex)

}

I know UnityScript better than C# and that isn't at all tested so there are probably lots of errors, but have a go at implementing something similar

Scribe

EDIT

formatting seems to have failed so check my edited answer!

avatar image
1

Answer by akstntn · Jun 05, 2013 at 01:36 PM

You have NGUI.. so I recommend you this :

http://www.youtube.com/watch?v=T9Q4mhGgDmg

http://www.youtube.com/watch?v=VONzxuzhGN8

http://www.youtube.com/watch?v=NC-jiZ5hdD8

good luck.

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

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Screen.width ~ HealthBar problem - C# 2 Answers

Smooth tail followscript 0 Answers

Need help with enemy AI 1 Answer

health regeneration after death 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