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 Barnizzler · Nov 08, 2011 at 05:40 AM · guitexturepickuphealthbar

Slowly diminishing healthbar + objects that add to the healthbar.

Alright, Hello guys. I am trying to get my health bar to gradually decrease over time. I have got it so that when you click the mouse the health goes down, but how do I make it go down by itself? I'd also like to know how to go about a script to attach to a 'medkit' type object so that it adds say, 10hp back onto the health bar? So at the moment I am using a guitexture for the bar. Below is the code that makes it diminish with the mouseclick.

 var maximumHitPoints = 10.0;
 var hitPoints = 10.0;
 var healthGUI : GUITexture;
 var die : AudioClip;
 var buttonDown : boolean = false;
 //var resetPoints : float = 0.0;
 var damage : float = 0.0;
 
 private var healthGUIWidth = 0.0;
 
 function Awake () {
     healthGUIWidth = healthGUI.pixelInset.width;
 }
 
 function Update() {
     if (Input.GetButton("Fire1"))
     {
         damage = Time.deltaTime;
         ApplyDamage();
     }
 }
 
 function LateUpdate () {
     UpdateGUI();
 }
 
 function ApplyDamage () {
     hitPoints -= damage;
     if (hitPoints < 0.0)
         Die();
 
 }
 
 function Die () {
     if (die)
         AudioSource.PlayClipAtPoint(die, transform.position);
 }
 
 function UpdateGUI () {
 
     var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
     healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
 
 } 
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
0

Answer by syclamoth · Nov 08, 2011 at 05:48 AM

Well, if you just remove all the 'if(Input.Whatever)' bits from Update, it'll decrease health on its own (since you are no longer checking for whether the player is clicking or not). As for the medikit, just have something like this in your Update-

if(Input.GetButtonDown("Heal"))
{
    hitPoints += 10;
}

Or, however you want to do it.

If you need a 'pickup' for health, do this-

function OnTriggerEnter(other : Collider)
{
    if(other.gameObject.tag == "HealthPickup")
    {
        hitPoints += 10;
        Destroy(other.gameObject);
    }
}

You could set that up so that each individual pickup has its own health value, and when you touch it it heals you for that amount.

Comment
Add comment · Show 9 · 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 Barnizzler · Nov 12, 2011 at 12:47 AM 0
Share

Sorry, Its the exact same code as in the question, just without the ifbutton peice. The site automatically formats the code in the comment bit not how it originally looks.

var maximumHitPoints = 10.0; var hitPoints = 10.0; var healthGUI : GUITexture; var die : AudioClip; var buttonDown : boolean = false; //var resetPoints : float = 0.0; var damage : float = 0.0;

private var healthGUIWidth = 0.0;

function Awake () { healthGUIWidth = healthGUI.pixelInset.width; }

function Update() { if (Input.GetButton("Fire1")) { damage = Time.deltaTime; ApplyDamage();

 }

}

function LateUpdate () { UpdateGUI(); }

function ApplyDamage () { hitPoints -= damage; if (hitPoints < 0.0) Die();

}

function Die () { if (die) AudioSource.PlayClipAtPoint(die, transform.position); }

function UpdateGUI () {

 var healthFraction = $$anonymous$$athf.Clamp01(hitPoints / maximumHitPoints);
 healthGUI.pixelInset.x$$anonymous$$ax = healthGUI.pixelInset.x$$anonymous$$in + healthGUIWidth *   healthFraction;

}

avatar image syclamoth · Nov 12, 2011 at 02:31 AM 0
Share

Could you edit that comment so that it has spaces before each line? It's impossible to read as is.

avatar image Barnizzler · Nov 12, 2011 at 03:02 AM 0
Share

Just edited the post. This site munches it up tho, That's not how it's formatted in monodevelop.

avatar image syclamoth · Nov 12, 2011 at 04:24 AM 0
Share

Yeah, that's why you need to have four spaces before each line! The site munches it, sure, but it's possible to get it right if you're careful. Rule of thumb- copypasting from another editor almost never works here. I mean, of course that's not how it's formatted in $$anonymous$$onoDevelop, nobody could work that way.

avatar image Barnizzler · Nov 13, 2011 at 12:22 AM 0
Share

Alright, so I tried implementing function OnTriggerEnter(Collider other) { if(other.gameObject.tag == "HealthPickup") { hitPoints += 10; Destroy(other.gameObject); } }

But is does not seem to wanna compile. I put it in the same script that takes away the hitpoints right? Not a completely different script?

Show more comments
avatar image
0

Answer by Barnizzler · Nov 08, 2011 at 06:07 AM

Thank's so much syclamoth, works exactly how I wanted. With the medkit code you posted, that only increases the hitpoints if your pressing a button? I have an object in game which acts as a medkit and should increase the healthbar when a collision is detected.

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 syclamoth · Nov 08, 2011 at 06:09 AM 0
Share

You should post this as a comment, not as an answer.

Anyway, if you have collisions working, just put that bit inside of the OnCollisionEnter, or OnTriggerEnter, along with some code which checks for what kind of pickup you just tripped over.

avatar image Barnizzler · Nov 09, 2011 at 12:15 AM 0
Share

Sorry, this is my first time posting here. So, where and how would I put the OnCollisionEnter to check for the pickup? Excuse my lack of knowledge, I am just learning.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Healthpickup isn't working 1 Answer

Help With Enemy Health bars 3 Answers

Dynamic Health Bar in unity 4.6 0 Answers

Issue with GUITexture width. 1 Answer

Minion floating healthbar over the head 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