(下準備)
- Cube等のオブジェクトを使って「シールド」を作成

(スクリプト)
- 新規にC#スクリプトを作成
- 名前を「TankShield」に変更
- 下記のコードを書いてチェック
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankShield : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
public AudioClip sound2;
private int shieldHP = 10;
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("EnemyShell"))
{
Destroy(other.gameObject);
GameObject effect = Instantiate(effectPrefab, other.transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(sound, transform.position);
shieldHP -= 1;
if(shieldHP < 1)
{
Destroy(gameObject);
AudioSource.PlayClipAtPoint(sound2, Camera.main.transform.position);
}
}
}
}
(設定)
- スクリプトをShieldオブジェクトに追加
- エフェクトやサウンド等は自由に設定
- Is Triggerにチェックを入れる(重要)

- 敵の砲弾のTagを設定(重要)

(実行)
- 設定が完了したらゲームを再生して確認
- Shieldが敵の砲弾を破壊・・・・、

- HPが0になったらShieldが破壊されれば成功です。
