Unity關(guān)于BOSS多管血條和激光攻擊

預期效果圖


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>
    /// 根據(jù)條件,更改血條
    /// </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) {
            //設(shè)置精靈
            backGroud.overrideSprite = Resources.Load(backGroudName,typeof(Sprite))as Sprite;
            fill.overrideSprite = Resources.Load(fillName,typeof(Sprite))as Sprite;
            //設(shè)置value
            bossSlider.value = (hightDemage - allDemage)/ 3333;
        }
    }

    /// <summary>
    /// boss減血方法
    /// </summary>
    /// <param name="Demage">Demage.</param>
    public void BossDemage(float Demage)
    {
        bossHp -= Demage;
        //統(tǒng)計所受到的傷害
        allDemage += Demage;
    }

}

在新建一個腳本,掛載在武器上

4625192-f4160c30fd807569.png
using UnityEngine;
using System.Collections;

public class WeaponFireScr : MonoBehaviour
{
    //根據(jù)bool值,手動開火
    public bool isFire = false;
    //劃線
    LineRenderer weaponLineRender;
    //射線
    Ray ray;
    //射線檢測信息
    RaycastHit hitInfo;
    //擊中物體后激活的特效
    GameObject hit;
    //動態(tài)偏移量
    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);
            //設(shè)置射線
            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);
                //設(shè)置擊中特效所在的位置
                hit.transform.position = hitInfo.point;
                //boss掉血
                bossHpSliderScr.BossDemage(20);
            } else {
                //如果沒擊中物體,向前飛行
                weaponLineRender.SetPosition (1, weaponLineRender.transform.forward * 500f);
                //關(guān)閉特效
                hit.SetActive (false);
            }
            //動態(tài)偏移量
            number -= Time.deltaTime * 2;
            //設(shè)置動態(tài)偏移量(使激光即劃線有動態(tài)效果)
            weaponLineRender.material.SetTextureOffset ("_MainTex", new Vector2 (number, 0));
        } else {
            //不開火,歸位
            hit.SetActive(false);
            weaponLineRender.SetPosition (1, weaponLineRender.transform.position);
            weaponLineRender.gameObject.SetActive (false);
        }
    }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容