タワーデフェンスゲーム　Unity　敵の機能　3/10 終点に達した時に敵オブジェクトが非表示にする

# 概要
前回の続きです。

https://qiita.com/RYA234/items/2632c67fffcc4ce4e07a

実装する点としては、敵オブジェクトが経路の終点に到達すると消えるようにします。
以下gifアニメの様な挙動を実現します。

![Enemy1222222.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/1b7bdcd3-395c-601c-a001-8920a6e56827.gif)

次

https://qiita.com/RYA234/items/5d51902ac84f718aaa7f

# 開発環境

IDE:Rider
Unity:2020.3.42(LTS)
OS:Windows10

# 実装のポイント
EnemyrクラスのUpdateメソッドで終点か判断する
↓
ObjectPoolerクラスのReturnEnemyToPoolメソッドでオブジェクトを非表示する実行する
↓
オブジェクトは非表示になる。



# コード部分

```diff_c_sharp:Enemy.cs
using System;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    [SerializeField] private float moveSpeed = 3f;
    [SerializeField] private Waypoint waypoint;
    public Vector3 CurrentPointPosition => waypoint.GetWaypointPosition(_currentWaypointIndex);
    private int _currentWaypointIndex;

    private void Start()
    {
        _currentWaypointIndex = 0;
    }

    private void Update()
    {
        Move();
        if (CurrentPointPositionReached())
        {
+            UpdateCurrentPointIndex();
        }
    }

    private void Move()
    {
        Vector3 currentPosition = waypoint.GetWaypointPosition(_currentWaypointIndex);
        transform.position = Vector3.MoveTowards(transform.position, currentPosition, moveSpeed * Time.deltaTime);
    }
    
    private bool CurrentPointPositionReached()
    {
        return Vector3.Distance(transform.position, CurrentPointPosition) < 0.1f;
    }

+    private void UpdateCurrentPointIndex()
+    {
+        int lastWaypointIndex = waypoint.Points.Length - 1;
+        if (_currentWaypointIndex < lastWaypointIndex)
+        {
+            _currentWaypointIndex++;
+        }
+        else
+        {
+            ReturnEnemyToPool();
+            
+        }
+    }
+    private void ReturnEnemyToPool()
+    {
+        ObjectPooler.ReturnToPool(gameObject);
+    }
}

```


```diff_c_sharp:ObjectPooler.cs
using System.Collections.Generic;
using UnityEngine;

public class ObjectPooler : MonoBehaviour
{
    [SerializeField] private GameObject prefab;
    [SerializeField] private int poolSize;
    
    private List<GameObject> _pool;
    private GameObject _poolContainer;

    private void Awake()
    {
        _pool = new List<GameObject>();
        _poolContainer = new GameObject($"Pool - {prefab.name}");
        CreatePooler();
    }

    private void CreatePooler()
    {
        for (int i = 0; i < poolSize; i++)
        {
            _pool.Add(CreateInstance());
        }
    }
    
    private GameObject CreateInstance()
    {
        GameObject newInstance = Instantiate(prefab);
        newInstance.transform.SetParent(_poolContainer.transform);
        newInstance.SetActive(false);
        return newInstance;
    }

    // 
    public GameObject GetInstanceFromPool()
    {
        for (int i = 0; i < _pool.Count; i++)
        {
            if (!_pool[i].activeInHierarchy)
            {
                return _pool[i];
            }
        }
        // poolの数がいっぱいの場合は空のGameObjectを返す。
        return CreateInstance();
    }

+    public static void ReturnToPool(GameObject instance)
+    {
+        instance.SetActive(false);
+    }
}

```



# 参考
gameobject アタッチされているコンポーネントを指す。

https://docs.unity3d.com/ja/2018.4/ScriptReference/Component-gameObject.html

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/de481ff8-b9fc-c4f9-26ff-8580c6d5eaaa.png)


gameobject.setActive(false)

https://docs.unity3d.com/ja/560/ScriptReference/GameObject.SetActive.html

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/80cef2f9-dd2b-fc9f-9158-3c1dd3a684ce.png)

Section4 14

https://www.udemy.com/course/learn-how-to-create-a-2d-tower-defense-game-in-unity-2020/learn/lecture/22971554#content


github コミット分

https://github.com/RYA234/2DTowerDeffenceMyLearning/commit/59d7c049199713dc771b23c73340742aa7d62a8d

章まとめ

https://qiita.com/RYA234/private/6d878a7958a3c760c8af