- Home /
Show enemy health on collision help.
Im Looking for a way to show a GUI Texture(the enemies health) upon entering a collider, then once the enemy is dead the GUITexture no longer shows.
I managed to achieve the first part on my own, but once I kill the enemy, the enemy is destroyed but the health bar is still showing on the screen.
Any help would be appreciated.
Answer by Joshua · Jun 20, 2011 at 01:32 PM
Oh god, you're using that horrible tornado twins script aren't you? Info about arrays can be found here. If you'd use array here it would look something like this:
var health : Texture2D[];
static var lives : int = 9;
var oldLives : int = 0 //so we know lives changed if lives != oldLives
function Update() {
if( lives != oldLives ){
guiTexture.texture = health[lives];
oldLives = lives;
if( lives = 0 ){
Destroy( gameObject.Find( "roboBearCannonMod" );
Destroy( gameObject ) //destroy the guiText gameObject.
}
}
}
significantly shorter, isn't it? ^.^
Though this has shortened the original health script, I still havent been able to make the health bars of the enemy appear when close to them...have you got any ideas for how to do this?
In your question you said you had already solved that part. Anyway, use a trigger for that.
Answer by aldonaletto · Jun 20, 2011 at 10:57 AM
Child the GUITexture to the enemy (drag it to the enemy object) - when an object is destroyed, all its children are destroyed too.
When I child the GUITexture, it not longer appears in the game.
Yes, I forgot this: position has a different meaning in GUIText and GUITexture - it's expressed in viewport coordinates, which range from 0 to 1. When childed to a non-GUI object, any movevent takes the GUI object out of view. Let me test an idea - I'll be back soon!
Does your GUITexture follow the enemy? If it does, you're probably calculating its position in the enemy's script. If it's the case, calculate the health bar position right after the enemy position is updated - the GUITextue may become out of view in the Editor when childed, but its position will be updated any time the enemy moves when playing.
Another alternative could be to use OnDestroy in the enemy's script:
var myHealth:GameObject;
function OnDestroy(){
Destroy(myHealth);
}
This works fine, but you must assign the GUITexture to myHealth in the Inspector. Once assigned, you can create a prefab, select the enemy and the health GUI and drag both to the prefab - but every time a new enemy prefab is instantianted you will have to adjust the health bar coordinates, since its position will be initially set to the enemy position.
@aldonaletto It's good practice to edit your main answer if you suggest a totally different approach in a comment then you did in the answer. Upvoted anyway for correctness ;)
Answer by Ben 39 · Jun 20, 2011 at 12:57 PM
This is the health code I use for the enemy..its attached to a GUITexture.
var health8 : Texture2D; //eight lives left
var health7 : Texture2D; //seven lives left
var health6 : Texture2D; //six lives left
var health5 : Texture2D; //five lives left
var health4 : Texture2D; //four lives left
var health3 : Texture2D; //three lives left
var health2 : Texture2D; //two lives left
var health1 : Texture2D; //one lives left
var health0 : Texture2D; //no lives left
static var LIVES1 = 9;
function Update ()
{
switch(LIVES1)
{
case 9:
guiTexture.texture = health8;
break;
case 8:
guiTexture.texture = health7;
break;
case 7:
guiTexture.texture = health6;
break;
case 6:
guiTexture.texture = health5;
break;
case 5:
guiTexture.texture = health4;
break;
case 4:
guiTexture.texture = health3;
break;
case 3:
guiTexture.texture = health2;
break;
case 2:
guiTexture.texture = health1;
break;
case 1:
guiTexture.texture = health0;
Destroy(gameObject.Find("roboBearCannonMod")); // Destroys Health Box
LIVES1 = 9;
break;
}
}
oh god, why dont you use an array? that would drastically reduce the amount of written lines, also you dont need to have a seperate variable for health count as it would be the array size :)
Never$$anonymous$$d, deleted to comment and posted an answer that also solved your main problem. In the future, please don't post answers to your own question like this, but rather edit your main question. It's easier for us to then find the info we need to help you, and it doesn't make it seem like the question is already answered. ;)