- Home /
Scripting a Death Fall
Hi there, Im stuck with a problem since at least 2 weeks. I searched for lot of info and forums and stuff but nothing.
I want my character dying every time I fall from 8 meter or more.
Is there a simple way to script this . My environement is huge and lot of denivelation. Can I script something without having a huge empty plane to detect the collision ?
What im trying to script with no success is to register the last time I was on the ground ( Y position ), register the next position grounded ( Y position ) and make the difference, if the difference is 8 or more kill my avatar.
Please help me
Answer by aldonaletto · Feb 28, 2012 at 10:43 AM
I would do exactly the same thing: measure the fall height from the last grounded position to the new ground location, and apply damage or kill the player, depending on the height. If you're character is a CharacterController, you can do the following:
var falling: boolean = false; // tells when the player is falling private var lastY: float; // last grounded height private var character: CharacterController;
function Start(){ character = GetComponent(CharacterController); lastY = transform.position.y; }
function Update(){ if (character.isGrounded == false){ // if character not grounded... falling = true; // assume it's falling } else { // if character grounded... if (falling){ // but was falling last update... var hFall = lastY - transform.position.y; // calculate the fall height... if (hFall > 8){ // then check the damage/death // player is dead } } lastY = transform.position.y; // update lastY when character grounded } }
okay but how can I script my player died ? Destroy something or ...
Usually you should create some effect to show that the player is dying - in a first person game, for instance, fade the screen to black - then reload the scene, what will destroy and recreate the player.
A simple but auto sufficient function to fade to black and reload the current level is the following:
var duration = 5.0; // fade duration in seconds
function Die(){ // create a GUITexture: var fade: GameObject = new GameObject(); fade.AddComponent(GUITexture); // and set it to the screen dimensions: fade.guiTexture.pixelInset = Rect(0, 0, Screen.width, Screen.height); // set its texture to a black pixel: var tex = new Texture2D(1, 1); tex.SetPixel(0, 0, Color.black); tex.Apply(); fade.guiTexture.texture = tex; // then fade it during duration seconds for (var alpha:float = 0.0; alpha < 1.0; ){ alpha += Time.deltaTime / duration; fade.guiTexture.color.a = alpha; yield; } // finally, reload the current level: Application.LoadLevel(Application.loadedLevel); } Add this code to the script above, and call the Die() function when the player dies.
The script of dying works perfectly thank you very very much !!! But now when I die my player is destroyed. I have another script of spawn working only when I strart the game. How can I add this script in my death script to make respawn my player every time I die?
$$anonymous$$y death script goes like this:
public class DeathRecognition : $$anonymous$$onoBehaviour { public bool falling = false; public float lastY; public float hauteurChute; public CharacterController character; private float deathFall = 7.632f;
void Update(){ if (character.isGrounded == false) { hauteurChute = lastY - transform.position.y ; if (hauteurChute < 0) lastY = this.transform.position.y;
} else { if ( hauteurChute > 0.5 ) { Debug.Log("Hauteur du saut :"+ hauteurChute);
} if ( hauteurChute >= deathFall ) { Debug.Log("DEAD"); Destroy(gameObject);
} hauteurChute = 0.0f;
lastY = this.transform.position.y;
} } } AND $$anonymous$$Y SPAWN SCRIPT GOES LI$$anonymous$$E THIS
public class Spawn : $$anonymous$$onoBehaviour { public Transform spawnPoint; public void spawn() { transform.position = spawnPoint.position; } }
There are two main ways to handle the player death: reload the whole level (like in the Die() function), or just "respawn" the player at the spawning position.
If you reload the level, the player must pick all the items and kill all enemies again, because everything in the level is restarted (except static variables).
The respawning alternative usually is done by just moving the player to the spawn point, like in your Spawn script. In this case, you should not destroy the player: replace the Destroy(gameObject); instruction with StartCoroutine(Die()); in your DeathRecognition function, and place the respawning code inside Die().
That's a C# version of the Die function (hope it's ok!), this time respawning the player at the spawn point ins$$anonymous$$d of reloading the whole level:
public Transform spawnPoint; public float duration = 5.0f; // fade duration in seconds
IEnumerator Die(){ // create a GUITexture: GameObject fade = new GameObject(); fade.AddComponent<GUITexture>(); // and set it to the screen dimensions: fade.guiTexture.pixelInset = new Rect(0, 0, Screen.width, Screen.height); // set its texture to a black pixel: Texture2D tex = new Texture2D(1, 1); tex.SetPixel(0, 0, Color.black); tex.Apply(); fade.guiTexture.texture = tex; // then fade the scene to black float alpha = 0f; while (alpha < 1.0f){ alpha += Time.deltaTime / duration; fade.guiTexture.color.a = alpha; yield return 0; } // "respawn" the player at the spawn point: transform.position = spawnPoint.position; // fade the scene in: while (alpha > 0){ alpha -= Time.deltaTime / duration; fade.guiTexture.color.a = $$anonymous$$athf.$$anonymous$$ax(alpha, 0f); yield return 0; } Destroy(fade); // the GUITexture isn't needed anymore }