Now then, let's get started.

1. Open the Play scene which you had created in the previous post. If you've not created the Play scene, create a New Scene and save it as Play, of course you can name it whatever you want.

2. Once you open the scene, you should have a Main Camera, I would suggest you toPosition it at (0, 1, -15) 

3. Add a Directional Light to the scene by navigating to Gameobject->Light->Directional Light. Position it at (0, 1, -15) as well.

4. Add a Cube to the scene from Gameobject->3D Object->Cube. Rename it as Player. Position it at (-1, 1, -10). Add a Rigidbody component to this Player and uncheck the Use Gravity checkbox.

 5. Create another Cube object and rename it as Fire. Position it at (2, 1, -10). Check the Is Trigger checkbox of the Fire's Box Collider component.

You can add a material to this Fire if you want, just like I did.

6. It is now time to add a Text UI component named Health, to indicate a Health Bar HUD.

7. Place the text so that it is visible on the top left corner of the Game screen. Make sure you move the anchor points with the text as well


The above image shows you the things that I configured. You can follow it if you like or you can configure it the way you want.
1 signifies the position of the Health text
I have renamed the Text to HealthText.
Signifies the Position of the HelathText Rect Transform and also it's Anchor's min maxpositions. Also the Text content is changed to Health.
4 Best Fit is checked so as to make the Text dynamic. Max and Min size are set. Color of the font has been changed to White.
(Overlook the error in the console)

8. Create another Text element under the Canvas named GameOver. Position it wherever you want to with the anchor points placed at the four corners. Also check the Best FitcheckboxChange the font color if you want to.

   
9. It is time to add a Slider, which will be used to indicate the Health of the PlayerPlace it besides the Health text and resize it as per your needs with the anchors placed around the corners.

10. We will disable the slider handle as we don't need it.

11. Change the Slider Fill color to Green

12. Create a C# script named MovementScript in the Scripts folder. Attach it to the PlayerGameObject. Open this script and add the below code to it.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MovementScript : MonoBehaviour { public Slider healthBarSlider; //reference for slider
public Text gameOverText; //reference for text
private bool isGameOver = false; //flag to see if game is over void Start(){
gameOverText.enabled = false; //disable GameOver text on start
} // Update is called once per frame
void Update () {
//check if game is over i.e., health is greater than 0
if(!isGameOver)
transform.Translate(Input.GetAxis("Horizontal")*Time.deltaTime*10f, 0, 0); //get input
} //Check if player enters/stays on the fire
void OnTriggerStay(Collider other){
//if player triggers fire object and health is greater than 0
if(other.gameObject.name=="Fire" && healthBarSlider.value>0){
healthBarSlider.value -=.011f; //reduce health
}
else{
isGameOver = true; //set game over to true
gameOverText.enabled = true; //enable GameOver text
}
}
}

- In the Start function we disable the GameOver text as we need it to be displayed only when the health is zero.
- The code above is very simple. The Update function gets input from the keyboard as long as the isGameOver flag is false. Press Right Arrow to move the Player to Right and Left Arrow to move it Left
OnTriggerStay function checks for contact between the Player and Fire object as long as the slider value is greater than zero. If the Player is in contact with the Fire, it's health will reduce at a rate of 0.01 units/frame. This value is reflected on the Slider. If the health value is zero, we will enable the GameOver text.
(Note: The slider value is set to 1 by default)

  
13. Save the script and return to Unity. Add the Slider and GameOver text elements to the script reference fields as below:

Now, your Health Bar HUD is all ready to be tested. Press the Play button to test your scene. Use the left and right arrow keys to move the Player.

See you around.

Also check out,
Unity 4.6 New GUI - Create a Blinking / Flashing Text

Dynamic Canvas Using Canvas Scaler / Reference Resolution - New Unity 4.6 GUI

Unity 4.6 GUI - Create A Dynamic Menu With The New UI

Unity 4.6 GUI - Create A Health Bar HUD

Unity 4.6 GUI - Create A Level Select Scroll Menu

Unity 4.6 GUI - Create An Animated Menu

Unity 4.6 GUI - Create A Level Lock/Unlock System

CREATE A ENERGY / HEALTH BAR HUD的更多相关文章

  1. Create a “% Complete” Progress Bar with JS Link in SharePoint 2013

    Create a “% Complete” Progress Bar with JS Link in SharePoint 2013 SharePoint 2013 has a lot new fea ...

  2. how to create a ring progress bar in web skills

    how to create a ring progress bar in web skills ring progress bar & circle progress bar canvas j ...

  3. UI相关教程:HUD、UMG和Widget

    转自:http://aigo.iteye.com/blog/2258612 蓝图脚本来处理 ================================================== 用UM ...

  4. 12岁的少年教你用Python做小游戏

    首页 资讯 文章 频道 资源 小组 相亲 登录 注册       首页 最新文章 经典回顾 开发 设计 IT技术 职场 业界 极客 创业 访谈 在国外 - 导航条 - 首页 最新文章 经典回顾 开发 ...

  5. Ceph常用命令

    目录 [1.环境准备] [2.部署管理] [3.集群扩容] [4.用户管理] [5.密钥环管理] [6.块设备管理] [7.快照管理] [8.参考链接] 简要说明: 最近心血来潮,对分布式存储感兴趣, ...

  6. PIXI 宝物猎人(7)

    介绍 ,本实例来自官网 代码结构 打开 treasureHunter.html 文件,你将会看到所有的代码都在一个大的文件里.下面是一个关于如何组织所有代码的概览: //Setup Pixi and ...

  7. Unity 2018 Cookbook (Matt Smith 著)

    1. Displaying Data with Core UI Elements (已看) 2. Responding to User Events for Interactive UIs (已看) ...

  8. Python制作的射击游戏

    如果其他朋友也有不错的原创或译文,可以尝试推荐给伯乐在线.] 你有没有想过电脑游戏是怎样制作出来的?其实它没有你想象的那样复杂! 在这个教程里,你要学做一个叫<兔子和獾>的塔防游戏,兔子作 ...

  9. Website's Game source code

    A Darkroom by doublespeakgames <!DOCTYPE html> <html itemscope itemtype="https://schem ...

随机推荐

  1. 统计《ASP.Net特供视频教程》总长度

    忽然想统计一下我录制过的视频一共多长时间,由于视频文件很多,一共72个,挨个打开进行累加不是程序员应该想起的办法.所以就打算写一个程序来完成这件事,最核心的问题就是“获得一个视频文件的时长”. ffm ...

  2. C++ Primer学习笔记二

    vector<int> a(10,0); for(vector<int>::iterator itor=a.begin();itor!=a.end();itor++) *ito ...

  3. 拒绝低调,国内首家推出微软WP8.1移动开发全套免费课程

    活动类型:公开课 开始时间:2014-10-20 20:00 活动地点:YY频道:85155393 课程简介:学习本次公开课你将收获和体会到:      Windwos Phone8.1的推出正是微软 ...

  4. [转]Sublime Text 2 设置文件详解

    Sublime Text 2是那种让人会一眼就爱上的编辑器,不仅GUI让人眼前一亮,功能更是没的说,拓展性目前来说也完全够用了,网上介绍软件的文章和推荐插件的文章也不少,而且很不错,大家可以去找找自己 ...

  5. [JAVA] java仿windows 字体设置选项卡

    想用java做一个像windows里一样的txt编辑软件,涉及到字体设置选项卡,在网上找了很久都没找到,就生气啦自己写一个,现在贴这里分享一下,下次再遇到这样的问题就不用自己亲自打代码啦! packa ...

  6. 辐射4 中文武器命名方法, 不用winhex, 直接游戏内操作

    参考资料:https://www.reddit.com/r/fo4/comments/3t354b/tip_you_can_add_text_formatting_italic_underline/h ...

  7. Abp Application级别的生命周期

    本篇级别: 中高级篇,假设各位知道Abp是什么, Abp里面的基本的概念及用法(想了解基本概念的可在这里学习: http://www.cnblogs.com/mienreal/p/4358806.ht ...

  8. 有关CSS中字体响应式的设置

    在进行页面响应式设计中,往往需要根据屏幕分辨率来显示不同大小的字体.通常的做法是通过media queries给不同的分辨率指定不同的字体样式,例如: body { font-size: 22px; ...

  9. Java Web指导方向

    第一阶段 第二阶段 第三阶段 第四阶段 第五阶段 第六阶段 第七阶段 第一阶段:Java程序员 技术名称 内                 容 说明 Java语法基础 基本语法.数组.类.继承.多态 ...

  10. 易出错的C语言题目之一:宏定义与预处理

    1.写出下列代码的运行结果: #include<stdio.h> #include<string.h> #define STRCPY(a,b) strcpy(a##_p,#b) ...