- Home /
Player Health Not Working?
Hi, I have a player with three lives (The lives are represented by gameobjects). My script is meant to make the player lose a life when they collide with a hazard (Trigger), they can hit only three hazards before being destroyed. The problem is, that when I hit a hazard; my player loses one life, and on the second hit, they lose both leftover lives and the player is destroyed instantly. Why does this happen all of a sudden? I had my script working perfectly beforehand. :/
Script:
#pragma strict
// Controls player health using 3 lives, 2 of which are visable
var fullHealth : int = 3;
var curHealth : int = 3;
var player : GameObject;
var explosion : GameObject;
var life1 : GameObject;
var life2 : GameObject;
function OnTriggerEnter(collider : Collider) {
if(collider.tag == "Hazard") {
curHealth -= 1;
}
}
function Update () {
if(curHealth >= fullHealth){
curHealth = fullHealth;
}
if(curHealth == 2){
curHealth = 2;
Destroy(life2);
}
if(curHealth == 1){
curHealth = 1;
Destroy(life1);
}
if(curHealth <= 0){
curHealth = 0;
Destroy(player);
Instantiate(explosion,transform.position, transform.rotation);
}
}
That's what I thought, but it doesn't seem to work? - It seems that whenever the player hits a hazard, the lives randomly subtract. *hits a hazard, loses two lives. Second game hits a hazard, loses all three.
Check if you have the trigger object duplicated by accident, so the player enters 2 colliders at the same time.
Answer by MrRandomFella · Jun 10, 2014 at 10:31 AM
Alrighty! I've just come across something very strange, my player was dying on the second hit with the "average sized" trigger. When I shrank it down or made it bigger, things began to work again. Strangely though, any other trigger of the same size didn't effect the player like the "buggy" one did. I guess it was all just a collision problem. Thank you to everyone that put in and helped! It was very appreciated! :P
Answer by Kiwasi · Jun 10, 2014 at 07:24 AM
I find a short period of invincibility immediately after hitting the trigger works well. You can do this by setting a bool on either the trigger or the player.
Thanks for the idea of invincibility after getting hit, I'll have to look into this :D
Answer by screenname_taken · Jun 10, 2014 at 07:07 AM
Perhaps you have an animation causing the trigger to trigger multiple times? Like a stumble or a knockback animation that makes the trigger move out and then back in.
Hmmm, maybe that is the cause, I'll remove the animation and get back with the result. :)
Ok, still nothing. $$anonymous$$y player just seems to take the first hit successfully and the second hit destroys it. :/
Answer by yigites · Jun 10, 2014 at 07:14 AM
Maybe you dublicated "Hazard" object and forgot to move so there may be 2 Hazard object inside each other.
Your answer
Follow this Question
Related Questions
Damage not triggering in build 1 Answer
Health below zero but shows negative numbers 1 Answer
Object Not Recieving Damage 3 Answers
Expecting ) found = 1 Answer
Enemy Health Damage 2 Answers