Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Toy · Feb 28, 2012 at 03:51 AM · collisionpositionheightdeathfall

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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 } }

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Toy · Feb 28, 2012 at 09:12 PM 0
Share

okay but how can I script my player died ? Destroy something or ...

avatar image aldonaletto · Feb 29, 2012 at 05:07 AM 1
Share

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.

avatar image Toy · Feb 29, 2012 at 05:30 PM 0
Share

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;
  }
}

avatar image aldonaletto · Mar 01, 2012 at 04:23 AM 0
Share

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 }

avatar image Toy · Mar 01, 2012 at 02:21 PM 0
Share

ok I will try thank you very very much :)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I want my player to die when collide with enemy from front or back and not when it jumps on it (i alread tried checking if player is grounded or not) and if someone could provide script for it ill be glad 0 Answers

How to change fall delay of a gameobject by time? 2 Answers

How to restart the level upon cantact 1 Answer

My Player dies only when he wants to?!? 1 Answer

How to get the relative position of a Collider with OnControllerColliderHit()? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges