# 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? Nov 21 '18 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 Nov 21 '18 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. Nov 21 '18 at 11:32
• I edited my post @DMGregory Nov 21 '18 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
Dec 3 '18 at 20:36

This tutorial series aims to create a simple AI for a racing game in 3D. You can simply achieve the same thing in 2D as well.

• As I mentioned on another of your recent answers, here on GameDev.StackExchange, answers are expected to contain at least a rough outline of the proposed solution, rather than relying wholly on external links to provide the explanation. Link-only answers are subject to deletion, so please consider editing this post to describe the method the tutorial presents. You can still link to the source for credit to the creator & further reading. Nov 18 '18 at 17:30