I’ve been meaning to add falling damage to Unitys First Person Controller. I am just unsure of the best way to go about doing this. Was thinking of starting a timer when grounded == false, then after lets say 4 seconds it would flag your character to die after grounded == true. Any other suggestions that would work better?
You can look at how they do it on the FPSWalkerEnhanced script. Your method would work fine too. Alternatively, you could create a variable that you increment as long as your character isn’t grounded then, once you he hts the ground, subtract the variable’s value from the player’s health.
For what I’m doing it will just be a kill or no kill type of thing. Not a specific amount of damage
I think the best way would be to create a variable that you increment from the character moving in the negative Y direction (downward).
Like this you can say if distance fallen > 5 meter then die or something like that.
Great idea! I could Create two variables. One for beginning to fall which would keep track of the y var. Then when the User hits the ground add that Y position to another var which could be landed. From there compare the two var and if it’s greater then a certain amount then kill the Player.
So I did some messing around with the CharacterMotor and came up with this. Problem is when I jump like normal I get a positive number and when I fall it’s always a negative. No matter where I place my terrain on the Y axis I always get this issue with jumping and falling.
var beginFalling : float;
var landing : float;
var fallDistance : float;
var didJump : boolean;
var player : GameObject;
var fell : boolean;
function Update () {
if (!useFixedUpdate)
UpdateFunction();
//FALL DEATH calculation
if(grounded == false) {
didJump = false;
if(fell == false) {
fell = true;
beginFalling = 0;
beginFalling = player.transform.position.y;
}
}
if(grounded == true didJump == false) {
didJump = true;
landing = player.transform.position.y;
fallDistance = beginFalling - landing;
Debug.Log(beginFalling);
Debug.Log(landing);
Debug.Log(fallDistance);
if(fallDistance > 90) {
Debug.Log("DEAD");
//Death function
}
landing = 0;
fallDistance = 0;
}
I think rather than finding where your character start to fall, do something like this:
float lastPositionY = 0f;
float FallDistance = 0f;
function Update ()
{
if(grounded)
{
FallDistance= 0;
lastPositionY =0;
}
else
{
if (lastPositionY > player.transform.position.y)
{
FallDistance += lastPositionY - player.transform.position.y;
}
lastPositionY = player.transform.position.y;
}
}
In other words when the character move in the negative Y, add the Y distance traveled since the last frame to the FallDistance float.
Can someone write a standalone script for fall damage?