Unity关于BOSS多管血条和激光攻击
2017-02-06 本文已影响0人
UnityLin
预期效果图
4625192-9893f2404a452844.png
新建一个脚本,挂载在Canvas下的血条上(使用Slider)
4625192-20d5cc420358fbb5.png
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class BossHpSliderScr : MonoBehaviour
{
//Slider
Slider bossSlider;
//背景
Image backGroud;
//
Image fill;
//血量
float bossHp = 10000;
//当前所受到的伤害
public float allDemage;
void Start()
{
bossSlider = GetComponent<Slider> ();
backGroud = transform.GetChild (0).GetComponent<Image> ();
fill = transform.GetChild (1).GetChild (0).GetComponent<Image> ();
}
void Update()
{
//
BossHpSlider (0,3333,"yellow","Greed");
BossHpSlider (3333,6667,"red","yellow");
BossHpSlider (6668,10000,"black","red");
}
/// <summary>
/// 根据条件,更改血条
/// </summary>
/// <param name="lowDemage">Low demage.最低伤害</param>
/// <param name="hightDemage">Hight demage.最高伤害</param>
/// <param name="backGroudName">Back groud name.血条背景</param>
/// <param name="fillName">Fill name.血条</param>
void BossHpSlider(float lowDemage,float hightDemage,string backGroudName,string fillName)
{
if (allDemage >= lowDemage && allDemage <= hightDemage) {
//设置精灵
backGroud.overrideSprite = Resources.Load(backGroudName,typeof(Sprite))as Sprite;
fill.overrideSprite = Resources.Load(fillName,typeof(Sprite))as Sprite;
//设置value
bossSlider.value = (hightDemage - allDemage)/ 3333;
}
}
/// <summary>
/// boss减血方法
/// </summary>
/// <param name="Demage">Demage.</param>
public void BossDemage(float Demage)
{
bossHp -= Demage;
//统计所受到的伤害
allDemage += Demage;
}
}
在新建一个脚本,挂载在武器上
4625192-f4160c30fd807569.pngusing UnityEngine;
using System.Collections;
public class WeaponFireScr : MonoBehaviour
{
//根据bool值,手动开火
public bool isFire = false;
//划线
LineRenderer weaponLineRender;
//射线
Ray ray;
//射线检测信息
RaycastHit hitInfo;
//击中物体后激活的特效
GameObject hit;
//动态偏移量
float number;
//脚本代理
BossHpSliderScr bossHpSliderScr;
void Start()
{
weaponLineRender = transform.Find ("Fire").GetComponent<LineRenderer> ();
hit = transform.Find ("Fire/Hit").gameObject;
bossHpSliderScr = GameObject.Find ("BossHPSlider").GetComponent<BossHpSliderScr> ();
}
void Update()
{
if (isFire) {
//开启
weaponLineRender.gameObject.SetActive (true);
//设置射线
ray = new Ray (weaponLineRender.transform.position, weaponLineRender.transform.forward);
if (Physics.Raycast (ray, out hitInfo, 500f)) {
//划线
weaponLineRender.SetPosition (0, weaponLineRender.transform.position);
weaponLineRender.SetPosition (1, hitInfo.point);
//激活击中的特效
hit.SetActive (true);
//设置击中特效所在的位置
hit.transform.position = hitInfo.point;
//boss掉血
bossHpSliderScr.BossDemage(20);
} else {
//如果没击中物体,向前飞行
weaponLineRender.SetPosition (1, weaponLineRender.transform.forward * 500f);
//关闭特效
hit.SetActive (false);
}
//动态偏移量
number -= Time.deltaTime * 2;
//设置动态偏移量(使激光即划线有动态效果)
weaponLineRender.material.SetTextureOffset ("_MainTex", new Vector2 (number, 0));
} else {
//不开火,归位
hit.SetActive(false);
weaponLineRender.SetPosition (1, weaponLineRender.transform.position);
weaponLineRender.gameObject.SetActive (false);
}
}
}