敵の作成③(敵にHPをつける)

敵にHPをつける
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip destroySound;
public int enemyHP;
private void OnTriggerEnter(Collider other)
{
// もしもぶつかった相手に「Missile」というタグ(Tag)がついていたら、
if (other.gameObject.CompareTag("Missile"))
{
// エフェクトを発生させる
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
// 0.5秒後にエフェクトを消す
Destroy(effect, 0.5f);
// 敵のHPを1ずつ減少させる
enemyHP -= 1;
// ミサイルを削除する
Destroy(other.gameObject);
// 敵のHPが0になったら敵オブジェクトを破壊する。
if (enemyHP == 0)
{
// 親オブジェクトを破壊する(ポイント;この使い方を覚えよう!)
Destroy(transform.root.gameObject);
// 破壊の効果音を出す
AudioSource.PlayClipAtPoint(destroySound, transform.position);
}
}
}
}



Danmaku I(基礎1/全22回)
他のコースを見る
敵にHPをつける
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip destroySound;
public int enemyHP;
private void OnTriggerEnter(Collider other)
{
// もしもぶつかった相手に「Missile」というタグ(Tag)がついていたら、
if (other.gameObject.CompareTag("Missile"))
{
// エフェクトを発生させる
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
// 0.5秒後にエフェクトを消す
Destroy(effect, 0.5f);
// 敵のHPを1ずつ減少させる
enemyHP -= 1;
// ミサイルを削除する
Destroy(other.gameObject);
// 敵のHPが0になったら敵オブジェクトを破壊する。
if (enemyHP == 0)
{
// 親オブジェクトを破壊する(ポイント;この使い方を覚えよう!)
Destroy(transform.root.gameObject);
// 破壊の効果音を出す
AudioSource.PlayClipAtPoint(destroySound, transform.position);
}
}
}
}



敵の作成③(敵にHPをつける)