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

敵のHP
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
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(sound, Camera.main.transform.position);
}
}
}
}




【2019版】Danmaku I(基礎1/全22回)
他のコースを見る
敵のHP
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
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(sound, Camera.main.transform.position);
}
}
}
}




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