how do i make player die when falling certain height?
I want game-over screen to pop up when player falls. my idea is if, player falls certain distance within certain amount of time then set game-over screen active. can anyone show the proper way to write it down please. thanks.
Answer by Brylos · Dec 06, 2016 at 07:26 PM
If you're using a Character Controller component it shouldn't be too hard
public CharacterController Player;
float CurrentFallTime;
public float MaxFallTime = 3;
// This is how many seconds you want to occur before it is decided that he will die from falling.
bool PlayerIsFalling;
void Update()
if (Player.isGrounded == false)
{
PlayerIsFalling = true;
}
else PlayerIsFalling = false;
if (PlayerIsFalling)
{
CurrentFallTime += Time.deltatime;
}
You didn't specify if you want the player to die when he hits the ground or weather or not he's in the air.
If you want him to be able to die while in the air add this to the update void:
if (CurrentFallTime >= MaxFallTime)
{
PlayerDeath();
//You'll need to make a void for when your player dies
}
If you want him to die when he hits the ground add these to the update void:
if (Player.IsGrounded && CurrentFallTime < MaxFallTime)
{
CurrentFallTime = 0;
}
if (CurrentFallTime >= MaxFallTime && Player.IsGrounded)
{
PlayerDeath();
}
I believe that should work. Tell me how it goes.
Answer by tupak1993 · Dec 09, 2016 at 07:50 PM
hi im getting these error....any idea? THanks.
Thats my fault. You'll have to rewrite that section like this:
if (Player.isGrounded)
{
PlayerIsFalling = false;
}
else PlayerIsFalling = true;
Thanks appreciate your help but its still not working. there are no compier error but it just doesn't work... any idea? Im beginner on this.
And for some reason i cannot add the scrrenshot either....
Answer by Coleclaw199 · Dec 11, 2016 at 12:06 AM
public class Fall : Monobehaviour {
public CharacterController Player;
float CurrentFallTime = 3F;
public bool PlayerIsFalling;
// This is where your error was.
void Update() // You had a semicolon here.
{
if(Player.isGrounded == false)
{
PlayerIsFalling = true;
}
else // Your other error was putting the "PlayerIsFalling = false" here.
{
PlayerIsFalling = false
}
if(PlayerIsFalling)
{
CurrentFallTime += Time.daltaTime;
}
}
}
This is untested code, so this may or may not fix your problem, good day!
every time i try using this code to rester the scene it just says "Scene '1' couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded." but it was added to the build settings.
public class DeathScript : $$anonymous$$onoBehaviour
{
public string Level;
public CharacterController Player;
bool PlayerIsFalling;
float CurrentFallTime;
public float $$anonymous$$axFallTime = 3;
void Update ()
{
if (Player.isGrounded == false)
{
PlayerIsFalling = true;
} else
{
PlayerIsFalling = false;
}
if (PlayerIsFalling = true)
{
CurrentFallTime += Time.deltaTime;
}
if (CurrentFallTime >= $$anonymous$$axFallTime)
{
Scene$$anonymous$$anager.LoadScene(Level);
}
}
}