artificial intellegence

i was making car racing game and i need help ont he logic of how to write competator cars script; or even some randomly moving cars in the city.
Thank you in advance.

my question is that; the city has many buildings and streets; in what logic sholud i write the script that the cars could notice that they follow only the streets while the turn or moving stright…thank you in advance again.

3 Answers

3

This is kind of a broad question. The first step would be to write a behavior component script and attach it to your competitor car prefab/object. In this script you can have some logic in the Update() function that would move the car every frame, i.e. transform.position = transform.position + Time.deltaTime * speed * movementDirection; You can adjust the speed and movementDirection so that they always move along whatever track you have set up. Beyond this it depends much more on the specifics of how you have your levels set up and what behaviors you want for the cars.

Use waypoints. You can get a nice result.
If I am not wrong, use MoveTowards() function to move between two Vector3 types. Populate a list, a stack or an array with waypoints then make your competitors route through the waypoints. Make sparing waypoints and randomly choose some and discard others for more real behavior. Make smooth rotations during the path between two waypoints by checking if whatever axis position you are using is greater, to the left or to the right of the previous waypoints. This way the movement wont look mechanical.
Does it makes sense?

The game runs at 60 frames per second, Update() is called 60 times per second, and will do everything inside it within that frame. You can't pause the function in the middle and continue on the next update. If you absolutely have to do that, look up Coroutines on google, which are a bit more complicated, but they are not so bad. Definitely a few steps up from what you are doing here though.

Its not a simple question, you should consider posting this in the community threads instead.

But to get you started, here is how I would do my first prototype:

First I would build the city from elements/nodes and then link these together.
Each part of the road would have its own position, length, roadgrip/de-accelleration on the car tires and perhaps other properties.

Then I would build a circle or an 8 map to see if I could make the cars follow the nodes connected.

Each vehicle only needs to know which node its attached to. When going over the border of a node, it should ask the node what node is connected in that direction. The result is the new connection for the vehicle.

In Update() you let each vehicle move in its own driving direction and do its own “have I reached my goal/waypoint” which is the end of the node.

The thing with this approach is that you can even do it without showing them on the screen, as this can be done in pure CONSOLE programming. We did this at my education in C++ back in 1993, so I know it works.

If you want the cars to have AI enough to avoid driving into each other, then you have to make them aware of each other or perhaps, let the NODE (road part) handle all the attached vehicles.

In our school assignment, we had them all simulated in a que and the assignment was to make them stop for red and drive for green etc. and they couldnt overtake each other, so the slowest vehicles set the pace for the rest.

Again, calling this AI is perhaps a bit too much, but they do move automatically. You could put in some sort of “calculate which route to take” sort of waypoint list and store this inside each car, that way they would have to plan (pathfind) their own routes and you would end up with a map full of vehicles driving around as they please.

If you want this approach, then you could implement a “state machine” in each vehicle too.
Its a simple variable that stores what mode/state your vehicle is in.
It could be:

  • calculating route (this could take a while and be split over more than one frame)
  • following route
  • parked
  • holding for red light
  • crashed
  • run out of gas
  • … etc.

inside your Update() you make a switch(vehicleState) and on each case you can call whatever functions you need ot make the vehicle behave like the state its in.

Hope this helps.