敵の作成④(クラスの継承:HP)
![7fc0687c a4ca 4964 a7eb 24a167c0067e](https://codegenius.org/uploads/slide/image/7507/7fc0687c-a4ca-4964-a7eb-24a167c0067e.jpeg)
クラスの継承
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyA : EnemyBase // ←ここを「EnemyBase」に変更する(これでEnemyBaseクラスを「継承」する)
{
void Start()
{
// EnemyBaseを「継承」しているので、そこで定義されている「HP」変数を使うことができる。
HP = 5;
}
}
![16b98b2a c973 4db2 a91f ec29eb70926c](https://codegenius.org/uploads/slide/image/7508/16b98b2a-c973-4db2-a91f-ec29eb70926c.jpeg)
![9bd3f275 efc8 4257 9798 ae602c1f931e](https://codegenius.org/uploads/slide/image/7509/9bd3f275-efc8-4257-9798-ae602c1f931e.jpeg)
![A00bd4ae f503 4e4b bea8 e9445162c4c2](https://codegenius.org/uploads/slide/image/7510/a00bd4ae-f503-4e4b-bea8-e9445162c4c2.jpeg)
![47d33646 2269 4854 9dfe 76e41bcb12d0](https://codegenius.org/uploads/slide/image/7511/47d33646-2269-4854-9dfe-76e41bcb12d0.jpeg)
Missileスクリプト
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Missile : MonoBehaviour
{
public GameObject effectPrefab;
private void OnTriggerEnter(Collider other)
{
// ★テクニック
// TryGetComponentはoutの後ろで指定したコンポーネント(スクリプト)を持っているか否かを判定できる。
// 「クラス継承」の場合、「スーパークラス」まで辿ることができる。
// (条件)もしも、ミサイルにぶつかった相手方がEnemyBaseを持っているならば・・・
if (other.TryGetComponent(out EnemyBase enemy))
{
// TakeDamage()メソッドを呼び出す。
// 「1」はこのミサイルのパワー
enemy.TakeDamage(1);
// ミサイルを破壊する。
Destroy(gameObject);
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
}
}
}
![2ee3f24a f880 4be4 9ebc 3e0e9978bd3d](https://codegenius.org/uploads/slide/image/7512/2ee3f24a-f880-4be4-9ebc-3e0e9978bd3d.jpeg)
![792daa3e 045c 4335 835c 160d8a077364](https://codegenius.org/uploads/slide/image/7513/792daa3e-045c-4335-835c-160d8a077364.jpeg)
【2021版】Danmaku(基礎/全55回)
他のコースを見る![7fc0687c a4ca 4964 a7eb 24a167c0067e](https://codegenius.org/uploads/slide/image/7507/7fc0687c-a4ca-4964-a7eb-24a167c0067e.jpeg)
クラスの継承
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyA : EnemyBase // ←ここを「EnemyBase」に変更する(これでEnemyBaseクラスを「継承」する)
{
void Start()
{
// EnemyBaseを「継承」しているので、そこで定義されている「HP」変数を使うことができる。
HP = 5;
}
}
![16b98b2a c973 4db2 a91f ec29eb70926c](https://codegenius.org/uploads/slide/image/7508/16b98b2a-c973-4db2-a91f-ec29eb70926c.jpeg)
![9bd3f275 efc8 4257 9798 ae602c1f931e](https://codegenius.org/uploads/slide/image/7509/9bd3f275-efc8-4257-9798-ae602c1f931e.jpeg)
![A00bd4ae f503 4e4b bea8 e9445162c4c2](https://codegenius.org/uploads/slide/image/7510/a00bd4ae-f503-4e4b-bea8-e9445162c4c2.jpeg)
![47d33646 2269 4854 9dfe 76e41bcb12d0](https://codegenius.org/uploads/slide/image/7511/47d33646-2269-4854-9dfe-76e41bcb12d0.jpeg)
Missileスクリプト
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Missile : MonoBehaviour
{
public GameObject effectPrefab;
private void OnTriggerEnter(Collider other)
{
// ★テクニック
// TryGetComponentはoutの後ろで指定したコンポーネント(スクリプト)を持っているか否かを判定できる。
// 「クラス継承」の場合、「スーパークラス」まで辿ることができる。
// (条件)もしも、ミサイルにぶつかった相手方がEnemyBaseを持っているならば・・・
if (other.TryGetComponent(out EnemyBase enemy))
{
// TakeDamage()メソッドを呼び出す。
// 「1」はこのミサイルのパワー
enemy.TakeDamage(1);
// ミサイルを破壊する。
Destroy(gameObject);
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
}
}
}
![2ee3f24a f880 4be4 9ebc 3e0e9978bd3d](https://codegenius.org/uploads/slide/image/7512/2ee3f24a-f880-4be4-9ebc-3e0e9978bd3d.jpeg)
![792daa3e 045c 4335 835c 160d8a077364](https://codegenius.org/uploads/slide/image/7513/792daa3e-045c-4335-835c-160d8a077364.jpeg)
敵の作成④(クラスの継承:HP)