I have a system in place that allows my player (FPS Controller) to take health damage if it falls more than a certain distance. I’m trying to make it into a 2 stage system where a higher fall would result in greater health loss.
Basic script-
#pragma strict
var lastPositionY : float = 0f;
var fallDistance : float = 0f;
var player : Transform;
private var controller : CharacterController;
var currentHealth : float = 100.0f;
function Start()
{
controller = GameObject.Find("First Person Controller").GetComponent(CharacterController);
}
function Update ()
{
if(lastPositionY > player.transform.position.y)
{
fallDistance += lastPositionY - player.transform.position.y;
}
lastPositionY = player.transform.position.y;
if(fallDistance >= 5 && controller.isGrounded)
{
currentHealth -= 20;
ApplyNormal();
}
if(fallDistance <= 5 && controller.isGrounded)
{
ApplyNormal();
}
}
function ApplyNormal()
{
fallDistance = 0;
lastPositionY = 0;
}
function OnGUI()
{
GUI.Box(Rect(10, 20, 50, 20),"" + currentHealth);
}
This works fine as it is. I changed the script by adding an additional IF statement so as any fall greater than 10 would result in greater health loss BUT the script only recognises the first IF statement (any fall >=5) and not the second.
revamp script-
#pragma strict
var lastPositionY : float = 0f;
var fallDistance : float = 0f;
var player : Transform;
private var controller : CharacterController;
var currentHealth : float = 100.0f;
function Start()
{
controller = GameObject.Find("First Person Controller").GetComponent(CharacterController);
}
function Update ()
{
if(lastPositionY > player.transform.position.y)
{
fallDistance += lastPositionY - player.transform.position.y;
}
lastPositionY = player.transform.position.y;
if(fallDistance >= 5 && controller.isGrounded)
{
currentHealth -= 20;
ApplyNormal();
}
if(fallDistance >= 10 && controller.isGrounded)
{
currentHealth -= 50;
ApplyNormal();
}
if(fallDistance <= 5 && controller.isGrounded)
{
ApplyNormal();
}
}
function ApplyNormal()
{
fallDistance = 0;
lastPositionY = 0;
}
function OnGUI()
{
GUI.Box(Rect(10, 20, 50, 20),"" + currentHealth);
}
Any suggestions guys ?