CREATE A ENERGY / HEALTH BAR HUD
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
2 I have renamed the Text to HealthText.
3 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 Fitcheckbox. Change 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 Player. Place 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
CREATE A ENERGY / HEALTH BAR HUD的更多相关文章
- 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 ...
- 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 ...
- UI相关教程:HUD、UMG和Widget
转自:http://aigo.iteye.com/blog/2258612 蓝图脚本来处理 ================================================== 用UM ...
- 12岁的少年教你用Python做小游戏
首页 资讯 文章 频道 资源 小组 相亲 登录 注册 首页 最新文章 经典回顾 开发 设计 IT技术 职场 业界 极客 创业 访谈 在国外 - 导航条 - 首页 最新文章 经典回顾 开发 ...
- Ceph常用命令
目录 [1.环境准备] [2.部署管理] [3.集群扩容] [4.用户管理] [5.密钥环管理] [6.块设备管理] [7.快照管理] [8.参考链接] 简要说明: 最近心血来潮,对分布式存储感兴趣, ...
- PIXI 宝物猎人(7)
介绍 ,本实例来自官网 代码结构 打开 treasureHunter.html 文件,你将会看到所有的代码都在一个大的文件里.下面是一个关于如何组织所有代码的概览: //Setup Pixi and ...
- Unity 2018 Cookbook (Matt Smith 著)
1. Displaying Data with Core UI Elements (已看) 2. Responding to User Events for Interactive UIs (已看) ...
- Python制作的射击游戏
如果其他朋友也有不错的原创或译文,可以尝试推荐给伯乐在线.] 你有没有想过电脑游戏是怎样制作出来的?其实它没有你想象的那样复杂! 在这个教程里,你要学做一个叫<兔子和獾>的塔防游戏,兔子作 ...
- Website's Game source code
A Darkroom by doublespeakgames <!DOCTYPE html> <html itemscope itemtype="https://schem ...
随机推荐
- 【转载】php中iconv函数使用方法
原文:http://www.phpweblog.net/star65225692/archive/2011/03/23/7524.html 在选择用什么工具开发,唯一的指导标准就是:用最少的人 ...
- 设计模式之美:Singleton(单件)
索引 意图 结构 参与者 适用性 缺点 效果 相关模式 实现 实现方式(一):使用 Static 变量初始化 Singleton. 实现方式(二):使用 Lazy Initialization 来实现 ...
- Win32 多线程学习笔记
学到的API函数 一.线程 创建线程.结束线程.获取线程的结束码 CreateThread ExitThread GetExitCodeThread 二.线程结束时触发 创建线程之后,等待线程的结束之 ...
- R 中同步进行的多组比较的包:npmc
方差检验可以评估组间的差异.依据检验的结果,虽然你可以拒绝不存在差异的原假设,但方差检验并没有告诉你哪些组显著地与其他组有不同.Robert 在 <R in Action>一书中推荐了一个 ...
- atitit.web的动态include 跟变量传递 java .net php
atitit.web的动态include 跟变量传递 java .net php 1. 动态include <jsp:include 1 2. 使用QueryString 1 3. 使用Ses ...
- paip.自适应网页设计 跟 响应式 设计方法与工具补充(2)o54
paip.自适应网页设计 跟 响应式 设计方法与工具补充(2)o54 #-----响应式 设计框架 Bootstrap比较热门. Foundation 号称是世界上最先进的响应式前端框架. #---绝 ...
- iOS-项目打包为ipa文件
最近自己做的一个项目,由于app store发布流程比较复杂,且审核周期较长,客户希望提前能看到产品,所以我先给自己的项目打包成一个ipa文件(类似Android的apk安装包),然后发布在" ...
- 关于eclispe保存代码自动格式化的设置
最近在项目开发,上级要求所有开发人员,代码必须格式和(Ctrl+Shift+F),但是还是会偶尔忘记格式化,今天看了有一种保存之后eclipse可以自动格式代码的设置 1.请大家在eclipse设置下 ...
- 利用jsoup爬虫工具,爬取数据,并利用excel导出
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.FileInputStream; i ...
- MySQL的几种连接 join/inner join/cross join/逗号/left join/right join/natural join
转载请注明出处!! 之前数据表连接操作多使用逗号或者join,对几种连接的概念一直浑浑噩噩,最近研究了一波,把这些连接的区别搞明白了. 连接:A xjoin B(主表 操作 关联表) selec ...