(Unity)プレーヤーのHPを作成する(OnControllerColliderHitの活用)

(サンプルコード)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerHP : MonoBehaviour
{
    public GameObject effectPrefab;
    public AudioClip sound;
    public int HP;

    void OnControllerColliderHit(ControllerColliderHit hit)
    {
        if(hit.gameObject.CompareTag("EnemyBullet"))
        {
            Destroy(hit.gameObject);

            HP -= 1;
            GameObject effect = Instantiate(effectPrefab, hit.transform.position, Quaternion.identity);
            Destroy(effect, 0.5f);
            AudioSource.PlayClipAtPoint(sound, transform.position);

            if (HP < 1)
            {
                Destroy(this.gameObject);
            }
        }
    }
}