Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink
Share
Go down
Member [Lv. 3]
Member [Lv. 3]
Posts : 37
Points : 1670
Rep : 19
Join date : 2014-06-14
View user profile

Health Bar

on Thu Sep 04, 2014 4:32 pm
Message reputation : 79% (14 votes)
This is a health bar script attach it to your player and fill out the texture variables in the inspector and modify some of the lines according to your game.
Code:

#pragma strict

//Size of Textures

var size : Vector2 = new Vector2(240, 40);

//Health Variables
var healthPos : Vector2 = new Vector2(20, 20);
var healthBarDisplay : float = 1;
var healthBarEmpty : Texture2D;
var healthBarFull : Texture2D;

//Hunger Variables
var hungerPos : Vector2 = new Vector2(20, 60);
var hungerBarDisplay : float = 1;
var hungerBarEmpty : Texture2D;
var hungerBarFull : Texture2D;

//Thirst Variables
var thirstPos : Vector2 = new Vector2(20, 100);
var thirstBarDisplay : float = 1;
var thirstBarEmpty : Texture2D;
var thirstBarFull : Texture2D;

//Stamina Variables
var staminaPos : Vector2 = new Vector2(20, 140);
var staminaBarDisplay : float = 1;
var staminaBarEmpty : Texture2D;
var staminaBarFull : Texture2D;

//Fall Rate
var healthFallRate : int = 150;
var hungerFallRate : int = 150;
var thirstFallRate : int = 100;
var staminaFallRate : int = 35;

private var chMotor : CharacterMotor;
private var controller : CharacterController;

var canJump : boolean = false;

var jumpTimer : float = 0.7;

function Start()
{
   chMotor = GetComponent(CharacterMotor);
   controller = GetComponent(CharacterController);
}

function OnGUI()
{
   //Health GUI
   GUI.BeginGroup(new Rect (healthPos.x, healthPos.y, size.x, size.y));
   GUI.Box(Rect(0, 0, size.x, size.y), healthBarEmpty);
   
   GUI.BeginGroup(new Rect (0, 0, size.x * healthBarDisplay, size.y));
   GUI.Box(Rect(0, 0, size.x, size.y), healthBarFull);
   
   GUI.EndGroup();
   GUI.EndGroup();
   
   //Hunger GUI
   GUI.BeginGroup(new Rect (hungerPos.x, hungerPos.y, size.x, size.y));
   GUI.Box(Rect(0, 0, size.x, size.y), hungerBarEmpty);
   
   GUI.BeginGroup(new Rect (0, 0, size.x * hungerBarDisplay, size.y));
   GUI.Box(Rect(0, 0, size.x, size.y), hungerBarFull);
   
   GUI.EndGroup();
   GUI.EndGroup();
   
   //Thirst GUI
   GUI.BeginGroup(new Rect (thirstPos.x, thirstPos.y, size.x, size.y));
   GUI.Box(Rect(0, 0, size.x, size.y), thirstBarEmpty);
   
   GUI.BeginGroup(new Rect (0, 0, size.x * thirstBarDisplay, size.y));
   GUI.Box(Rect(0, 0, size.x, size.y), thirstBarFull);
   
   GUI.EndGroup();
   GUI.EndGroup();
   
   //Stamina GUI
   GUI.BeginGroup(new Rect (staminaPos.x, staminaPos.y, size.x, size.y));
   GUI.Box(Rect(0, 0, size.x, size.y), staminaBarEmpty);
   
   GUI.BeginGroup(new Rect (0, 0, size.x * staminaBarDisplay, size.y));
   GUI.Box(Rect(0, 0, size.x, size.y), staminaBarFull);
   
   GUI.EndGroup();
   GUI.EndGroup();
}

function Update()
{
   //HEALTH CONTROL SECTION
   if(hungerBarDisplay <= 0 && (thirstBarDisplay <= 0))
   {
      healthBarDisplay -= Time.deltaTime / healthFallRate * 2;
   }
   
   else
   {
      if(hungerBarDisplay <= 0 || thirstBarDisplay <= 0)
      {
         healthBarDisplay -= Time.deltaTime / healthFallRate;
      }
   }
   
   if(healthBarDisplay <= 0)
   {
      CharacterDeath();
   }
   
   //HUNGER CONTROL SECTION
   if(hungerBarDisplay >= 0)
   {
      hungerBarDisplay -= Time.deltaTime / hungerFallRate;
   }
   
   if(hungerBarDisplay <= 0)
   {
      hungerBarDisplay = 0;
   }
   
   if(hungerBarDisplay >= 1)
   {
      hungerBarDisplay = 1;
   }
   
   //THIRST CONTROL SECTION
   if(thirstBarDisplay >= 0)
   {
      thirstBarDisplay -= Time.deltaTime / thirstFallRate;
   }
   
   if(thirstBarDisplay <= 0)
   {
      thirstBarDisplay = 0;
   }
   
   if(thirstBarDisplay >= 1)
   {
      thirstBarDisplay = 1;
   }
   
   //STAMINA CONTROL SECTION
   if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
   {
      chMotor.movement.maxForwardSpeed = 10;
      chMotor.movement.maxSidewaysSpeed = 10;
      staminaBarDisplay -= Time.deltaTime / staminaFallRate;
   }
   
   else
   {
      chMotor.movement.maxForwardSpeed = 6;
      chMotor.movement.maxSidewaysSpeed = 6;
      staminaBarDisplay += Time.deltaTime / staminaFallRate;
   }
   
   //JUMPING SECTION
   if(Input.GetKeyDown(KeyCode.Space) && canJump == true)
   {
      staminaBarDisplay -= 0.2;
      Wait();
   }
   
   if(canJump == false)
   {
      jumpTimer -= Time.deltaTime;
      chMotor.jumping.enabled = false;
   }
   
   if(jumpTimer <= 0)
   {
      canJump = true;
      chMotor.jumping.enabled = true;
      jumpTimer = 0.7;
   }
   
   //COMMENTED THESE SECTIONS OUT - UPDATED 16/07/14
   //if(staminaBarDisplay <= 0.05)
   //{
      //canJump = false;
      //chMotor.jumping.enabled = false;
   //}
   
   //else
   //{
      //canJump = true;
      //chMotor.jumping.enabled = true;
   //}
   
   if(staminaBarDisplay >= 1)
   {
      staminaBarDisplay = 1;
   }
   
   if(staminaBarDisplay <= 0)
   {
      //ADDED line 181 here!
      staminaBarDisplay = 0;
      canJump = false;
      chMotor.jumping.enabled = false;
      chMotor.movement.maxForwardSpeed = 6;
      chMotor.movement.maxSidewaysSpeed = 6;
   }
}

function CharacterDeath()
{
   Application.LoadLevel("SIMPLELEVEL");
}

function Wait()
{
   yield WaitForSeconds(0.1);
   canJump = false;
}

avatar
Admin
Admin
Posts : 608
Points : 3198
Rep : 406
Join date : 2013-12-11
Age : 21
View user profilehttps://www.youtube.com/user/misterninjaboy

Re: Health Bar

on Thu Sep 04, 2014 6:04 pm
Nice to see you helping out the community with these scripts! look forward to see more from you!
avatar
Moderator
Moderator
Posts : 76
Points : 1955
Rep : 16
Join date : 2014-06-02
Age : 15
Location : Milky Way
View user profile

Re: Health Bar

on Fri Sep 05, 2014 5:47 pm
Cool Script... Check out my tutorial its very helpful...... Mad Laughing Laughing Laughing Laughing
Member [Lv. 3]
Member [Lv. 3]
Posts : 37
Points : 1670
Rep : 19
Join date : 2014-06-14
View user profile

Re: Health Bar

on Fri Sep 05, 2014 10:34 pm
wooden_sword789 wrote:Cool Script... Check out my tutorial its very helpful...... Mad Laughing Laughing Laughing Laughing

Thanks but I don't use JS anymore
avatar
Moderator
Moderator
Posts : 76
Points : 1955
Rep : 16
Join date : 2014-06-02
Age : 15
Location : Milky Way
View user profile

Re: Health Bar

on Sun Sep 07, 2014 3:02 pm
the enum still works for c#
but the if() doesnt
Member [Lv. 1]
Member [Lv. 1]
Posts : 2
Points : 1192
Rep : 0
Join date : 2015-05-18
View user profile

Re: Health Bar

on Tue May 19, 2015 9:31 pm
wooden_sword789 wrote:Cool Script... Check out my tutorial its very helpful...... Mad Laughing Laughing Laughing Laughing
can u give me the link to it?the video plz im having trouble adding it
Member [Lv. 0]
Member [Lv. 0]
Posts : 1
Points : 1029
Rep : 0
Join date : 2015-10-26
View user profile

Re: Health Bar

on Mon Oct 26, 2015 3:17 am
Hi man i get the error: CharacterMotor is invalid type (not found) did you mean "UnityEngine.CharacterJoint"?
avatar
{SUPER MEMBER}-LV.4
{SUPER MEMBER}-LV.4
Posts : 55
Points : 1560
Rep : 11
Join date : 2015-01-18
Age : 41
Location : Italia
View user profile

This is the C # version for unity2018

on Thu Jul 26, 2018 6:39 am
Ok you have to edit the FirstPersonController script
and change the variables from private to public

  [SerializeField] public float m_WalkSpeed;
  [SerializeField] public float m_RunSpeed;
  public bool m_Jump;
Code:


/*------------------------------------------------------------------------------------------------
 * Conversion in C# by Digital_Pitbull 2018
 * Adventure GUI
--------------------------------------------------------------------------------------------------*/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityStandardAssets.Characters.FirstPerson;

public class Healthbar : MonoBehaviour {


    //Size of Textures

    public Vector2 size = new Vector2(240, 40);

    //Health Variables
    public Vector2 healthPos= new Vector2(20, 20);
    public float healthBarDisplay  = 1;
    public Texture2D healthBarEmpty;
    public Texture2D healthBarFull;

    //Hunger Variables
    public  Vector2 hungerPos = new Vector2(20, 60);
    public float hungerBarDisplay = 1;
    public  Texture2D hungerBarEmpty;
    public Texture2D hungerBarFull;

    //Thirst Variables
    public  Vector2 thirstPos= new Vector2(20, 100);
    public float thirstBarDisplay  = 1;
    public Texture2D thirstBarEmpty;
    public Texture2D thirstBarFull;

    //Stamina Variables
    public Vector2 staminaPos= new Vector2(20, 140);
    public float staminaBarDisplay = 1;
    public Texture2D staminaBarEmpty;
    public Texture2D staminaBarFull;

    //Fall Rate
    public int healthFallRate  = 150;
    public int hungerFallRate  = 150;
    public int thirstFallRate  = 100;
    public int staminaFallRate  = 35;

    FirstPersonController chMotor;
    CharacterController controller;

    public  bool canJump = false;

    public float jumpTimer = 0.7f;

    void Start()
    {
        chMotor = GetComponent<FirstPersonController>();
        controller = GetComponent<CharacterController>();
    }

    void OnGUI()
    {
        //Health GUI
        GUI.BeginGroup(new Rect(healthPos.x, healthPos.y, size.x, size.y));
        GUI.Box(new Rect(0, 0, size.x, size.y), healthBarEmpty);

        GUI.BeginGroup(new Rect(0, 0, size.x * healthBarDisplay, size.y));
        GUI.Box(new Rect(0, 0, size.x, size.y), healthBarFull);

        GUI.EndGroup();
        GUI.EndGroup();

        //Hunger GUI
        GUI.BeginGroup(new Rect(hungerPos.x, hungerPos.y, size.x, size.y));
        GUI.Box(new Rect(0, 0, size.x, size.y), hungerBarEmpty);

        GUI.BeginGroup(new Rect(0, 0, size.x * hungerBarDisplay, size.y));
        GUI.Box(new Rect(0, 0, size.x, size.y), hungerBarFull);

        GUI.EndGroup();
        GUI.EndGroup();

        //Thirst GUI
        GUI.BeginGroup(new Rect(thirstPos.x, thirstPos.y, size.x, size.y));
        GUI.Box(new Rect(0, 0, size.x, size.y), thirstBarEmpty);

        GUI.BeginGroup(new Rect(0, 0, size.x * thirstBarDisplay, size.y));
        GUI.Box(new Rect(0, 0, size.x, size.y), thirstBarFull);

        GUI.EndGroup();
        GUI.EndGroup();

        //Stamina GUI
        GUI.BeginGroup(new Rect(staminaPos.x, staminaPos.y, size.x, size.y));
        GUI.Box(new Rect(0, 0, size.x, size.y), staminaBarEmpty);

        GUI.BeginGroup(new Rect(0, 0, size.x * staminaBarDisplay, size.y));
        GUI.Box(new Rect(0, 0, size.x, size.y), staminaBarFull);

        GUI.EndGroup();
        GUI.EndGroup();
    }

    void Update()
    {
        //HEALTH CONTROL SECTION
        if (hungerBarDisplay <= 0 && (thirstBarDisplay <= 0))
        {
            healthBarDisplay -= Time.deltaTime / healthFallRate * 2;
            
        }

        else
        {
            if (hungerBarDisplay <= 0 || thirstBarDisplay <= 0)
            {
                healthBarDisplay -= Time.deltaTime / healthFallRate;
            }
        }

        if (healthBarDisplay <= 0)
        {
            CharacterDeath();
        }

        //HUNGER CONTROL SECTION
        if (hungerBarDisplay >= 0)
        {
            hungerBarDisplay -= Time.deltaTime / hungerFallRate;
        }

        if (hungerBarDisplay <= 0)
        {
            hungerBarDisplay = 0;
        }

        if (hungerBarDisplay >= 1)
        {
            hungerBarDisplay = 1;
        }

        //THIRST CONTROL SECTION
        if (thirstBarDisplay >= 0)
        {
            thirstBarDisplay -= Time.deltaTime / thirstFallRate;
        }

        if (thirstBarDisplay <= 0)
        {
            thirstBarDisplay = 0;
        }

        if (thirstBarDisplay >= 1)
        {
            thirstBarDisplay = 1;
        }

        //STAMINA CONTROL SECTION
        if (controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
        {
            chMotor.m_RunSpeed = 10;
            staminaBarDisplay -= Time.deltaTime / staminaFallRate;
        }

        else
        {
            chMotor.m_WalkSpeed = 6
            staminaBarDisplay += Time.deltaTime / staminaFallRate;
        }

        //JUMPING SECTION
        if (Input.GetKeyDown(KeyCode.Space) && canJump == true)
        {
            staminaBarDisplay -= 0.2f;
            Wait();
        }

        if (canJump == false)
        {
            jumpTimer -= Time.deltaTime;
            chMotor.m_Jump = false;
        }

        if (jumpTimer <= 0)
        {
            canJump = true;
            chMotor.m_Jump = true;
            jumpTimer = 0.7f;
        }


        if (staminaBarDisplay >= 1)
        {
            staminaBarDisplay = 1;
        }

        if (staminaBarDisplay <= 0)
        {
            //ADDED line 181 here!
            staminaBarDisplay = 0;
            canJump = false;
            chMotor.m_Jump= false;
            chMotor.m_WalkSpeed = 6;
            //chMotor.movement.maxSidewaysSpeed = 6;
        }
    }

    void CharacterDeath()
    {

        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    IEnumerator Wait()
    {
       yield return new WaitForSeconds(0.1f);
        canJump = false;
    }

}

I hope you like it lol!
Back to top
Permissions in this forum:
You cannot reply to topics in this forum