Hello people, I desperately need your help as I’m going insane!
For the past week I’ve been sat at my computer trying to create a health system for a 2.5D game I’m making for a university assignment. But i just cant seem to get there.
Now I want my own custom GUI that I’ve made to display HEALTH as well as LIVES and the only way I can think to do it is by enabling and disabling the appropriate mesh renderers when the player is at certain health percentages.
I’ve written this code so far which gives me no errors but it doesn’t work the way expected. I haven’t yet got a damage functionality working either. so if someone could help me to write an apply damage script to go with this that would be super as well!
if you read the code you may be able to grasp how i’m expecting this code to work but if not do please ask so i can describe how i want this to work…
(i’m not very good at programming either)
public var Health = 100;
public var MinHealth = 0;
public var MaxHealth = 100;
public var Lives = 3;
public var MinLives = 0;
public var MaxLives = 3;
public var Damage = 35;//Damage is in increments of 35
function Start ()
{
Lives = 1;//Player starts with 3 lives
Health = 30;//Player starts with 100 points of health
}
function Update ()
{
if(Health >= 101);//If players health is greater or equal to 101 make it equal 100
{
Health = 100;
}
if(Health == 100);//If players health is equal to 100 go to the following function
{
FullHealth();
}
if(Health == 65);//If players health is equal to 65 go to the following function
{
TwoThirdsHealth();
}
if(Health == 30);//If players health is equal to 30 go to the following function
{
OneThirdHealth();
}
if(Health <= 0);//If players health is equal to 0 go to the following function
{
NoHealth();
Die();
//minus life
}
if(Lives >= 4); //If players health is greater or equal to 4 make it equal 3
{
Lives = 3;
}
if(Lives == 3); //When Player has 3 lives go to the following function
{
ThreeLives();
}
if(Lives == 2); //When Player has 2 lives go to the following function
{
TwoLives();
}
if(Lives == 1); //When Player has 1 life go to the following function
{
OneLife();
}
if(Lives == 0); //When Player has no lives go to the following functions
{
NoLives();
PermaDeath();
}
}
//Health GUI
function FullHealth()//Displays appropriate health icon
{
GameObject.Find("3Health").renderer.enabled = true;
GameObject.Find("2Health").renderer.enabled = false;
GameObject.Find("1Health").renderer.enabled = false;
GameObject.Find("0Health").renderer.enabled = false;
}
function TwoThirdsHealth()//Displays appropriate health icon
{
GameObject.Find("3Health").renderer.enabled = false;
GameObject.Find("2Health").renderer.enabled = true;
GameObject.Find("1Health").renderer.enabled = false;
GameObject.Find("0Health").renderer.enabled = false;
}
function OneThirdHealth()//Displays appropriate health icon
{
GameObject.Find("3Health").renderer.enabled = false;
GameObject.Find("2Health").renderer.enabled = false;
GameObject.Find("1Health").renderer.enabled = true;
GameObject.Find("0Health").renderer.enabled = false;
}
function NoHealth()//Displays appropriate health icon
{
GameObject.Find("3Health").renderer.enabled = false;
GameObject.Find("2Health").renderer.enabled = false;
GameObject.Find("1Health").renderer.enabled = false;
GameObject.Find("0Health").renderer.enabled = true;
}
//Lives GUI
function ThreeLives()//Displays appropriate life count
{
GameObject.Find("0Lives").renderer.enabled = false;
GameObject.Find("1Life").renderer.enabled = false;
GameObject.Find("2Lives").renderer.enabled = false;
GameObject.Find("3Lives").renderer.enabled = true;
}
function TwoLives()//Displays appropriate life count
{
GameObject.Find("0Lives").renderer.enabled = false;
GameObject.Find("1Life").renderer.enabled = false;
GameObject.Find("2Lives").renderer.enabled = true;
GameObject.Find("3Lives").renderer.enabled = false;
}
function OneLife()//Displays appropriate life count
{
GameObject.Find("0Lives").renderer.enabled = false;
GameObject.Find("1Life").renderer.enabled = true;
GameObject.Find("2Lives").renderer.enabled = false;
GameObject.Find("3Lives").renderer.enabled = false;
}
function NoLives()//Displays appropriate life count
{
GameObject.Find("0Lives").renderer.enabled = true;
GameObject.Find("1Life").renderer.enabled = false;
GameObject.Find("2Lives").renderer.enabled = false;
GameObject.Find("3Lives").renderer.enabled = false;
}
function Die()//Kills the player and takes 1 life away and then respawns player at last checkpoint
{
Destroy.GameObject.Find("Character");
//minus one life
//respawn
}
function PermaDeath ()//Loads the Game Over Scene after 3 seconds
{
yield WaitForSeconds(3);
Application.LoadLevel("Game_Over");
}
Any and all help will be greatly appreciated and if someone can get this working then I wish I could pay you for it! for a skilled programmer I bet this is a short task but this has taken me a week!
This is way too overkill and overcomplicated. What you need is a GUITexture set up and a script with a link to this GUITexture, a public health variable, a function to damage it and script to handle health and an array of textures. What you can do to simplify is that divide your health with a number to get proportions, for example health%25 will give you back 0,1,2,3,4, which you can use to set the appropriate texture in the GUITexture.Texture = textures[dividedValue]; this way you got your gui update up and running in three line of code.
K, i took the time to write something you want, it wasn’t tested, but you need something like this. Put this script on an empty gameobject and hook up the guitexture, textures, and tweak values if you wish.
public int health = 100;
public int maxHealth = 100;
public int minHealth = 0;
public int lives=3;
public int damage = 35;
public GUITexture gui;
public Texture[] textures;
private int lastTexture;
// Use this for initialization
void Start () {
}
public void Damage()
{
health-=damage;
}
// Update is called once per frame
void Update () {
if(health<0) health =0;
if(health>100) health = 100;
int currentTexture = health % 25;
if (lastTexture!=currentTexture)
{
lastTexture=currentTexture;
gui.texture = textures[currentTexture];
}
if(health == 0)
{
Destroy(GameObject.Find("Character"));
health = 100;
--lives;
//do respawn
}
if(lives == 0 )
{
Application.LoadLevel("Game_Over");
}
}
I agree that you should probably use GUITextures for this. I’ve written some example code based on what it looks like you’re trying to achieve. I’m sure this could be cleaned up a bit, as I know JS but I wouldn’t say I was an expert. So my apologies if it’s a little messy. I’ve added commenting to explain the different behaviors, and I hope I’ve done this adequately. Please let me know if you have any problems or questions.
//The GUITexture for the health. Create a GUITexture and assign the full
//health texture to it
var healthTexture : GUITexture;
//Then assign the corresponding health textures directly to these slots
var health0 : Texture2D;
var health1 : Texture2D;
var health2 : Texture2D;
var health3 : Texture2D;
var health = 100; //Starting health
var zeroThirdHealth = 0; //The amount of health that counts as none (I'm assuming it's 0)
var oneThirdHealth = 30; //The amount of health that counts as 1 third
var twoThirdHealth = 65; //The amount of health that counts as 2 thirds
var threeThirdHealth = 100; //The amount of health that counts as 3 thirds
//The GUITexture for the lives. Create a GUITexture and assign the full
//lives texture to it
var livesTexture : GUITexture;
//Then assign the corresponding life textures directly to these slots
var lives0 : Texture2D;
var lives1 : Texture2D;
var lives2 : Texture2D;
var lives3 : Texture2D;
var lives = 3; //Starting lives
//Is there a life 0? In other words, once the life count reaches 0, does the
//player die, or do they still have one more try?
var life0 = false;
//You can use this just to test the damage system beforehand. Just click
//the box in the inspector to call the ApplyDamage function with 35 damage
var testApplyDamage = false;
function Start () {
}
function Update () {
//Just for testing
if (testApplyDamage == true)
{
testApplyDamage = false;
ApplyDamage(35);
}
if (health > threeThirdHealth) //If health gets above the 3 thirds point, bring it back down
{
health = threeThirdHealth;
}
if (health < zeroThirdHealth) //If health gets below the 0 thirds point, bring it back up so it doesn't go negative
{
health = zeroThirdHealth;
}
//Display the appropriate life texture
if (lives == 3 && livesTexture.texture != lives3)
{
livesTexture.texture = lives3;
}
if (lives == 2 && livesTexture.texture != lives2)
{
livesTexture.texture = lives2;
}
if (lives == 1 && livesTexture.texture != lives1)
{
livesTexture.texture = lives1;
}
if (lives == 0 && livesTexture.texture != lives0)
{
livesTexture.texture = lives0;
}
if (health == zeroThirdHealth && lives == 1 && life0 == false)
{
//Player dies
print ("Player has died");
}
}
//Displays the appropriate health icon. Call this function from anywhere to
//apply damage, and specify the amount of damage to apply.
//Like this - ApplyDamage(35);
//If you're calling this function from another script, you'll need to let
//that script know where this one is. You would just assign the object
//THIS script is attached to to a GameObject variable slot in the inspector
//pane for the script calling this function.
//Like so - *ObjectWithThisScript*.GetComponent("*NameOfThisScriptExactly*").ApplyDamage(35);
//You would remove the asterisks (*)
function ApplyDamage(damage : int)
{
health -= damage; //Subtract the specified damage from the current health
//If health is at the 3 thirds point, display the assigned 3/3 health GUITexture
if (health == threeThirdHealth)
{
healthTexture.texture = health3;
}
//If health is equal to or over the 2 thirds point, but is still under
//the 3 thirds point, display the assigned 2/3 health GUITexture
if (health >= twoThirdHealth && health < threeThirdHealth)
{
healthTexture.texture = health2;
}
//If health is equal to or over the 1 third point, but is still under
//the 2 thirds point, display the assigned 1/3 health GUITexture
if (health >= oneThirdHealth && health < twoThirdHealth)
{
healthTexture.texture = health1;
}
//If health is over the zero thirds point, but still under the one thirds point,
//display the assigned 0/3 health GUITexture
if (health > zeroThirdHealth && health < oneThirdHealth)
{
healthTexture.texture = health0;
}
//If health is under or equal to the zero thirds point, but the lives are above
//1 (either 2 or 3), reset health back to maximum but subtract a life
if (health <= zeroThirdHealth && lives > 1)
{
lives -= 1;
health = threeThirdHealth;
healthTexture.texture = health3;
//Add any sounds/effects for losing a life here
}
//If health is under or equal to the zero thirds point and the player had 0 lives
//left, they die. (This is if life0 is set to true)
if (health <= zeroThirdHealth && lives == 0)
{
healthTexture.texture = health0;
//Player dies
print ("Player has died");
}
//If health is under or equal to the zero thirds point, but the player still
//had 1 life left and life0 is set to true, do the same as usual
if (health <= zeroThirdHealth && lives == 1 && life0 == true)
{
lives -= 1;
health = threeThirdHealth;
healthTexture.texture = health3;
//Add any sounds/effects for losing a life here
}
//...However, if they had one life left but
//life 0 is set to false, then they lose that final life and die
if (health <= zeroThirdHealth && lives == 1 && life0 == false)
{
lives -= 1;
healthTexture.texture = health0;
//Player dies
print ("Player has died");
}
}