Hello I'am trying to make a system that instantiate and object, and the moves it to a set of waypoints. When i use intantiate in the Start() function it travels like intended through the waypoints, but when i add the instantiate line to my update() function it only travels a little while and then stops, the same goes for the rest of the instantiated objects. It has something to do with timer i guess, but i have tried a few approaches, but they all lead to the same thing. Hope someone can help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class waypointTest : MonoBehaviour
{
[SerializeField] private Transform[] waypoints;
public GameObject waypointInvader;
GameObject go;
private int nextUpdate = 1;
public float speed = 5f;
private int waypointIndex;
void Start()
{
}
void Update()
{
if (Time.time >= nextUpdate)
{
nextUpdate = Mathf.FloorToInt(Time.time) + 1;
go = Instantiate(waypointInvader, transform.position, transform.rotation) as GameObject;
}
if (go.transform.position != waypoints[waypointIndex].transform.position)
{
Vector3 newPos = Vector3.MoveTowards(go.transform.position, waypoints[waypointIndex].transform.position, speed * Time.deltaTime);
go.transform.position = newPos;
if (newPos == waypoints[waypointIndex].transform.position)
{
waypointIndex += 1;
}
if (waypointIndex == waypoints.Length)
{
waypointIndex = 0;
}
}
}
}