多米诺 发表于 2012-6-15 00:29

Unity3D令人蛋疼的血条制作 还是共享了..

首先在场景创建GUI Texture,拖动到你想让它出现的位置,取名“BloodLength”

看到这张图大家领悟了吧 没错 就是用图片拼接的方法实现血条血量

设置初始血条长度为100。
创建控制血条的脚本:
在脚本中我将人物碰撞及血量减少的方法放到了一起,下面来做详细解释:var player_ :GameObject;//获取自动寻路的角色
var target_: Transform ;//获取你控制的角色
var attackRange = 200.0;//碰撞发生的范围//player_hp包里面血条的各种形态(需导入)
var h00:Texture2D;
var h05:Texture2D;
var h10:Texture2D;
.......................................
var h100:Texture2D;var animation_time:float;//依靠player出脚的时间来判断target应该减少的血量

var g_Health :GameObject;//获取GUI Texture
static var health = 100;//定义血量初始值
//判断血量,更改素材function DownBloodLength ()
{
g_Health = GameObject.Find("BloodLength");
if(health > 95 )
{
g_Health.guiTexture.texture = h100;
return;
}
else if(health > 90)
{
g_Health.guiTexture.texture = h95;
return;
}
else if(health > 85)
{
g_Health.guiTexture.texture = h90;
return;
}
else if(health > 80)
{
g_Health.guiTexture.texture = h85;
return;
}
else if(health > 75)
{
g_Health.guiTexture.texture = h80;
return;
}
else if(health > 70)
{
g_Health.guiTexture.texture = h75;
return;
}
else if(health > 65)
{
g_Health.guiTexture.texture = h70;
return;
}
else if(health > 60)
{
g_Health.guiTexture.texture = h65;
return;
}
else if(health > 55)
{
g_Health.guiTexture.texture = h60;
return;
}
else if(health > 50)
{
g_Health.guiTexture.texture = h55;
return;
}
else if(health > 45)
{
g_Health.guiTexture.texture = h50;
return;
}
else if(health > 40)
{
g_Health.guiTexture.texture = h45;
return;
}
else if(health > 35)
{
g_Health.guiTexture.texture = h40;
return;
}
else if(health > 30)
{
g_Health.guiTexture.texture = h35;
return;
}
else if(health > 25)
{
g_Health.guiTexture.texture = h30;
return;
}
else if(health > 20)
{
g_Health.guiTexture.texture = h25;
return;
}
else if(health > 15)
{
g_Health.guiTexture.texture = h20;
return;
}
else if(health > 10)
{
g_Health.guiTexture.texture = h15;
return;
}
else if(health > 5)
{
g_Health.guiTexture.texture = h10;
return;
}
else if(health > 0)
{
g_Health.guiTexture.texture = h05;
return;
}
else if(health <= 0)
{
g_Health.guiTexture.texture = h00;
return;
}
}function Start()
{
animation.wrapMode = WrapMode.Loop;

var guale = target_.animation["guale"];
guale.wrapMode = WrapMode.Once;
}function Update () {
      DownBloodLength ();
      
      if(target_ == null)
                   return;

//距离满足碰撞条件
var Distance_CS :float = Vector3.Distance(player_.transform.position,target_.position);
if(Distance_CS < attackRange)
{
if(Distance_CS >12)
{
   var targetPoint1 = target_.position;//当前Cube位置
   player_.transform.rotation = Quaternion.LookRotation(targetPoint1 - transform.position,Vector3.up);
   player_.animation.CrossFade("pao");
}
else if(Distance_CS <= 12)
{//如果2人物接近,player进行踢腿。
   animation_time+=Time.deltaTime;
   player_.animation.CrossFade("chujiao");
}
if(player_.animation.IsPlaying("chujiao"))
{//在脚本中踢腿的动作完成需60帧,也就是两秒。
      if(animation_time > 2)
      {
          health = health -Mathf.CeilToInt(animation_time/10); //进行减血。。。
          if(health <= 0)
          {
         target_.animation.CrossFade("guale");
          }
      }      
}
}完!{:soso__6235880048239246314_3:}






多米诺 发表于 2012-6-15 00:30

这是一个老外写的 我们拓的{:soso__11564777293513085719_3:}

暴躁小强 发表于 2012-11-13 11:09

多米诺 发表于 2012-6-15 00:30 static/image/common/back.gif
这是一个老外写的 我们拓的

顶支持下{:5_439:}

mic6033 发表于 2012-12-4 18:29

very good sample

fengnv314322 发表于 2012-12-6 17:15

xcghfxhsfht

chu35177 发表于 2012-12-17 14:55

看一下下 还要大于10个字

go369 发表于 2012-12-17 17:04

这代码也太庸长了吧,还搞一大堆贴图,为啥不用一个贴图进行缩放来显示进度呢?

xiaosi1278 发表于 2012-12-24 11:17

新手顶一下!!

xiaosi1278 发表于 2012-12-24 14:55

下载看看!

Joy 发表于 2012-12-24 20:41

没有简便一点儿的方法吗?
页: [1] 2
查看完整版本: Unity3D令人蛋疼的血条制作 还是共享了..