敵が前進と攻撃を繰り返すようにする(Listの活用法)
![A23ed1a8 635a 4331 92b9 acdfc40495ea](https://codegenius.org/uploads/slide/image/4462/a23ed1a8-635a-4331-92b9-acdfc40495ea.jpeg)
![72fd4059 aa76 46e9 8353 ef9af26de99f](https://codegenius.org/uploads/slide/image/4463/72fd4059-aa76-46e9-8353-ef9af26de99f.jpeg)
前進停止&攻撃開始
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Chase : MonoBehaviour
{
private GameObject target;
private NavMeshAgent agent;
private EnemyShotShell2 shot2;
void Start()
{
target = GameObject.Find("Goal");
agent = GetComponent<NavMeshAgent>();
// ポイント
// 子供のコンポーネントを取得する。
shot2 = transform.GetComponentInChildren<EnemyShotShell2>();
}
void Update()
{
if (target != null)
{
agent.destination = target.transform.position;
}
}
// ★前進停止の仕組み
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Wall"))
{
// 立ち止まる。
agent.isStopped = true;
// 攻撃を開始する。
shot2.enabled = true;
}
}
}
![61a32c44 5db1 4670 b050 23d003e17706](https://codegenius.org/uploads/slide/image/4464/61a32c44-5db1-4670-b050-23d003e17706.jpeg)
![9ab44145 064e 493a a8c2 06bc6fe095d2](https://codegenius.org/uploads/slide/image/4465/9ab44145-064e-493a-a8c2-06bc6fe095d2.jpeg)
![4dd6b98a 650f 4f90 a134 be4292836493](https://codegenius.org/uploads/slide/image/4466/4dd6b98a-650f-4f90-a134-be4292836493.jpeg)
リストを使って複数の敵を管理する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class DestroyWall : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
private int HP = 20;
// ★リスト
private List<GameObject> enemyList = new List<GameObject>();
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Enemy")
{
// ★リスト
enemyList.Add(other.gameObject);
}
if (other.gameObject.CompareTag("EnemyShell"))
{
HP -= 1;
other.gameObject.SetActive(false);
AudioSource.PlayClipAtPoint(sound, transform.position);
GameObject effect = Instantiate(effectPrefab, other.transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
if(HP < 1)
{
this.gameObject.SetActive(false);
for (int i = 0; i < enemyList.Count; i++)
{
// ★リスト
// 前進を再開する。
enemyList[i].GetComponent<NavMeshAgent>().isStopped = false;
// 攻撃を停止する。
enemyList[i].GetComponentInChildren<EnemyShotShell2>().enabled = false;
}
}
}
}
}
![C331ae24 33d4 4d9c b18c 66519014fca2](https://codegenius.org/uploads/slide/image/4467/c331ae24-33d4-4d9c-b18c-66519014fca2.jpeg)
Unity Code Memo
他のコースを見る![A23ed1a8 635a 4331 92b9 acdfc40495ea](https://codegenius.org/uploads/slide/image/4462/a23ed1a8-635a-4331-92b9-acdfc40495ea.jpeg)
![72fd4059 aa76 46e9 8353 ef9af26de99f](https://codegenius.org/uploads/slide/image/4463/72fd4059-aa76-46e9-8353-ef9af26de99f.jpeg)
前進停止&攻撃開始
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Chase : MonoBehaviour
{
private GameObject target;
private NavMeshAgent agent;
private EnemyShotShell2 shot2;
void Start()
{
target = GameObject.Find("Goal");
agent = GetComponent<NavMeshAgent>();
// ポイント
// 子供のコンポーネントを取得する。
shot2 = transform.GetComponentInChildren<EnemyShotShell2>();
}
void Update()
{
if (target != null)
{
agent.destination = target.transform.position;
}
}
// ★前進停止の仕組み
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Wall"))
{
// 立ち止まる。
agent.isStopped = true;
// 攻撃を開始する。
shot2.enabled = true;
}
}
}
![61a32c44 5db1 4670 b050 23d003e17706](https://codegenius.org/uploads/slide/image/4464/61a32c44-5db1-4670-b050-23d003e17706.jpeg)
![9ab44145 064e 493a a8c2 06bc6fe095d2](https://codegenius.org/uploads/slide/image/4465/9ab44145-064e-493a-a8c2-06bc6fe095d2.jpeg)
![4dd6b98a 650f 4f90 a134 be4292836493](https://codegenius.org/uploads/slide/image/4466/4dd6b98a-650f-4f90-a134-be4292836493.jpeg)
リストを使って複数の敵を管理する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class DestroyWall : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
private int HP = 20;
// ★リスト
private List<GameObject> enemyList = new List<GameObject>();
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Enemy")
{
// ★リスト
enemyList.Add(other.gameObject);
}
if (other.gameObject.CompareTag("EnemyShell"))
{
HP -= 1;
other.gameObject.SetActive(false);
AudioSource.PlayClipAtPoint(sound, transform.position);
GameObject effect = Instantiate(effectPrefab, other.transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
if(HP < 1)
{
this.gameObject.SetActive(false);
for (int i = 0; i < enemyList.Count; i++)
{
// ★リスト
// 前進を再開する。
enemyList[i].GetComponent<NavMeshAgent>().isStopped = false;
// 攻撃を停止する。
enemyList[i].GetComponentInChildren<EnemyShotShell2>().enabled = false;
}
}
}
}
}
![C331ae24 33d4 4d9c b18c 66519014fca2](https://codegenius.org/uploads/slide/image/4467/c331ae24-33d4-4d9c-b18c-66519014fca2.jpeg)
敵が前進と攻撃を繰り返すようにする(Listの活用法)