AIプログラムとかUnityゲーム開発について

探索や学習などを活用したAI系ゲームを作りたいと思います。

ThirdPersonShooterを作る(4)

 範囲攻撃を実装しました。
 初めはcolliderでやろうかと思ったんですが、なかなか手間取ったので、爆弾側がゾンビをサーチして範囲内にいたらダメージを与える仕様に変更しました。
f:id:yasu9780:20161008172457g:plain


 コード的には、爆発までのリミットを設定(ここでは4秒)。
 爆発リミットで、particleSystemで爆発を起こし、ゾンビをサーチして範囲内(ここでは10f)なら、ApplyDamageでダメージを与える。
 ApplyDamage側ではゾンビが流血し、hpを消耗。この爆弾は100ダメージなので即死。


パーティクルの大きさを動的に変更

ps2.GetComponent<ParticleSystem>().startSize *= 20f;

パーティクル再生後にパーティクルと爆弾GameObjectが自動消滅

Destroy(ps2, ps2.GetComponent<ParticleSystem>().duration);
Destroy(gameObject, ps2.GetComponent<ParticleSystem>().duration);
public class bomb : MonoBehaviour {

    public int owner;
    float explosionLimit = 4f;
    Rigidbody rb;
    GameObject ps;
    GameObject ps2;

    void Start () {
        name = "Bomb";
        rb = GetComponent<Rigidbody>();
        rb.AddForce( (transform.forward + Vector3.up) * 5f, ForceMode.VelocityChange);
        ps = (GameObject)Resources.Load("Explosion");
    }

    void Update () {
        if (explosionLimit > 0f)
        {
            explosionLimit -= Time.deltaTime;
            if (explosionLimit <= 0f)
            {
                GetComponent<MeshRenderer>().enabled = false;
                if (ps2) Destroy(ps2.gameObject);
                ps2 = Instantiate(ps, transform.position + Vector3.up, transform.rotation) as GameObject;
                ps2.GetComponent<ParticleSystem>().startSize *= 20f;
                Destroy(ps2, ps2.GetComponent<ParticleSystem>().duration);
                Destroy(gameObject, ps2.GetComponent<ParticleSystem>().duration);

                for (int i = 0; i < 20; i++)
                {
                    if (Main.red[i] == null || Main.red[i].GetComponent<Enemy>().dead == true) continue;
                    float dis = Vector3.Distance(transform.position, Main.red[i].transform.position);
                    if (dis < 10f)
                        Main.red[i].GetComponent<Enemy>().ApplyDamage(100, owner, Main.red[i].transform );
                }
            }
        }
    }
}


 パーティクル再生後自動消滅はこちらを参考にしました。ありがとうございます。
UnityのShurikenのParticleSystemで自動Destroy - 万年素人からGeekへの道