I have a really annoying bug in my code and I can’t for the life of me figure out whats wrong with it. any help will be much appreciated, Thank you in advance. please appreciate that I am very new to unity scripting.
Heres my script…
#pragma strict
var isDead : boolean;
var showDeadGUI : boolean;
var canMove : boolean;
var lastPositionY : float = 0f;
var fallDistance : float = 0f;
var player : Transform;
private var controller : CharacterController;
var currentHealth : float = 10.0f;
function Start () {
controller = GameObject.Find("First Person Controller").GetComponent(CharacterController); //require the first person controller script
isDead = false; //is the player dead?
showDeadGUI = false; //should be asking to restart?
canMove = true; //can the player move?
}
function Update () {
if(lastPositionY > player.transform.position.y) //last y position > player y
{
fallDistance += lastPositionY - player.transform.position.y; //if the last y position is more or equel to the fall distance change the players y position
}
lastPositionY = player.transform.position.y; //sets the last y position
if(fallDistance >= 5 && controller.isGrounded) //defines the fall height
{
currentHealth -= 5; //takes 5 away from the players current health
ApplyNormal(); //applys a normal statement
}
if(fallDistance <= 5 && controller.isGrounded) //if fall distance is less than 5 apply a normal
{
ApplyNormal(); //applys a normal
}
if(currentHealth <= 0) //if the players health is less than or the same as 0 set isDead to true
{
isDead = true; //sets is dead to true
}
if (isDead == true) //is isDead true?
{
showDeadGUI = true; //should the code be showing Dead GUI
canMove = false; //can the player move?
{if (Input.GetKeyDown (KeyCode.Return)) { //if player wants to restart press enter
Application.LoadLevel (0); //restart the game, the first level
showDeadGUI = false; //once enter pressed stop showing dead GUI
}
}
}
if (canMove == false) //if the game is over we need to tell the script to freeze the game
{
Time.timeScale = 0.00; //sets the speed of the game to 0 FPS
if (Input.GetKeyDown (KeyCode.Return)) //is the return key down?
{
Time.timeScale = 1.00; //sets the game to full playback rate
}
}
}
function ApplyNormal() //applys a normal
{
fallDistance = 0; //defines the fall distance
lastPositionY = 0; //defines the fall height
}
function OnGUI() //defines a GUI function
{
GUI.Box(Rect(10, 20, 100, 20),"" + currentHealth); //draws a box with current health
if (isDead == false) //isDead false?
{
GUI.color = Color.red; //sets the color to red if false
}else
{
GUI.color = Color.green; //sets the color to green if true
}
GUI.Box(Rect(10, 40, 100, 20),"" + isDead); //draws the isDead GUI box
if (showDeadGUI == true)
{
GUI.backgroundColor = Color.red; //sets the dead GUIs background to red
GUI.color = Color.red; //sets the text color to white
GUI.Box(Rect(100, 400, 1000, 100),"" + "Press Enter To Restart. Don't feel bad that you died just press enter and pretend that it never happend"); //draws the dead GUI box and asks for a restart
}
}