- Home /
How to make a player die after health reaches 0
hey guys was just wondering how do i make my player die and respawn after the healthbar reaches 0
This is the code: using UnityEngine; using System.Collections;
public class PlayerHealth : MonoBehaviour { public int maxHealth = 100; public int curHealth = 100;
public float healthBarLengh;
// Use this for initialization
void Start () {
healthBarLengh = Screen.width / 2;
}
// update is called once per frame
void update () {
AddjustCurrentHealth(0);
}
void OnGUI() {
GUI.Box(new Rect(10, 40, healthBarLengh, 20), curHealth + "/" + maxHealth);
}
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLengh = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
Answer by EliteMossy · Apr 04, 2013 at 05:54 AM
Here is the fixed script.
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLengh;
// Use this for initialization
void Start () {
healthBarLengh = Screen.width / 2;
}
// update is called once per frame
private void update () {
AddjustCurrentHealth(0);
}
private void OnGUI() {
GUI.Box(new Rect(10, 40, healthBarLengh, 20), curHealth + "/" + maxHealth);
}
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth < 0) {
KillPlayer();
}
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLengh = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
public void KillPlayer() {
// Play death animation etc.
// Restart level or change position of player
}
}
Sorry just edited, realized you completely screwed it up :D
Thanks so much! Working fine! can probably tell im not the programmer in the project, one last question! how would i go about getting the player to respawn?
Cool, don't forget to mark the correct answer as an answer. Also to respawn, you just instanitate from a prefab which you can drop in to your script.
Add this to the top of your script underneath public int curHealth = 100;
public GameObject PlayerPrefab;
Assign the prefab of the player to the Inspector in Unity.
Now change your $$anonymous$$illPlayer code to this:
public void $$anonymous$$illPlayer() {
Instaniate(PlayerPrefab, transform, rotation);
Destroy(gameObject);
}
Should work, but i would look at some other resources. This is not the way i would do it, and not 100% sure if it is a good way.
Answer by save · Apr 03, 2013 at 10:54 AM
You already have the (curHealth < 0) statement, call for a function inside that.
if(curHealth < 0) {
curHealth = 0;
KillPlayer();
}
public void KillPlayer() {
// Play death animation etc.
// Restart level or change position of player
}
To respawn you could just create a prefab of you player and Instantiate a new GameObject at some waypoint. http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
$$anonymous$$eep getting a 'Unexpected Symbol 'public' nothing seems to fix it
public class PlayerHealth : $$anonymous$$onoBehaviour { public int maxHealth = 100; public int curHealth = 100;
public float healthBarLengh;
//} you need a bracket right here
// Use this for initialization
void Start () {
healthBarLengh = Screen.width / 2;
}
Answer by joeyaaaaa · Apr 04, 2013 at 04:46 AM
Actually i think you are declaring new pubs(vars) in a wrong place(ithink)but just add that bracket i placed within my comments where i put it if it still doesnt work move those publics to the beginning of script or add the bracket to the end, sprry its hard to tell whats wrong or where brackets go sometimes edit: i read wrong sorry i read"how would u fix it lmao any ways im sure its where your publics
Yeah, but he needs to post his full script so we can see.
using UnityEngine;
using System.Collections;
public class PlayerHealth : $$anonymous$$onoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLengh;
// Use this for initialization
void Start () {
healthBarLengh = Screen.width / 2;
}
// update is called once per frame
void update () {
AddjustCurrentHealth(0);
}
void OnGUI() {
GUI.Box(new Rect(10, 40, healthBarLengh, 20), curHealth + "/" + maxHealth);
}
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth < 0) {
curHealth = 0;
$$anonymous$$illPlayer();
}
public void $$anonymous$$illPlayer() {
// Play death animation etc.
// Restart level or change position of player
}
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLengh = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
ya the bracket i told you to add probably shouldnt be there, i know pretty much only java but i see public class PlayerHealth : $$anonymous$$onoBehaviour {
should connect to a bracket at the end of your script, i believe so and i believe these>
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLengh; should come first but after this>using UnityEngine;
using System.Collections;
again i am unsure
but i am sure that
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth < 0) {
curHealth = 0;
$$anonymous$$illPlayer();
}
you need another bracket at the end of this function^^^^^^^^^^^^
public void $$anonymous$$illPlayer() {
// Play death animation etc.
// Restart level or change position of player
/*}
}*/
^^^^^^and for now remove these two brackets, they are stopping the code from seeing the restVVVVVVVVVVVVV
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLengh = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
health bar script please help 2 Answers
Damage taking? 1 Answer
Having trouble with player.transform 1 Answer
Unity Effect 1 Answer