(スクリプト)
using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyHP2 : MonoBehaviour { public GameObject effectPrefab; public AudioClip sound; public int HP; private void OnCollisionEnter(Collision collision) { if(collision.gameObject.CompareTag("Rock")) { Destroy(collision.gameObject); GameObject effect = Instantiate(effectPrefab, collision.transform.position, Quaternion.identity); Destroy(effect, 1.0f); AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position); HP -= 1; if(HP == 0) { Destroy(transform.root.gameObject); } } } }
(確認)
- ヒットした瞬間に、エフェクトとサウンドが発生
- 敵のHPが1減少する
- 敵のHPが0になった瞬間に敵が消える