- Home /
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());
}
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.
Thank you for the advice any where we can pull even a little performance power makes a difference.
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.
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!!!
Thank you very much for your help, I was able to build a respawn point into the existing java script.
Okay so did you just build upon the javascript you have? If so, can you 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).
Okay if you find this answer, anyway helpful - just give me an upvote:)
@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
Follow this Question
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