Codementor Events

My Project AI with Cars

Published Jul 14, 2019
My Project AI with Cars

Hey guys! In this post I want to share with all of you a little project in unity. In this project I am trying to develop an AI which is able to handle a car through a circuit. This AI must be able to dodge obstacles find the shortest way to a point (This last one is going to take long long long time). Besides that, the AI should be able to avoid any collision with another car. The first version has been specified in the wiki of the project.

At the moment I am writing this post, there must be hundred of programmers able to do that. I honestly don’t know whether I am doing is the right way or not. But I want to share with you my vision of how this kind of game works so you will be able to take it and modify it as you wish.
In this project I took the following approach:
There are three components: AI, Who manages the car; Car, A car. Duhhh; And a circuit, It contains the nodes of the circuit.
The circuit component gives the AI the nodes the car must use as reference in order to complete the circuit. Each node contains a BoxCollider attach to it. Actually, this wasn’t the first approach that I had in mind. My first approach was to use a Vector2 like a barrier, here a diagram of my plan

First approach.png

As you can see, the AI has a target (Node 2), there is a normal vector (Node 1-Node 2, let’s call it nNode1Node2) which is used a reference and another normal vector (Car-Target Node (Node 2), let’s call it nCarTarget), nCarTarget is updated every frame, in order to know when car cross the node. This is done with the help of dot product, when Vector2.Dot (nNode1Node2, nCarTarget) is less than 0, it means that the car has crossed the node. I was really excited with this because it doesn’t involve any physics process which could mean a light process. Until I came with this:

![First approach.png](https://ucarecdn.com/976d46f7-59c7-4240-b4d0-e806393e8ce5/First approach problem.png)

I know it looks forced, but what happens if the car must take a route which for a moment makes dot product less than zero? Well, with that problem in mind, I could not solve it without the help of other variable named width, but for some reason, in my mind, this process could involve a heavy process. I’ve never tested it.
So, I just went to the easiest solution, which is a BoxCollider that triggers a method when the car reaches the node. With this approach I just need car.direction’s property (direction returns to where the car is pointing out) and nTargetCar vector because I can know if the target node is on the left or on the right side of the car.
A possible solution to this is to set nodes between node 1 and node 2 as many nodes as possible, but honestly, I don’t like it, I want an approach which the AI must find a path between the two nodes dynamically avoiding obstacles and other cars. I have no idea how to do it, but the power of good programming practices a lecture shall help me (And cry).

Well, I leave you my project on gitlab. Hope you enjoy it, and if you have any question or suggestion or want your money back, feel free to text me.

Project: https://gitlab.com/jordan.j.motta.v/project-ai

May the code be with you!

Cover photo: Photo by Hitesh Choudhary on Unsplash

Discover and read more posts from Jordan Motta
get started
post commentsBe the first to share your opinion
garynable
25 days ago

Hello. Most of the cars that were purchased from the states are in the bidfax database, and almost everyone has access to it. Therefore, in order for you to sell your car without any problems, I advise you to use the services of the service bidfax remove vin https://vin.rip/en/partners/bidfax-info/ . Here you can request that your entire search history be deleted from the database of this site. When the deletion is complete, you will receive a report.

Show more replies