- Home /
Death on impact with water
So i have a open ocean, on top of it are platforms. in my game you jump from platform to platform! object is to get to the finish line without touching the water! i'd like to make it so that when you touch the water u'r player die, and you respawn at the begining! so far there are no life counter, so loosing a life is not a relevant factor! i'm pretty ned to unity so effects like: fading to red upon death, ETC are not important! i'm using the default "first person controller" that comes with the program! i'm very new to unity so i'd appreciate if any responses where kept simple! posting a script won't be enough, i'm afraid! i need to know what to do with it! i'd be very glad for any help!
Answer by save · May 12, 2012 at 11:00 PM
You could have a collider set as trigger that upon collision initiates a sequence on the player.
//Trigger collider script
function OnTriggerEnter (other : Collider) {
if (!other.CompareTag("Player") return; //Exit if this isn't the player
var pScript : PlayerScript = other.GetComponent(PlayerScript); //Get the script component of the player (this could be cached for performance too)
pScript.Kill(); //Run the function called Kill on the player
}
//Part of the player script
private var startPos : Vector3;
function Start () {
startPos = transform.position;
}
function Kill () {
animation.Play("death"); //Initiate the 'death' animation
yield WaitForSeconds (animation["death"].length); //Wait for animation to finish
transform.position = startPos; //Reposition the player at starting point
}
If you don't use the physics engine you might as well want to check for a certain height, and when below that you initiate the kill function.
private var startPos : Vector3;
private var waterLevel : float = .0;
private var isDead : boolean = false;
function Start () {
startPos = transform.position;
}
function Update () {
if (transform.position.y < waterLevel && !isDead) Kill();
}
function Kill () {
isDead = true;
animation.Play("death");
yield WaitForSeconds (animation["death"].length);
transform.position = startPos;
isDead = false;
}
tnx for the scripts, but as i said i'm new, and i dunno exactly how to integrate it in my game! of course i assume i just click "create a new script" and paste this code. but then what! whats the simplest way of markign an object so that the player dies when co$$anonymous$$g in contact with it! i used the default "daylight water" for the visuals, and a water prefab i downloaded of the internet. (the prefab only creates the small waves in the water, the water does not react when my player comes in contact with it)
You need to add a collider to whatever you are using as water! I recommend making a big, invisible box trigger for this- when you create a collider there is a checkbox called 'Is Trigger'- set this to true.
As for the water 'reacting' when a player enters it- this stuff is tantamount to fluid simulation. The default water asset looks good from a distance, but definitely doesn't do that kind of complicated effect without serious modification.
In any case, if you want to make any kind of game mechanics at all, you'll need to do a little coding practice first. One of the few things Unity lacks is a magical 'make game' button- the fact is, games are about 90% program$$anonymous$$g, and there's not much you can do to change that.
@mr16thprestige As syclamoth says you need to have some fundamental knowledge of scripting before you're trying to make a game. I recommend you to start here.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071420im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Making a turret shoot at a low fire rate. 2 Answers
AI Awareness 2 Answers
Hide Objects on Higher Plane 0 Answers
Death script problem 2 Answers
Which is better, lots of if-statements or seperate button scripts? 1 Answer