You are viewing our Forum Archives. To view or take place in current topics click here.
Simple Player & Enemy Health Script (C#)
Posted:

Simple Player & Enemy Health Script (C#)Posted:

GameDev13
  • New Member
Status: Offline
Joined: Jul 17, 201310Year Member
Posts: 14
Reputation Power: 0
Status: Offline
Joined: Jul 17, 201310Year Member
Posts: 14
Reputation Power: 0
I recently found one of my old projects for a game and thought i would share some of the scripts i have found. Here is a player and enemy health script in C#

Player Health:
using UnityEngine;
using System.Collections;


public class PlayerHealth : MonoBehaviour {
   
   
   public int maxHealth = 100;
   public int curHealth = 100;
   
   public float healthBarLength;
   
   // Use this for initialization
   void Start () {
      healthBarLength = Screen.width / 2;
   }
   
   // Update is called once per frame
   void Update () {
      AdjustCurrentHealth(0);
      }
   
   void OnGUI() {
      
      GUI.Box (new Rect (10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
      
      
   }
   
   
   public void AdjustCurrentHealth(int adj) {
      curHealth += adj;

      if(curHealth < 0)
             curHealth = 0;

         if (curHealth > maxHealth)
           curHealth  = maxHealth;
      
      if (maxHealth < 1)
         maxHealth = 1;   
      
      healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
      
      
   }
}


Enemy Health:

using UnityEngine;
using System.Collections;


public class EnemyHealth : MonoBehaviour {
   
   
   public int maxHealth = 100;
   public int curHealth = 100;
   
   public float healthBarLength;
   
   // Use this for initialization
   void Start () {
      healthBarLength = Screen.width / 2;
   }
   
   // Update is called once per frame
   void Update () {
      AdjustCurrentHealth(0);
      }
   
   void OnGUI() {
      
      GUI.Box (new Rect (10, 40, healthBarLength, 20), curHealth + "/" + maxHealth);
      
      
   }
   
   
   public void AdjustCurrentHealth(int adj) {
      curHealth += adj;

      if(curHealth < 0)
             curHealth = 0;

         if (curHealth > maxHealth)
           curHealth  = maxHealth;
      
      if (maxHealth < 1)
         maxHealth = 1;   
      
      healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
      
      
   }
}



This uses a simple box which changes size as you use health, Very simple script as i never worked on it (Focusing on a character generation script). This might be some use to the noobs but to the good coders, Probably no use at all :p
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.