Hi, i have a question why is my ram object not placed at the beginning of the path when I drag it into the world? When I drag it, the ram is positioned in the middle of the path (2,2), and it seems not to follow the path according to the order I specified in the list. What could be the cause?
in this case i’m using unity verison 2022.3
and this is the code:
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
using UnityEngine.Animations;
using Vector3 = UnityEngine.Vector3;
public class EnemyMover : MonoBehaviour
{
[SerializeField] List<Waypoint> path = new List<Waypoint>();
[SerializeField] [Range(0f, 5f)] float speed = 1f;
// Start is called before the first frame update
void Start()
{
FindPath();
ReturnToStart();
StartCoroutine(FollowPath());
}
void FindPath()
{
path.Clear();
GameObject[] waypoints = GameObject.FindGameObjectsWithTag("Path");
foreach(GameObject waypoint in waypoints)
{
path.Add(waypoint.GetComponent<Waypoint>());
}
}
void ReturnToStart()
{
transform.position = path[0].transform.position;
}
IEnumerator FollowPath()
{
foreach(Waypoint waypoint in path)
{
Vector3 startPosition = transform.position;
Vector3 endPosition = waypoint.transform.position;
float travelPercent = 0f;
transform.LookAt(endPosition);
while(travelPercent < 1f) {
travelPercent += Time.deltaTime * speed;
transform.position = Vector3.Lerp(startPosition, endPosition, travelPercent);
yield return new WaitForEndOfFrame();
}
}
Destroy(gameObject);
}
}