- Home /
How do I make my character die when hitting the terrain?
Hello. I am new at Unity, and I am not exactly good at programming. I use JavaScript. I am creating a game where you have to jump on blocks and avoid the ground, aka the terrain. I am confused on how to die on contact with the terrain, have the game fade to black, and have my character respawn at the beginning of the level. Could somebody help me figure this out? Thanks! Also, this is a first-person 3D game.
Answer by Nick4 · Jun 11, 2014 at 10:47 AM
If you're new, you should look into some basic tutorials.
First off, you're going to need to attach a terrain collider to your terrain (which is unnecessary if you have just created one because it already comes with a terrain collider) and a regular collider to your character. Collider simply detects collision. This way when your character collides with the terrain you'll be able to collect collision data from a function called "OnCollisionEnter(collision : Collision)". Then inside this function, you're gonna need to check if it's your character that collided with the terrain with an if statement. Which will be something like this.
function OnCollisionEnter(collision : Collision)
{
// In this case, your character must have the tag "Player"
// just because we're checking whether collided object's tag is collider
if(collision.transform.tag == "Player")
{
Respawn();
}
}
function Respawn()
{
// Application.loadedLevel is the last loaded level which is the current one
Application.LoadLevel(Application.loadedLevel);
}
As you can see I've also added a Respawn function, which loads current level.
To fade the screen to black gradually requires a simple black guiTexture. Simply animate its alpha channel from 0 to 255 then play the animation right before respawning. Good luck.
The script isn't exactly working. There is one error on the script. On line 5, it says unexpected char: ' '. (BCE0044) What can I do to fix this?
I have another problem. The script isn't working. Could somebody help?
Answer by Imposible · Jun 11, 2014 at 10:42 AM
Hi there!
i think the good idea for that is to check your player's collider....during OnTriggerEnter ... then check if your player collide with the Terrain.....then performs a die animation or whatever you want...
basic for this is use CharacterController.isGrounded to check if the player is on ground..
==neutra==
Your answer
Follow this Question
Related Questions
Can't crash airplane into ground 4 Answers
Camera Colliding with Terrain 1 Answer
Airplane - Collision With Terrain Problem 1 Answer
Stopping character from walking on grass 0 Answers
Death, Respawn and other things. 0 Answers