- Home /
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;
}
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.
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;
}
Could you edit that comment so that it has spaces before each line? It's impossible to read as is.
Just edited the post. This site munches it up tho, That's not how it's formatted in monodevelop.
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.
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?
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.
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.
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
Follow this Question
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