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
0
Question by GhillieMonsters · Jan 07, 2013 at 06:52 PM · deathhealth

Death upon health = 0

Any help would be appreciated, we are trying to figure the best way to create a death; whether the app should just close, or a death room, anything really. We are having a very difficult time working through this issue and any help would be appreciated; here is our health script;

 var fullHealth : int = 100; 
 
 var curHealth : int = 100;  
 
 function OnCollisionEnter(collision: Collision) 
 {
     switch (collision.gameObject.tag)
 
 {case "Enemy" : 
 
         curHealth -= 1; 
         print ("hit");
         break;
 
         case "heal" : 
         curHealth += 50; 
         print ("got health");
         break;
         
     }
 }
 
 function Update () 
 {
 
     if(curHealth >= fullHealth)
     { 
         curHealth = fullHealth; 
     }
 
     if(curHealth <= 0)
     {
          curHealth = 0; Debug.Log("You Died");
     }
 }
 
 function OnGUI()
 {
     GUI.Label (Rect (55, 625, 200, 40),"" +curHealth.ToString()); 
 } 
Comment
Add comment · Show 2
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 Piflik · Jan 08, 2013 at 09:02 PM 0
Share

Not an answer to the question, just a small tip: You should move the death check (and the one for more than full health) also into OnCollisionEnter, since this is where the health variable is modified and there is no need fo dooing two if statements every frame. Not a big hit on performance (probably not even noticeable), but if you ever do more complex calculations this might be different.

avatar image GhillieMonsters · Jan 09, 2013 at 06:44 PM 0
Share

Thank you for the advice any where we can pull even a little performance power makes a difference.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Geo.Ego · Jan 07, 2013 at 07:13 PM

It depends on what you want to do when your player dies. Do you want the app to close? That doesn't seem likely. When you say "death room", I assume you mean a place that you would go to after the player dies. In this case, you would create a new Scene (File > New Scene), call it "Death Room" or something similar, and where you call Debug.Log("You Died");, instead call this:

 Application.LoadLevel ("Death Room");

Similarly, if you'd like to just restart the level, you can call:

 Application.LoadLevel (Application.loadedLevel);

Basically, whatever you decide, you just need to use Application.LoadLevel to move your player to the proper place.

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
avatar image
0

Answer by Karsnen_2 · Jan 07, 2013 at 07:17 PM

GhilleMonsters : I do not fully understand your need.

You seem to ask for a design question but then you have your script here. Let me tell you from what I have understood.

On Coding-wise :

I do not see any issue with your code though. It seems good.

On Design-wise :

When the player runs out of health, I do not think you should "close the app". It is offensive. You should cut this screen and take the user to another screen where he could see his result. Or you could just stay in the same screen and initiate a pop-up.

Combing Both : I am not good in UnityScript, so here is a equivalent C# script for you.

 using UnityEngine;
 using System.Collections;
  
 public class HealthController: MonoBehaviour 
 {
     
     int CurrentHealth, FullHealth;
     
     void Awake ()
     {
         CurrentHealth = 100;
         FullHealth = 100;
     }
     
     void OnCollisionEnter (Collision hit)
     {
         if(hit.collider.tag == "Enemyy")
         {
             CurrentHealth--;
             Debug.Log("It is a hit");
             
         }
         
         if(hit.collider.tag == "heal")
         {
             CurrentHealth += 50;
             Debug.Log("Health Obtained");
         }
     }
     
     
     void Update ()
     {
         if(CurrentHealth >= FullHealth)
             CurrentHealth = FullHealth;
         
         if(CurrentHealth <= 0)
         {
             CurrentHealth = 0;
             Debug.Log("You Died");
             
             /*
              // Here you can do lots of things, such as 
              
               Application.LoadLevel("FinishScreen");
               
              // Display a UI to show the result to player and give him options, such as 
              
               /// Restart the game
               ///  Quita the Game
               /// Main Menu
              */
         }
         
     }
 }



Hope this helps you. If not, buzz me I will see to provide more help from your scenario.

Cheers!!!

Comment
Add comment · Show 5 · 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 GhillieMonsters · Jan 08, 2013 at 06:08 PM 0
Share

Thank you very much for your help, I was able to build a respawn point into the existing java script.

avatar image Karsnen_2 · Jan 08, 2013 at 07:43 PM 0
Share

Okay so did you just build upon the javascript you have? If so, can you share?

avatar image GhillieMonsters · Jan 09, 2013 at 06:43 PM 0
Share

I replaced the existing health = 0 order which was to send the word death out across the log; I replaced it with a statment that says when the health is 0 load level 0 (our main menu).

avatar image Karsnen_2 · Jan 10, 2013 at 02:26 PM 0
Share

Okay if you find this answer, anyway helpful - just give me an upvote:)

avatar image Geo.Ego · Jan 10, 2013 at 04:48 PM 1
Share

@Ghillie$$anonymous$$onsters: You should mark one of these answers as the correct answer, or submit one of your own and mark it as an answer. It's a big help to the community.

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

11 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

Related Questions

Multiple Cars not working 1 Answer

My health decreases to fast. 1 Answer

Helpppp - AnimationState.layer ?????? -1 Answers

How to add colliders to Tron Like Trail 0 Answers

Very simple inventory script... 0 Answers


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