SO i have created an object pool for ‘n’ number of enemies. I want to enable each enemy on the basis of a time period and as soon as the enemy is enabled, it will find a path, complete its run and return back to the object pool & reset (Disable and reset path and coords).
But as soon as i run the game, the object pool is formed but the enemies are not moving. Where did i go wrong?
using System.Collections.Generic;
using UnityEngine;
public class EnemyMovementScript : MonoBehaviour
{
List<Waypoint> path = new List<Waypoint>();
[Range(0f, 10f)]
[SerializeField] float speed;
void onEnable()
{
findPath();
returnToStart();
StartCoroutine(FollowPath());
}
void returnToStart()
{
transform.position = path[0].transform.position;
}
void findPath()
{
GameObject parent = GameObject.FindGameObjectWithTag("path");
foreach(Transform child in parent.transform)
{
path.Add(child.GetComponent<Waypoint>());
}
}
IEnumerator<WaitForEndOfFrame> FollowPath()
{
foreach(Waypoint waypoint in path){
Vector3 startPosition = transform.position;
Vector3 endPosition = waypoint.transform.position;
float travelPercentage = 0;
transform.LookAt(endPosition);
while(travelPercentage < 1f)
{
transform.position = Vector3.Lerp(startPosition, endPosition, travelPercentage);
travelPercentage += Time.deltaTime * speed;
yield return new WaitForEndOfFrame();
}
}
gameObject.SetActive(false);
}
}
This is my movementScript for movement of enemies