Platform kill player on contact
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);
}
Answer by Jhyatt · Feb 03, 2016 at 08:00 PM
You'll need a trigger positioned at the location you want your character to die if it enters.
These are the steps I would take to achieve this effect.
Create a cube
Check "Is Trigger" under the Box Collider properties.
Position and scale the cube so that it is in the area you want your character to die if entered.
Make the cube a child of your platform game object.
Disable the mesh renderer of the cube.
Create a new tag for the cube and call it something like "DeathTrigger"
Then apply this section of code to your character script:
void OnTriggerEnter(Collider collider)
{
if (collider.tag == "DeathTrigger")
{
currentHealth = 0f;
}
}
(Take note that this code is C#. I don't know if it's exactly the same syntax for your JS code.)
I hope that helps you out.
EDIT:
I think this would be the syntax for JS. I'm not 100% certain on that though.
function OnTriggerEnter (collider: Collider) {
if (collider.tag == "DeathTrigger")
{
currentHealth = 0f;
}
}
Ty, That code works like a charm for me! Now im 1 step closer to get the scripts i need
Your answer
![](https://koobas.hobune.stream/wayback/20220612062858im_/https://answers.unity.com/themes/thub/images/avi.jpg)