- Home /
Vector2.MoveTowards in IEnumerator
I am trying to move a boss ship (In a boss style way such that it stops after reaching destination then after sometime continues). But the Boss Ship is not moving with Vector2.MoveTowards.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Boss : MonoBehaviour
{
public List<Transform> wayPoints;
[SerializeField] GameObject spawner;
[SerializeField] WaveConfig waveConfig;
[SerializeField] float moveWait = 1;
int wayPointsIndex = 0;
bool moving = true;
IEnumerator Start()
{
Destroy(GameObject.FindGameObjectWithTag("Respawn"));
wayPoints = waveConfig.GetWaypoints();
var targetPos = wayPoints[wayPointsIndex].position;
var movementSpeed = waveConfig.GetMovementSpeed() * Time.deltaTime;
do
{
yield return StartCoroutine(Move(targetPos, movementSpeed));
}
while (moving);
}
private void OnDestroy()
{
Instantiate(spawner);
}
IEnumerator Move(Vector3 targetpos, float movementspeed)
{
if (wayPointsIndex <= wayPoints.Count - 1)
{
transform.position = Vector2.MoveTowards(transform.position, targetpos, movementspeed);
if(targetpos == transform.position)
{
wayPointsIndex++;
}
if(wayPointsIndex == wayPoints.Count)
{
wayPointsIndex = 1;
}
}
yield return null;
}
}
But the ship is not moving. Anyone got some idea?
Comment