Im a designer student and I am making a game which involves a player that dies when he falls from more than 10 meters. I have a lot of platform in my map from different height. I want to know how can I make a script that recognizes that my player dies when he falls more than 10 meters.
How can i make a counter that calculates my fall every frame, every time my player fall?
if(!player.isGrounded)
{
//calculate the distance between our current height and the height we were in the last frame
lastYTravelDistance = player.position.y - lastYPosition;
//if the difference is negative, it means we're descending
fallHeight += lastYTravelDistance < 0 ? lastYTravelDistance : 0;
}
else
{
//we check to see if we passed the allowed falling distance and kill the player if necessary
if(fallHeight >= -deathHeight)
KillPlayer();
//reset fall height since we landed (doesn't matter if we're dead or alive)
fallHeight = 0;
}
//cache our current Y position for comparison in the next frame
lastYPosition = player.position.y;
yeah, that might work but a little more info would help. Like is this JS or C#? void start? function start? function update void update, what var need to be at the start
Thank you so much. It's very simple solution with only one correction, as I think. There is need sign lower at line if (fallHeight <= -deathHeight) So after that it works just fine.
yeah, that might work but a little more info would help. Like is this JS or C#? void start? function start? function update void update, what var need to be at the start
– shadowpuppetThank you so much. It's very simple solution with only one correction, as I think. There is need sign lower at line
– nikm902if (fallHeight <= -deathHeight)So after that it works just fine.