unity3d 生命槽的设置

 最近看一个视频教程 主要讲对砍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);
		
	}
}

运行后的效果如下,看上去还是太简陋了。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值