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 SirMcsquizy · May 15, 2014 at 03:55 PM · c#guiaihealthbar

How to put a health bar above enemy?C#

Hi there!

So, I must feel really stupid or something because I cant seem to figure this out, and I even looked it up.

Basically, I would like a red bar above the enemy that would go down when ever it gets hit. If someone could like... point me in the right direction or help me, that would be great.

Again sorry if I sound like a complete idiot. GUI is not my thing.

-Squizy

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
1

Answer by morgan23 · May 15, 2014 at 04:08 PM

BugzergArcade's vids tutorial's he does what your asking.

Comment
Add comment · Show 4 · 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 SirMcsquizy · May 15, 2014 at 04:14 PM 0
Share

@morgan23

Thank you so much! I completely forgot about these tutorials! :D

avatar image morgan23 · May 15, 2014 at 04:26 PM 0
Share

np use the ngui one they would be better too

avatar image SirMcsquizy · May 15, 2014 at 04:38 PM 0
Share

@morgan23

I would if I had the money....

avatar image morgan23 · May 15, 2014 at 05:21 PM 0
Share

thiers a free version I use it because I don't really like scripting in the ongui method.

avatar image
0

Answer by FlaflaTwo · May 15, 2014 at 05:08 PM

One way to do it would be using `Transform.InverseTransformPoint(Vector3)` to transform a world point (i.e., the point in which you want the health bar to "float" on) to screen coordinates. Then you would use Unity's GUI features to draw a health texture for the bar, labels, etc. Here is an example:

 public class Bar : MonoBehaviour {
     public Transform barWorldCoords;
     public Texture barBackground;
     public Texture barForeground;
     public float health = 1.0f; // From 0.0 (no health) to 1.0 (full health)
 
     void OnGUI() {
         Vector3 screenPoint = Transform.InverseTransformPoint(barWorldCoords.position);

         // Draw Background
         GUI.DrawTexture(new Rect(screenPoint.x-100, screenPoint.y-20, 200, 40), 
                         barBackground, ScaleMode.StretchToFill, true);
         // Draw Foreground
         GUI.DrawTexture(new Rect(screenPoint.x-100, screenPoint.y-20, 200F*health, 40),
                         barForeground, ScaleMode.StretchToFill, true, 
                         ((barForeground.width*health)/barForeground.height));
     }
 
 }

This isn't tested, so it might have bugs, but you should get the general idea from it.

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 SirMcsquizy · May 16, 2014 at 04:05 PM 0
Share

@FlaflaTwo

I get a An object reference is required to access a non static member for the InverseTransformPoint

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Creating A GUI health bar 0 Answers

Drawing 4 GUIButton's with a forloop. Need help changing color? 1 Answer

Return to origional position. 2 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