# simple 2d car AI unity

I have a top down Car Game. how can I implement AI for the Cars? how the car follow the way points? I want AI cars also rotate itself along the path.

I tried this solution

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class waypoint : MonoBehaviour {

public List<Transform> waypoints;
private Transform currentWaypoint;
public float speed = 5f;
private float closeEnouth = 0.5f;
int point = 0;

void Start()
{
currentWaypoint = waypoints[point];
}
// Update is called once per frame
void Update()
{

Quaternion rotation = Quaternion.LookRotation(waypoints[point].position - transform.position, transform.TransformDirection(Vector3.forward));
transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
float dist = Vector3.Distance(waypoints[point].position, transform.position);
transform.position = Vector3.MoveTowards(transform.position, waypoints[point].position, Time.deltaTime * speed);
if (Vector3.Distance(this.transform.position, waypoints[point].position) < closeEnouth)
{
if (point + 1 < waypoints.Count)
point++;

}
}
}


and the car moved in the path but it moves reversed and if arrives the final node it stops and corrects its direction and console displays "Look rotation viewing vector is zero" error.

• What have you tried so far? Presumably you started by searching for existing solutions to making objects follow waypoints in Unity - of the methods you found, which ones did you try putting into practice, and where did you get stuck or get an unexpected result? Commented Nov 21, 2018 at 1:31
• I tried the solution in your link and the car moved in the path but it moves reversed and if arrives the final node it stops and corrects its direction Commented Nov 21, 2018 at 8:00
• I linked to a page of search results with many solutions, so saying you tried "rhe solution" is not enough detail to let us reproduce your problem. Edit your question to show us what method you tried, how you implemented it, and how the results differ from what you want. Commented Nov 21, 2018 at 11:32
• I edited my post @DMGregory Commented Nov 21, 2018 at 12:01
• It seems the error is telling you exactly what is wrong: "Look rotation viewing vector is zero". Why is it zero? Where is it set?
– Anko
Commented Dec 3, 2018 at 20:36