- Home /
How to make health go down when objects collide
Here's my script
/Health Script/
var clockBG : Texture2D; var clockFG : Texture2D; var clockFGMaxWidth : float; //the starting width of the foreground bar var currentHealth:float;
function Start() { startTime = 10.0; clockFGMaxWidth = clockFG.width; currentHealth = 95.8; }
function Update () { if (Input.GetButtonDown("Fire1")) currentHealth -= 1; }
function OnGUI() { //this is the width that the foreground bar should be var newBarWidth:float = (currentHealth/100) * clockFGMaxWidth;
Debug.Log("current health " + currentHealth);
//a spacing variable to help us position the clock
var gap:int = 20;
GUI.BeginGroup(new Rect (Screen.width - clockBG.width - gap,
gap, clockBG.width, clockBG.height));
GUI.DrawTexture(Rect (0,0, clockBG.width, clockBG.height), clockBG);
GUI.BeginGroup(new Rect(5,6, newBarWidth, clockFG.height));
GUI.DrawTexture(Rect(0,0, clockFG.width, clockFG.height), clockFG);
GUI.EndGroup();
GUI.EndGroup();
}
I was wondering how do I make the health drop when an object collides with another, sort of like when two enemies hit, they attack each other. Let me know if I need to post any more information to help you answer the question.
You could use http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html to check for collisions and then adjust the health variable
Answer by Millionaiire · Apr 07, 2014 at 12:01 PM
I tried that and found nothing, does anyone have any other suggestions?
Your answer
![](https://koobas.hobune.stream/wayback/20220613140634im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
LoadLevel Health Question. 1 Answer
Health Script 1 Answer
Player Health drops fast, how to pause after damage 0 Answers
Setting Scroll View Width GUILayout 1 Answer
Die on collision doesnt work 2 Answers