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 Rgalaxy · Jan 05, 2014 at 09:27 AM · javascriptlogichealthbar

Healt Bar Logic

so i try to make a health bar, i use GUITexture to create the bar, the width is 330 (for some reason it HAS to be like that)..

now i try to achieve how the bar could decrease on every attack that it receive.. and it's done!

but the problem is i couldn't convert the 330 into 100% bar length..

my code for attack is like this..

 function Attack(){
     var percenthp : float;
     var tekstur : GUITexture;
     var enemyhpdisplay;
     var hplength ;
     
     enemy_hp -= player_atk;         //player_atk is 5 and declared in other place, so as enemyhp
     //print(enemy_hp);
     percenthp = (enemy_hp/enemy_maxhp)*100;
 
     tekstur= GameObject.Find("enemyHp").GetComponent(GUITexture);
     tekstur.guiTexture.pixelInset.width= percenthp;    
         
 }

so we know my healthbar width is 330, but everytime i click the attack the bar just drom from 330 width, into 95 width,.. i couldn't know how to convert it, so the width will decrease by percentage of the length.. could anybody help me ?

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 sath · Jan 05, 2014 at 09:36 AM 0
Share

Have you tried a search before asking ?

http://answers.unity3d.com/questions/314754/guitexture-width-change.html

avatar image Rgalaxy · Jan 05, 2014 at 09:48 AM 0
Share

@sath i've tried it, but still couldn't achieve it..

i mean, i put the calculation on function Attack() which i call on function OnGUI()

does i need to initialize it on function Start()?

sorry, still not too good on algorithm.. x.x

1 Reply

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

Answer by sath · Jan 05, 2014 at 11:01 AM

Is this more helpful ?

  #pragma strict
 
 // drag the GUITexture from the Hierarchy view to this field in the Inspector:
 var tekstur: GUITexture;
 
 var enemy_hp : int = 100;
 
 private var width330: float; // the initial healthBar width
 
 function Start () {
     width330 = tekstur.pixelInset.width;
 }
 
 function Update () {
 
     if(Input.GetMouseButtonDown(0)){//you can Attack in OnGUI if already have a button
         Attack();                    
     }
 
     // set the health bar width:
     tekstur.pixelInset.width = width330 * enemy_hp / 100;
     
     if (enemy_hp <= 0){ // enemy has died?
         Destroy(gameObject);
     } 
 }
 
 
 
 function Attack(){
 
     enemy_hp -= 5;//player_atk;   
 }


Also there are several ways to make a health bar (like GUI.DrawTexture)

you can watch this tutorial if you want to learn more (and have patience)

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 Rgalaxy · Jan 05, 2014 at 12:58 PM 0
Share

wow, cool it works ! oh my why i'm so blind with this simple math problem.. thanks @sath ! Thanks for the tutorial from youtube too, i've been watching it for about 5-8 times.. i can make it decrease just couldn't change the 330 width to become 100 on real game.. :/

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

18 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

NullReferenceException: Object reference not set to an instance of an object... 0 Answers

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

How Do I Make A Health Bar 6 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