How to kill player (and or end game) when enemy hits the ground? JAVASCRIPT
I have this game where my player is a hat and you catch the bombs and don't let them fall to the ground, like you're protecting it or something like that.
Anyway, seeing as the whole point in the game is to keep the bombs from touching the ground, I need a script where the player is killed and or the game ends when a bomb hits the ground. I've tried/looked up everything, but I can't seem to find a code/code this stupid script myself. Please help!
Answer by dan5071 · Feb 14, 2016 at 01:05 AM
How exactly do you define the game ending or the player being killed? Is the player game object destroyed? Does a menu pop up? Whatever the logic behind those things happening might be, one way you could approach the problem is to setup a collider on the ground and do collision detection through OnCollisionEnter(). When you detect a collision with the ground, you perform the necessary actions to end the game. Note that this will require all of your bombs to have (presumably) sphere or circle colliders in addition to the ground. Here are some good resources for learning more:
http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
http://unity3d.com/learn/tutorials/modules/beginner/physics/on-collision-enter
An alternative option would be to use the Update routine to detect whether or not each bomb's y position is below a certain height (i.e., the y position of the ground). If so, destroy the player game object, instantiate your replay menu, and do whatever else you might need to do. Note that this will require either each of your bombs to be running the update routine in the scripts attached to each individual object, or to run a single script that searches for all bombs in the scene and detects if their y position at ground level.
Your answer
Follow this Question
Related Questions
Destry object on collision with 'bullet' 0 Answers
Question about coll.contacts 1 Answer
Collider Issue 0 Answers
help with colliders not working 2 Answers
Hit from kinematic rigidbody2d 1 Answer