- Home /
Make the game stop if you get hit by enemy object
So what i wants to know is, how i make the game stop when, you get hit by a enemy object. if we say the health is 1; var Health : int = 1 And then something like if (Health == 0) (STOP GAME)
Something like that, just with some new commands, and stuff that i don't know. I just started scripting so.. yeah.. Well i want the health to go to 0 when hitting an enemy object, and that activates the ''if statement'' and stops the game. I think it sounds simple, but i don't know how simple it is. Could anyone post a script, doing what i just said, with the same var and if statement as i used (not some other statement that does the same as them)
And please explain what the things do, cause that is mostly what i want to know. Thanks.!
Answer by gajdot · Nov 19, 2013 at 04:21 PM
Well by hitting you mean colliding then you can do something like this:
function OnCollisionEnter(other: Collision) { //listens for collisions in your game
// you can read more about this http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
if(other.Tag=="Enemy"){ //check if that you bumped into an enemy
Health=Health-1; //decrease your health
if(Health<=0) { //if your health is 0 then stop game, exit what so ever
Time.timeScale = 0; // this will freeze the game, stop
// you can also use the following if you want the game to exit
//Application.Quit();
//or reload current level
//Application.LoadLevel(Application.loadedLevel);
}
}
}
I usually don't script in javascript so it may be some bugs or too, but this is the basic concept. Just attach this script to your player, add Enemy tag to your enemies, declare the health variable with the starting health and attach colliders both for you and your enemies.
EDIT: FINAL ANSWER: So the thing was that you where using character controller which doesn't have nor rigid body nor collider, but it has a special function, so modify your script like this:
var Health = 1;
function OnControllerColliderHit(hit: ControllerColliderHit){
if(hit.collider.tag=="Enemy"){ //check if that you bumped into an enemy
Health=Health-1; //decrease your health
if(Health == 0) { //if your health is 0 then stop game, exit what so ever
Time.timeScale = 0; // this will freeze the game, stop
// you can also use the following if you want the game to exit
//Application.Quit();
//or reload current level
//Application.LoadLevel(Application.loadedLevel);
}
}
}
You can remove the rigidbody from your "lava" cubes, it only needs a collider. So this script as it is will stop time in your script, nothing will move ( but you will be able to move the camera) if you just one to restart, then use last commented code instead with the loadlevel.
If you ahve any other question just ask.
Don't confuse him too much he just needs how to stop the time.
learning basics is great profit.
I didn't want to confuse him, it just wasn't clear what he wants. This things I wrote is basic stuff, nothing fancy and I commented out what they do, so he can chose what behavior he wants...
Thx for the script. And explaining what the different things does. I added a Health variable, Enemy tag to my enemys, and my enemy and charachter booth have colliders, but i does not seem to work.
When you say ''collider'' do you mean a normal collider like a box or sphere collider?
Just a silly question did you add rigidbody to your main character (and or to your enemyies?) and did you add the script to your character? You need at least one rigidbody for the collision to happen.
I did add a ridgidbody to at least one of them. and i also added the script to my character. $$anonymous$$y ''enemy'' is actually some rising lava, but right now im just using a simple box as an enemy and the character controller. Does it work for you?
Answer by anurag03062001 · Feb 22, 2020 at 03:37 PM
//attach this script to "lava " //kubo is palyer name
void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.name == "kubo") {
Destroy(col.gameObject);
}
}