- Home /
regenerating health script
hi. would anyone know where i could find a regenerating health script. because i really need one for my game. and if there is one where could i find one that shows a 'hit' screen until your health is back up? thanks in advance
soz i dont think this is the place to ask for scripts. its more the place to ask questions about scripts your making. that way you will get heaps better at your game making. if you give it a go i would start reading this http://unity3d.com/support/documentation/ScriptReference/Time-fixedTime.html
Answer by syclamoth · Oct 16, 2011 at 07:08 AM
I don't usually like writing people's code for them, but I don't have anything to do for the next 5 minutes, so here we go...
public float maxHealth = 100; public float regeneration = 5; float health = 100; // Decrease this in your 'damage' script- I won't go into that here
void Start() { health = maxHealth; }
void Update() { if(health < maxHealth) { health += regeneration * Time.deltaTime; if(health > maxHealth) { health = maxHealth; } } }
void OnGUI() { if(health < maxHealth) { Color tempColour = GUI.color; GUI.color = Color.red; GUI.Label(new Rect((Screen.width / 2) - 70, (Screen.height / 2) - 20, 140, 40), "OH NOE THE PAIN"); GUI.color = tempColour; } }
There you go! I can make a variant which fades the message out depending on how much health you have, but that's a different matter. You may want to define a custom GUIStyle which has bigger writing, and use that instead, but that's not entirely a scripting matter, so I can't really put it here.
EDIT: That bit about not liking to write people's code was a lie.
when i try to put it in it has over ten compiler issues.
And you don't just copypasta the entire script- it's supposed to go inside a class!
they got a point though. I think you should read this. It's about asking questions here.
http://answers.unity3d.com/questions/133869/how-to-ask-a-good-question.html
Your answer
Follow this Question
Related Questions
Health Regeneration speeds up when action performed? 1 Answer
Regain health on GUI 2 Answers
C# Health regeneration delay after hit 0 Answers
How can i make my health regenerate? 1 Answer
Help With Health Regeneration 1 Answer