How can i make this script to set CurrentHealth to 0 or lower when entering a certain trigger?
I have made a platform that is moving and i want to detect if that platform hits the player and not detect if the player is on top of it.
#pragma strict
var lastPositionY : float = 0f;
var fallDistance : float = 0f;
var player : Transform;
var maxDistance : float = 3f;
var healthTimer : float = 0f;
var regenTime : float = 200f;
var regen : float = 1f;
var healthMax : float = 20f;
var resX : float = 0f;
var resY : float = 1f;
var resZ : float = 0f;
var maxFall : float = 500f;
var timerSec : float = 0f;
var manualReset : KeyCode;
var currentHealth : float = 20.0f;
private var controller : CharacterController;
function Start()
{
controller = GameObject.Find("FPSController").GetComponent(CharacterController);
}
function Update ()
{
if(lastPositionY > player.transform.position.y)
{
fallDistance += lastPositionY - player.transform.position.y;
}
lastPositionY = player.transform.position.y;
if(fallDistance >= maxDistance && controller.isGrounded)
{
currentHealth -= 1;
fallDistance -= 1;
print("MainScript.js: Fall damage taken. HP left: " + currentHealth + " healthTimer: " + healthTimer);
}
if(fallDistance <= maxDistance && controller.isGrounded)
{
ApplyNormal();
}
if(currentHealth <= 0)
{
transform.position = new Vector3(resX, resY, resZ);
lastPositionY = 0f;
fallDistance = 0;
currentHealth = 20.0;
print("MainScript.js: Player is dead");
}
if(currentHealth < healthMax) {
healthTimer += 1;
}
if(healthTimer >= regenTime && currentHealth <= (healthMax - 1))
{
currentHealth += regen;
healthTimer -= regenTime;
print("MainScript.js: Health is below " + healthMax + ". Regen from: " + (currentHealth - regen) + " Regen to: " + currentHealth);
}
if(fallDistance >= maxFall)
{
transform.position = new Vector3(resX, resY, resZ);
print("MainScript.js: Player fell out of map (Distance: " + fallDistance + ")");
ApplyNormal();
}
if(Input.GetKeyDown(manualReset))
{
ApplyNormal();
transform.position = new Vector3(resX, resY, resZ);
currentHealth = 20;
healthTimer = 0;
print("MainScript.js: key '" + manualReset + "' pressed, player reset");
}
}
function ApplyNormal()
{
fallDistance = 0;
lastPositionY = 0;
}
function OnGUI()
{
GUI.Box(Rect(0, 0, 100, 20),"Health: " + currentHealth);
}