x


Make player die is they fall for too long

How would I go about achieving this? I'm looking for a way to prevent sequence breaking in my game and am under the impression this would stop that from happening.

more ▼

asked May 04 '11 at 08:30 AM

MK7 gravatar image

MK7
66 25 26 30

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

If you have a rigidbody on your gameobject you can use the velocity value to determine if your player has died.

http://unity3d.com/support/documentation/ScriptReference/Rigidbody-velocity.html

if(rigidbody.velocity.y > 10){
print("player has died")
}

if you are using a Character controller you might be able to do the same, im not sure though :)

edit:

public float timeinair = 0;
public float deathtimer = 10;
private bool dead = false;

void update(){
     if(!player.isgrounded){
          timeinair += time.deltatime
     }
     if(timeinair >= deathtimer){
          dead = true;
     }
}
more ▼

answered May 04 '11 at 08:46 AM

Kacer gravatar image

Kacer
705 15 18 31

Well, that would be quit dangerous, because say, your character can be boosted somehow to go faster with bonuses or else, then checking for velocity would create conflict.

May 04 '11 at 08:48 AM Jean Fabre

well, the y value should exceed that unless he falls for too long, or recieve a boost.

alternately he could check if the collider havnt been grounded for a certain time, so ex:(look at my edit)

May 04 '11 at 09:53 AM Kacer

This is definitely what I was looking for. Thanks a bunch for the help!

May 05 '11 at 01:49 AM MK7
(comments are locked)
10|3000 characters needed characters left

Hi,

I would build a simple collider below your platform, and when the player hit that collider, it means death. then you can easily define several similar colliders around your levels at different height.

This would prevent always checking in an update, that's not very efficient.

Bye,

Jean

more ▼

answered May 04 '11 at 08:46 AM

Jean Fabre gravatar image

Jean Fabre
3.1k 69 76 103

(comments are locked)
10|3000 characters needed characters left

well you could make it so that if your player falls to a certain point, then you basically respawn it to another.... Here is a script that i use:

function Update ()
{
    //check if you character fell off the platform
    if(transform.position.y < -10)
    {
        transform.position.y = 5.329594; //set the y axis that the player will spawn to
        transform.position.x = 5.203072; // set the x axis that the player will spawn to
        transform.position.z = -46.88659; // set the z axis that the player will spawn to
    }
}

where it says "< -10" it means that when the player is below that in the y axis, then it will respawn the player to where ever the next three transform.position axis are located.

attach this script to your player game object...

hope this helps

-Grady

(script from tornado twins)

more ▼

answered May 04 '11 at 08:40 AM

Grady gravatar image

Grady
1.1k 66 70 83

I like your script , but i keep on running into this error code:

Assets/_3scripts/fall.js(4,14): BCE0020: An instance of type 'UnityEngine.Transform' is required to access non static member 'position'.

Aug 02 '12 at 01:21 AM vakuzar
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x491
x99
x64

asked: May 04 '11 at 08:30 AM

Seen: 1453 times

Last Updated: Aug 02 '12 at 01:21 AM