I have made this code so that the car is going to follow the path, but the car doesn’t follow the Path and continues just straight.`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Path : MonoBehaviour
{
public Color lineColor;
public Color color = Color.white;
private List<Transform> Nodes = new List<Transform>();
void OnDrawGizmosSelected()
{
Gizmos.color = lineColor;
Transform[] pathTransform = GetComponentsInChildren<Transform>();
Nodes = new List<Transform>();
for (int i = 0; i < pathTransform.Length; i++)
{
if (pathTransform *!= transform)*
{
Nodes.Add(pathTransform*);*
}
}
for (int i = 0; i < Nodes.Count; i++)
{
Vector3 currentNode = Nodes*.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.color = Color.white;
Gizmos.DrawLine(previousNode, currentNode);
Gizmos.DrawWireSphere(currentNode, 0.2f);
}
}
}
`
that is the script for the Path
and this is the script for the AI car
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SteeringByItself : MonoBehaviour {
public Transform path;
public float maxSteerAngle = 180f;
public float speedForce = 2f;
private List Nodes;
private int currentNode = 0;
* // Use this for initialization*
* void Start () {*
Transform[] pathTransform = path.GetComponentsInChildren();
Nodes = new List();
for (int i = 0; i < pathTransform.Length; i++)
{
if (pathTransform != path.transform)
{
Nodes.Add(pathTransform*);*
}
}
}
private void FixedUpdate()
{
ApplySteer();
Drive();
CheckWayPointDistance();
}
private void ApplySteer ()
{
Rigidbody2D rb = GetComponent();
Vector3 relativeVector = transform.TransformPoint(Nodes[currentNode].position);
float newSteer = (relativeVector.x / relativeVector.magnitude) * maxSteerAngle;
rb.angularVelocity = newSteer;
}
private void Drive()
{
Rigidbody2D rb = GetComponent();
rb.AddForce(transform.up * speedForce);
}
private void CheckWayPointDistance()
{
if(Vector3.Distance(transform.position, Nodes[currentNode].position) < 0.5f)
{
if(currentNode == Nodes.Count -1)
{
currentNode = 0;
}
else
{
currentNode++;
}
}
}
}
What I’m doing wrong?