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 Jotakuun · May 07, 2014 at 05:52 PM · enemyfollowhealthbarhealth

How can I do health bar follows enemy?

Hi! I'm almost started to learning javascript and I'm doing exercises to get understanding of scripting. I'm trying to make a health bar following an enemy AI, but I don't know how to do it.

I'm not looking for someone how make the script for me, I just want someone try explain me how to do it because I tried so many times and I failed.

 static var maxHealth : float = 100 ;
 static var currentHealth : float;
 var dmg : float;
 
 var maxHealthtex : Texture2D;
 var currentHealthtex : Texture2D;
 
 var hpBarLenght : float;
 var percentOfHp : float;
 
 ...
 
 function Update () {
 percentOfHp = currentHealth/maxHealth;
 hpBarLenght = percentOfHp * 100;
 }
    
 function OnGUI() {
 if (currentHealth > 0) {
 GUI.DrawTexture(Rect(10, 10, hpBarLenght, 20), currentHealthtex);
 }
 }

I don't know how can I make GUI coordinates follows enemy.

Some suggestions?

Comment
Add comment · Show 2
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 Lo0NuhtiK · May 07, 2014 at 06:03 PM 0
Share

Look for Bergzerg Arcade on youtube (I "think" Bergzerg was the guys name on there, been a few years since I watched it) .. He had a Hack'N'Slash tutorial on his youtube and I remember learning how to do this from him when I first started using unity.

avatar image gdubrocks · May 07, 2014 at 06:32 PM 0
Share

https://www.youtube.com/watch?v=YYqzz1dy3Ak

1 Reply

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

Answer by Dazdingon · May 07, 2014 at 06:38 PM

Use : Camera.WorldToScreenPoint()

Get the on-screen position from the main camera


    var targetObject : Transform;
    var onScreenPosition : Vector3;
    onScreenPosition = Camera.main.WorldToScreenPoint(targetObject.position);

Then display the health bar above the targetObject(the enemy)

var displayRect : Rect; displayRect = Rect(onScreenPosition.x - 50, Screen.height-(onScreenPosition.y + 45), 100, 20);

 GUI.DrawTexture(displayRect, maxHealthtex);
 displayRect.width = hpBarLenght;
 GUI.DrawTexture(displayRect, currentHealthtex);

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 Jotakuun · May 08, 2014 at 11:28 AM 0
Share

This works! Thanks you! :)

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

23 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

Related Questions

Healthbar above enemy : 2d 2 Answers

How to create Enemy Health Bar ? 4 Answers

Instantiate with healthbar dont work correctly 0 Answers

Health below zero but shows negative numbers 1 Answer

Unity health problem and enemy colision 3 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