How to make the Ai car? (TopDown)

Hi guys, i was wondering how to make a car ai to race against?
I looked for a youtube tutorial but couldent find any pls help.

Tricky …I think I’d need to try it either with pathfinding the middle of track object or following something that’s got pathfinding attached or checking three points being NOT inside track object (straight ahead 200pix -15degrees 200pix or +15degrees 200pix) or using raycasting

1 Like

You could also use tween or platformer or etc to force the car on a specific path. That wouldn’t really be AI, but is a potential way to do it.

1 Like

I think …be nice to try it with a curved track!
Have a grid and curves that fit together on the grid and have custom points on the curve objects to follow. Number the points and refer to them with a variable. At the beginning of the scene (this is the tricky bit) build an array that has the list of the right points to go to on the right track object
Path[object][point]
And then you can either pathfindvto the points or use a force toward them as nd step through the array

1 Like



heres a simpler version of what i was waffling on about above
which seems to work! There’s only two points on the track

ive made a mistake on the code …whoops …oh no …looks ok!
racing

2 Likes

@petlimpet That’s how you learn though, by getting it wrong until you eventually get it right :slight_smile:

2 Likes

platformer?

(…)

So what your saying is to create a point on the track, and change the rotation of the ai car to those different points?

I wrote that late at night…I have no idea what I meant by platformer…
OH WAIT NEVER MIND I REMEMBER! I was saying you technically could put a top-down platformer in the car and then use events to control it, but alas @petlimpet has arrived with a much better and much less sleep deprived solution!

2 Likes

the code above isn’t quite (the sub events - think it needs a boolean in there - see below!) right but…
Yes - that’s what I’ve done there - there might be other better ways but I thought about other options but this seemed the best to me. Rotate towards a point, tick it off and rotate towards the next one.
It needs to rotate and use a force in the vehicles direction and so i don’t think pathfinding will work.
I thought about raycasting at various angles to work out where the edges of the track were but then decided against it.
With this i’ve numbered the track pieces on the editor. You could do it inside a repeat event at the beginning of the scene - which is doable but quite complicated.
You could have more than two points on the track.
You need code for if it misses a point …the distance to the point is less than 60 and the difference between car angle and angle to point is greater than say 40
you can raycast for obastacles and swerve to avoid …distance to obastacle less than 60 - raycast in direction of car…if positive then rotate.
You can add a variable for random x offset and y offset to the goto object when you move it - make it look less mechanical.
Use variables for speed and rotation speed

1 Like

update 1.1…
racing2

there’s still points on the straight its just that the z order of goto is less than the new track sections

3 Likes

Hi Dave!

Fun!
However, the problem with your solution is that the path of the car is always the same on the race track.
A simple alternative would be to mark the runway contour with invisible marks (like ‘x’ sprites) and manage the collision with them.
In addition, it will be more easy to manage car road exits.
So, with arrow keys, the car can take curves and straights.

A+
Xierra

2 Likes

Thanks Amigo - you’d still need to attach the sprites to points though. i did suggest an x and y random offset - to make it a bit more human. There’s definitely more than a few ways to do this and you could add more and more complexity to it. I quite enjoyed thinking about the problem

1 Like

Not at all, you move your car with arrow keys without path or points to follow.
A very small game from my mind, made some years ago just like that with GD, called Crashball.

A+
Xierra

1 Like

but he wants an ai…so not using keys.

It occurred to me while getting on with what I’m supposed to be doing, that what I did above is a bit like scalextric …if you remember that …Google Google … remarkably …it’s still a thing!. You could turn what I did into scalextric…press key go faster, release key and go slower. Go too fast and come off at the corners …which is why no one ever player scalextric for more than 5minutes and there’s loads on eBay!

1 Like

Oh no im fine with anything that makes a car that races by itself, but i can think of anything else its called that’s why this topic is called how to make ai car.

True, i was thinking that! cause i only have one track.

yea i guess i could lol, i was just too lazy, srry for not replying to this properly, i didn’t read it properly.

1 Like

Make more tracks!
When you change the position of goto, set it to the track.pointX(‘points’),+randominrange(-32,32) and same on y and it’ll take a more random path

1 Like