- Home /
Need help with player health
I'm creating an fps and I can't find a C# script to make my player lose health when he hits a fireball(enemy bullet). incase you need it heres my healthbar script
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour
{
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLenght;
// Use this for initialization
void Start ()
{
healthBarLenght = Screen.width / 2;
}
//Update is called once per frame
void Update()
{
AddJustCurrentHealth(0);
}
void OnGUI()
{
GUI.Box(new Rect(10, 10, healthBarLenght, 20), curHealth + "/" + maxHealth);
}
public void AddJustCurrentHealth(int adj)
{
curHealth += adj;
healthBarLenght = (Screen.width / 2) * (curHealth /(float)maxHealth);
}
}
and if you can include info about re-spawn
Answer by Daryn · Aug 12, 2012 at 05:54 PM
I would try something along the lines of using a trigger.
so
void OnTriggerEnter () {
curHealth = curHealth -1;
}
// this would make it so when ever it is triggered the curHealth will subtract 1
then attach a script to the fire ball.
void OnTriggerEnter () {
gameObject.active = false;
}
// this will make it disapear when triggered also to make something triggered just check mark the box on the collider component.
void Update () {
if(curHealth == 0){
Transform.localPosition = new Vector3(x,y,z,),Quaternion.Identifier;
curHealth = maxHealth;
}
}
// this is for the respawn just type in the x y and z coordinates you want the character to respawn at.
Answer by aldonaletto · Aug 12, 2012 at 02:07 PM
A common approach is to call the function AddjustCurrentHealth in your script when something hits the player. If a fireball collides with the player, you can use something like this in the fireball script (the fireball must be a rigidbody):
void OnCollisionEnter(Collision col){ // call the function AddJustCurrentHealth(-5) in the target object: col.transform.SendMessage("AddJustCurrentHealth", -5, SendMessageOptions.DontRequireReceiver) // destroy the fireball: Destroy(gameObject); }The instruction SendMessage actually doesn't send anything - it searches in the referenced object's scripts for a function with the specified name and call it (if any).
You should handle respawning in AddJustCurrentHealth: when the health becomes <= 0, play some death effect and decide whether the player can respawn or not (based on life count, for instance). To respawn the player just restore curHealth to maxHealth, move the player to the respawning point and play some respawning effect.
A simple way to add this to your script is as follows:
... // Add these variables: // create an empty object at the respawning point and drag it here: public Transform respawningPoint; public int curLives = 3; // life count public Texture2D blackTexture; // drag a black texture here public float fadeTime = 2; // fade duration float fadeLevel = 0; // fade control: 0 = no fade, 1 = black screen
void OnGUI()
{
GUI.Box(new Rect(10, 10, healthBarLenght, 20), curHealth + "/" + maxHealth);
// fade the screen to black according to fadeLevel:
if (fadeLevel > 0){
GUI.color.a = fadeLevel;
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), blackTexture);
}
}
public void AddJustCurrentHealth(int adj)
{
curHealth += adj;
healthBarLenght = (Screen.width / 2) * (curHealth /(float)maxHealth);
if (curHealth <= 0){
StartCoroutine(Die());
}
}
IEnumerator Die(){
curLives -= 1; // decrement life count
// death effect: fade screen to black during fadeTime
while (fadeLevel < 1){
fadeLevel += Time.deltaTime / fadeTime;
yield return 0;
}
// if player can respawn:
if (curLives > 0){
transform.position = respawningPoint.position;
transform.rotation = respawningPoint.rotation;
curHealth = maxHealth; // restore health
// respawn effect: fade screen in
while (fadeLevel > 0){
fadeLevel -= Time.deltaTime / fadeTime;
yield return 0;
}
} else {
// game over code
}
}
Your answer
Follow this Question
Related Questions
Scoring System with Networking 1 Answer
Attach GUI element to newly spawned player as their GUI 2 Answers
Creating 2d Health Bars 2 Answers
How to edit player health based on the player level 2 Answers