プレーヤーにHPを設定します。
(スクリプトを作成する)
・新規にC#スクリプトを作成→名前を「PlayerHealth」に変更
・下記のコードを記載してください。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHealth : MonoBehaviour {
private int playerHP = 3;
public GameObject effectPrefab;
public AudioClip explosion;
void OnTriggerEnter(Collider other){
if (other.gameObject.CompareTag ("EnemyShell")) {
playerHP -= 1;
Destroy (other.gameObject);
if (playerHP == 0) {
GameObject effect = Instantiate (effectPrefab, transform.position, Quaternion.identity) as GameObject;
Destroy (effect, 0.5f);
AudioSource.PlayClipAtPoint (explosion, transform.position);
// プレーヤーを非アクティブ状態にする
this.gameObject.SetActive(false);
}
}
}
}
・コードが書けたらチェック→スクリプトをPlayerオブジェクトに付ける。
・「effect」と「Sound」を設定しましょう(自由です)。
(当たり判定用のColliderを設定する)
初期状態では「Player」オブジェクトには「Collider」が付いていません。
・「Add Component」を押して、「Sphere Collider」を付けましょう。
*「Radius」(円の半径)の数値で円の大きさを変えることができます。
・円の大きさによって「当たり判定の厳しさ」を変えられるので自由に変更してみましょう。
Colliderの設定までできたらゲーム再生。
敵のミサイルが当たってHPが0になった際、プレーヤーが画面から消えれば成功です。


