最近看一个视频教程 主要讲对砍rpg的制作 为了提高学习效率 所以适当的做下笔记,也方便以后查找
首先将第一人称的控制器拖入到场景中命名为player 并添加脚本(主要用c#)
代码如下
using UnityEngine;
using System.Collections;
public class PlayHealth : MonoBehaviour {
public int maxhealth=100; //最大生命值
public int curhealth=100; //当前生命值
public float health; //血条的长度
// Use this for initialization
void Start () {
health=Screen.width/2; //将血条长度定义为屏幕的一般长
}
// Update is called once per frame
void Update () {
addjustcurhealth(0);
}
void OnGUI()
{
GUI.TextArea(new Rect(10,20,30,20),"HP");
GUI.Box(new Rect(40,20,health,20),curhealth+"/"+maxhealth);//用gui绘制血条
}
void addjustcurhealth(int adj) //血条长度变化函数
{
curhealth+=adj;
if(curhealth<0)
curhealth=0;
if(curhealth>maxhealth)
curhealth=maxhealth;
if(maxhealth<1)
maxhealth=1;
health=Screen.width/2*(curhealth/(float)maxhealth);
}
}
运行后的效果如下,看上去还是太简陋了。。