Can't add new gameobject (car) on a specific node of a path (car AI)
So there is a list of nodes which are children of a path. I built tens of those and made cars randomly spawn on each of the nodes with a small delay with an intention to make them continuing their move to next node forgetting about nodes before them. But every spawned car is just moving to the first node.
I tried to implement some ideas to every script of the game, but due to the fact I am a noob I ve failed.
Here is a script for the "path"
public class path : MonoBehaviour
{
public Color lineColor;
private List<Transform> nodes = new List<Transform>();
void OnDrawGizmosSelected()
{
Gizmos.color = lineColor;
Transform[] pathTransforms = GetComponentsInChildren<Transform>();
nodes = new List<Transform>();
for(int i = 0; i < pathTransforms.Length; i++)
{
if(pathTransforms[i] != transform)
{
nodes.Add(pathTransforms[i]);
}
}
for (int i = 0; i < nodes.Count; i++)
{
Vector3 currentNode = nodes[i].position;
Vector3 previousNode = Vector3.zero;
if (i > 0)
{
previousNode = nodes[i - 1].position;
}
else if (i==0 && nodes.Count > 1)
{
previousNode = nodes[nodes.Count - 1].position;
}
Gizmos.DrawLine(previousNode, currentNode);
Gizmos.DrawSphere(currentNode, 0.3f);
}
}
And here is a piece of the AI from "carEngine" script
public class CarEngine : MonoBehaviour
{...
public Transform path;
private List<Transform> nodes;
private int currentNode = 0;
...
void Start()
{
GetComponent<Rigidbody>().centerOfMass = centerOfMass;
Transform[] pathTransforms = path.GetComponentsInChildren<Transform>();
nodes = new List<Transform>();
for (int i = 0; i < pathTransforms.Length; i++)
{
if (pathTransforms[i] != path.transform)
{
nodes.Add(pathTransforms[i]);
}
}
}
private void FixedUpdate()
{
...
CheckWaypointDistance();
}
private void CheckWaypointDistance()
{
if (Vector3.Distance(transform.position, nodes[currentNode].position) < 5f)
{
if (currentNode == nodes.Count - 1)
{
currentNode = 0;
}
else
{
currentNode++;
}
How can I make new spawned cars ignore first nodes and make them follow the nearest next one?
Your answer

Follow this Question
Related Questions
make a self-driving car, combine Navimesh and waypoint? 1 Answer
AI agent that wait to reach the first destination before going for the second 1 Answer
Help with Layermask and List.Contains 0 Answers
A* Pathfinding and keep the enmy at range 0 Answers
HELP WITH PROJECT,Design a build an automated agent that will act and re-act to its environment 0 Answers