unity3d c#用於兩個坦克對打血條的增減

設置血條增減模式

using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 作用於兩個玩家互相攻擊
/// </summary>
public class OnCollision : MonoBehaviour
{
    public GameObject explosion;//定義一個粒子效果
    public GameObject bustedTank;//定義一個被摧毀時拋出的坦克殘骸
    void OnCollisionEnter(Collision other)
    {

        if (other.gameObject.tag != null && other.gameObject.tag.Equals("q"))   //尋找到對方的標籤
        {
            //先拿到被擊中物體的生命對象
            //獲取到類Life
            Life life = other.gameObject.GetComponent<Life>();
            //攻擊掉血每次20
            life.subBlood(20);
            GameObject p = GameObject.Find("Image"); //獲得精靈圖片
            Image imageShowBlood = p.GetComponent<Image>();//將Image類的組件賦給對象
            imageShowBlood.fillAmount = (life.bloods+0.0f)/100;//獲得圖片血條按比例減少減少
            if (life.bloods <= 0) //如果血量爲0以下
            {
                Destroy(other.gameObject);//拋出坦克的殘骸
                Instantiate(bustedTank, transform.position, transform.rotation);//殘骸位置是當前坦克位置
            }
        }
        if (other.gameObject.tag != null && other.gameObject.tag.Equals("e"))  //尋找到對方的標籤
        {
            //先拿到被擊中物體的生命對象
            //獲取到類Life
            Life life = other.gameObject.GetComponent<Life>();
            //攻擊掉血每次20
            life.subBlood(20);
            GameObject p = GameObject.Find("Image"); //獲得精靈圖片
            Image imageShowBlood = p.GetComponent<Image>();//將Image類的組件賦給對象
            imageShowBlood.fillAmount = (life.bloods + 0.0f) / 100;//獲得圖片血條按比例減少減少
            if (life.bloods <= 0) //如果血量爲0以下
            {
                Destroy(other.gameObject);//拋出坦克的殘骸
                Instantiate(bustedTank, transform.position, transform.rotation);//殘骸位置是當前坦克位置
            }
        }
        Destroy(gameObject);        //銷燬炮彈      
        GameObject obj = Instantiate(explosion, transform.position, transform.rotation);//產生爆炸效果
        Destroy(obj, 1.5f);        //銷燬爆炸效果
    }

}
using UnityEngine;

/// <summary>
/// 上個代碼 的附加腳本 負責生命的定義跟減少;
/// </summary>
public class Life : MonoBehaviour {
    public int bloods = 100;  // 定義坦克的血量爲100
    public void subBlood(int value)///公開血量
    {
        bloods -= value;//血量遞減

    }
}
發佈了29 篇原創文章 · 獲贊 10 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章