Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by NinjaRubberBand · Nov 19, 2013 at 04:13 PM · enemy

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.!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 15 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sdgd · Nov 19, 2013 at 04:36 PM 0
Share

Don't confuse him too much he just needs how to stop the time.

learning basics is great profit.

avatar image gajdot · Nov 19, 2013 at 04:52 PM 0
Share

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...

avatar image NinjaRubberBand · Nov 19, 2013 at 05:02 PM 0
Share

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?

avatar image gajdot · Nov 19, 2013 at 07:28 PM 0
Share

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.

avatar image NinjaRubberBand · Nov 19, 2013 at 08:35 PM 0
Share

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?

Show more comments
avatar image
0

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);
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to make an enemy change tags after death. 3 Answers

how can i set target(gameobject) from string(name) 1 Answer

How to stop enemy shooting through wall 1 Answer

How to update scoreline on a HIT? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges