0
\$\begingroup\$

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.

car during movement after arriving the final node

Thanks in advance

\$\endgroup\$
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ – DMGregory Nov 21 '18 at 1:31
  • \$\begingroup\$ 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 \$\endgroup\$ – Sh.Adel Nov 21 '18 at 8:00
  • \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Nov 21 '18 at 11:32
  • \$\begingroup\$ I edited my post @DMGregory \$\endgroup\$ – Sh.Adel Nov 21 '18 at 12:01
  • \$\begingroup\$ 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? \$\endgroup\$ – Anko Dec 3 '18 at 20:36
-1
\$\begingroup\$

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.

\$\endgroup\$
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Nov 18 '18 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.