タワーデフェンスゲーム　Unity　敵の機能　6/10 ２回目以降の敵オブジェクト生成時に最初の移動地点を初期地点にする

# 概要
前回の続きです

https://qiita.com/RYA234/items/9ba3908eec0a15709e91


今回は2Wave以降敵が最終地点に行くバグを修正します。
改善前
![before.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/70a1f6af-8025-0254-c12a-437fa0dcc5c8.gif)


改善後

![after.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2710623/56476396-36c0-3972-a166-069589e41757.gif)

次へ

https://qiita.com/RYA234/items/7a606fc8b879a4032a1b

# 開発環境
IDE:Rider
Unity:2020.3.42(LTS)
OS:Windows10


# UnityEdtior上の設定
前回と変化なし


# 実装のポイント
バグの原因は、Enemyクラスの変数_currentWaypointIndexの値が初期化されていないことです。

対処方法としては、
Enemyクラス側で_currentWaypointIndexを初期化する関数を作り
SpawnerクラスでEnemyを生成する際に、_currentWaypointIndexを初期化する関数を実行しまう。


# コード


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

public class Enemy : MonoBehaviour
{
    public static Action OnEndReached;
    
    [SerializeField] private float moveSpeed = 3f;
    public Waypoint Waypoint { get; set; }
    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()
    { 
        OnEndReached?.Invoke();
        ObjectPooler.ReturnToPool(gameObject);
    }

+    public void ResetEnemy()
+    {
+        _currentWaypointIndex = 0;
+    }
}
```


```diff_c_sharp:Spawner.cs
using System;
using System.Collections;
using UnityEngine;


    public class Spawner : MonoBehaviour
    {
        [SerializeField]  private  int enemyCount = 10;
        // Btw = Between
        [SerializeField] private float delayBtwSpawns;
        [SerializeField] private float delayBtwWaves = 1f;

        private int _enemiesRemaining;
        
        private int _enemiesSpawned;
        private float _spawnTimer;
        private ObjectPooler _objectPooler;
        private Waypoint _waypoint;

        private void Start()
        {
            _objectPooler = GetComponent<ObjectPooler>();
            _waypoint = GetComponent<Waypoint>();
            
            _enemiesRemaining = enemyCount;
        }

        private void Update()
        {
            _spawnTimer -= Time.deltaTime;
            if (_spawnTimer < 0)
            {
                _spawnTimer = delayBtwSpawns;
                if (_enemiesSpawned < enemyCount)
                {
                    SpawnEnemy();
                    _enemiesSpawned++;
                }
            }
        }
        
        // ReSharper disable Unity.PerformanceAnalysis
        private void SpawnEnemy()
        {
            GameObject newInstance = _objectPooler.GetInstanceFromPool();
            Enemy enemy = newInstance.GetComponent<Enemy>();
            enemy.Waypoint = _waypoint;
            enemy.transform.localPosition = transform.position;
+            enemy.ResetEnemy();
            newInstance.SetActive(true);
        }

        private IEnumerator NextWave()
        {
            yield return new WaitForSeconds(delayBtwWaves);
            _enemiesRemaining = enemyCount;
            _spawnTimer = 0f;
            _enemiesSpawned = 0;

        }
        
        private void RecordEnemyEndReached()
        {
            _enemiesRemaining--;
            if (_enemiesRemaining <= 0)
            {
                StartCoroutine(NextWave());
            }
        }
        private void OnEnable()
        {
            Enemy.OnEndReached += RecordEnemyEndReached;
        }

        private void OnDisable()
        {
            Enemy.OnEndReached -= RecordEnemyEndReached;
        }
    }

```




# 参考


Section4 16

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


githubコミット分

https://github.com/RYA234/2DTowerDeffenceMyLearning/commit/7e84c65f4a8cd92a182f87acc3ef294a6ae42946#diff-db59377f483c378a741bb5f5953b09af8a6f2f83380280d9200dda1b8321141d

章まとめ

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