- Home /
Kill player when hit Groung (JavaScript)
Well, i'm making an FPS the map is like a roof, so when you fall and hit the ground, you die, i tried lot's of methods i found here in Unity Answers but none of them worked, so, basicly, when I hit the Terrain(Untagget), the Player Cameras (FPS and Minimap) are disabled, and the DeathCamera is enabled. Well, but this is not my problem, my problem is that, if I use OnTriggerEnter, everything that hits the ground dies, and if I use OnCollisionEnter (them search by tag "Player") nothing happens when i hit the ground.
here's my code, if somebody can help me, thanks a lot, and sorry for my english, i'm from Brazil.
[JAVASCRIPT] Fall.js
#pragma strict
var fpsCamera : Camera;
var minimapCamera : Camera;
var gameoverCamera : Camera;
var skinDead : GUISkin;
var isDead : boolean = false;
function Start(){
fpsCamera.enabled = true;
minimapCamera.enabled = true;
gameoverCamera.enabled = false;
}
function OnCollisionEnter(collision : Collision){
if(collision.gameObject.tag == "Player"){
isDead = true;
DieFunc();
}
}
function DieFunc(){
fpsCamera.enabled = false;
minimapCamera.enabled = false;
gameoverCamera.enabled = true;
yield WaitForSeconds (5);
Application.LoadLevel("#1");
}
function OnGUI(){
GUI.skin = skinDead;
if(isDead){
GUI.Label(Rect(Screen.width /2 -60,Screen.height /2,500,500), "You're Dead");
}
}
(All variables are assigned, removed empty spaces to make the text smaller)
Thanks!
Answer by jenci1990 · Dec 02, 2014 at 12:13 PM
If your player collider or your ground collider is trigger, the OnCollisionEnter method couldn't called. Use OnTriggerEnter or change the colliders nonTrigger.
function OnTriggerEnter(collider : Collider) {
if(collider.gameObject.tag == "Player"){
isDead = true;
DieFunc();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613172931im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast to Terrain (Conditional Statements) 1 Answer
Terrain Collision Glitch 1 Answer
How can I make my truck drive on terrain and on bridge 1 Answer
How to keep instatiated object above a certain height? 1 Answer
Collision with Terrain 2 Answers