- Home /
Players Health wont work?!? PLEASE HELP
So I have a Ninja. The Ninja starts out with 2 Lives. I start my game, but when the Ninja gets hit ONCE it dies, even if the Ninja has 3 LIVES it dies in one hit. Please help. Every time the player gets hit the GUI Texture changes. This part works, it accurately displays my current number of HEARTS,but even if it says I have 2 HEARTS left It dies.
#pragma strict
static var life = 2; //Players Starting Health is 2 HEARTS
var life1 : Texture2D; //HEART 1
var life2 : Texture2D; //HEART 2
var life3 : Texture2D; //HEART 3
var lifeGUI : GUITexture;
function Update () {
switch(life) //CHANGES TEXTURE BASED ON LIFE
{
case 1:
lifeGUI.texture = life1;
break;
case 2:
lifeGUI.texture = life2;
break;
case 3:
lifeGUI.texture = life3;
break;
}
}
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.name == "Shuriken(Clone)" && life == 1) //IF PLAYER GETS HIT ON HIS LAST LIFE
{
TurnGameOver.dead = true;
TurnGameOver.dead2 = true;
}
if(other.gameObject.name == "Shuriken(Clone)" && life == 2) //PLAYER GETS HIT WITH 2 LIVES
{
life = 1;
}
if(other.gameObject.name == "Shuriken(Clone)" && life == 3)//PLAYER GETS HIT WITH 3 LIVES
{
life = 2; //LIFE DECREASES WHEN HIT
}
}
function OnTriggerExit(other : Collider)
{
if(other.gameObject.name == "Shuriken(Clone)")
{
TurnGameOver.dead = false;
}
}
Answer by TargonStudios · Dec 12, 2013 at 01:04 AM
A lot of games have immunity time (Time until a player/NPC can be damaged again) adding one in just might fix your problem. Another thing is deleting the Shuriken once it hits the player. For example:
#pragma strict
static var life = 2; //Players Starting Health is 2 HEARTS
var life1 : Texture2D; //HEART 1
var life2 : Texture2D; //HEART 2
var life3 : Texture2D; //HEART 3
var CanReceiveDamage = true;
var lifeGUI : GUITexture;
function Update () {
switch(life) //CHANGES TEXTURE BASED ON LIFE
{
case 1:
lifeGUI.texture = life1;
break;
case 2:
lifeGUI.texture = life2;
break;
case 3:
lifeGUI.texture = life3;
break;
}
}
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.name == "Shuriken(Clone)" && life == 1) //IF PLAYER GETS HIT ON HIS LAST LIFE
{
if(CanReceiveDamage == true){
Destroy(other.gameObject);// Destroys Shuriken
CanReceiveDamage= false;
ImmunityTime();
TurnGameOver.dead = true;
TurnGameOver.dead2 = true;
}
}
if(other.gameObject.name == "Shuriken(Clone)" && life == 2) //PLAYER GETS HIT WITH 2 LIVES
{
if(CanReceiveDamage == true){
Destroy(other.gameObject);
CanReceiveDamage= false;
ImmunityTime();
life = 1;
}
}
if(other.gameObject.name == "Shuriken(Clone)" && life == 3)//PLAYER GETS HIT WITH 3 LIVES
{
if(CanReceiveDamage == true){
Destroy(other.gameObject);
CanReceiveDamage= false;
ImmunityTime();
life = 2; //LIFE DECREASES WHEN HIT
}
}
}
function OnTriggerExit(other : Collider)
{
if(other.gameObject.name == "Shuriken(Clone)")
{
TurnGameOver.dead = false;
}
}
function ImmunityTime(){
yield WaitForSeconds(0.25);
CanReceiveDamage = true;
}
Oh and also, you should try and improve your script a bit more. Its very repetitive and could easily be shortened. You would only need 1 part of the script for all the damage parts, For example:
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.name == "Shuriken(Clone)")
{
if(CanReceiveDamage == true){
Destroy(other.gameObject);
CanReceiveDamage= false;
ImmunityTime();
Life--;
}
}
}
As simple as that.
Hope this helped you!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Can't get Gui Pos on top of Enemy ingame! 0 Answers
Help With Enemy Health bars 3 Answers
Show GUI.Label when touching trigger 4 Answers
How to disable my Script and Gui 1 Answer