You could instead do distance-to-next-node with an angle between the nodes to get an even better speed reduction which will not require absolute values to be written as guidance per nodes. The angle check is a normalised dot-produce that checks the cars direction against the node + next node's direction. The more on the side you approach the slower you will go if going too fast, and will also help aid steering as it can tie into that also.
If you like this tutorial series and want to try the game that made it possible then take Total Arcade Racing for a spin on Nintendo Switch www.nintendo.com/games/detail/total-arcade-racing-switch/ Steam store.steampowered.com/app/1264730/Total_Arcade_Racing/ or Xbox www.xbox.com/en-US/games/store/total-arcade-racing/9P0RZ0WCLMFM 📍 Support us on Patreon to help us make more videos like this one www.patreon.com/prettyflygames
This only works if you know where the curves (waypoints) are. But what about if you dont ? Like in those games with Map Creator Tools where Players create their custom circuits. What would be a good approach to solve that?
Often those type of build your own custom circuits are based on grids where you place pre-build blocks. In that case the still concept works as you would create an AI path per block. Then as the AI enters the block it picks the AI path. Once it enters the next block it picks up the AI path from that block and then it continues like that forever.
I think for maxSpeed don't set it as a public number. Instead just call it's maxSpeed from the TopDownCarController. 25:48 in the video. I used this on Awake instead. maxSpeed = topDownCarController.maxSpeed; After writing this I see why you set it to 16. Any number under 16 will then be slowed down to that speed. Over and will be max speed for car up to 1000. I was thinking it was going to cap max speed to 16.
So the reason why there are 2 different variables for maxSpeed is that this is the maxSpeed for the AI and not the car. I could probably have used a better name. Later on in the series the maxSpeed for the AI will be used to implement skill levels and some other things.
@@deoczidGONI the problem was that the waypoint was pointing towards itself in the setup that Cris had. Make sure that the Next waypoint slot points to another waypoint. If they still have issues, please feel free to drop by our Discord discord.gg/AyYtKXT and I can give you a hand.
Are your referring to the script DrawPathHandler.cs? In that case yes I did not cover it since it is not essential but you can download the source for it for free here: www.patreon.com/posts/51162606 as mentioned in the description.
Hello! First of all great work with these tutorials. I have a question, why does my car whenever I press play look up? even if you turn it left or right in the editor, pressing play automatically changes it.
Thanks 🙏 The reason why this is happening is that the car overrides the rotation that was given to it in the editor. To fix this simply add the following to TopDownCarController.cs void Start() { rotationAngle = transform.rotation.eulerAngles.z; }
@@RookieEditzTrendy ohh sorry it was already covered in the first episode ua-cam.com/video/vsPzo7IVTHw/v-deo.html What you do is place a waypoint a bit before a corner with a limited max speed, then the cars will slow down before they hit the corner.
Hey i just want to ask how do i make ai thats aware of the cars around them and walls so they dont run into them all the timr and also see a car infront is braking and slow down too so they dont ram into the said car
Hey there! The answers to your questions are covered in the next episodes so keep on watching. Avoidance: ua-cam.com/video/5SJ6AAI6Wcs/v-deo.html Path finding: ua-cam.com/video/58eCnaq9sls/v-deo.html
@@PrettyFlyGames hey so ill try the path finding soon but i wanna ask also since my cars are going up and down instead of left to right how can i change the direction of the avoidence vector Also how can i do pace laps for the game where i can change it from one line to another
@@PrettyFlyGames one more question too How do i have ai choose a lane thats open in the racing line rather than random lets say theres 3 lanes in a turn and its on the inside but wants to go outside cuz its clear how do i do that
@@talhadevyt if you make sure that all lanes have the same number of waypoints and roughly the same spacing then you can just select which lane to follow. That way you can gave a car in the middle lane either change to the left or right lane.
When finding all waypoints I get error "cannot implicitly convert type 'WayPointNode' to type 'WayPointNode[]'". For reference the problem is with the code shown in the video at 16:01 I would appreciate the help
Thanks! The same concept should work in 3D but you need to change a few things as the axis are different. I think that there are a few good tutorials already for this in 3D, or maybe I'm wrong?
Im going to give it another stab my man-! My 3D set up is wonky and i believe my car moves on Vector3.Left. I wish i could move the blue, red and green arrows to default without moving my car. I tried a parent object but dont like that.
@@fongaming101 Go for it! But having the model incorrectly rotated will cause a lot of problems for you later on. So I urge you to have a main object which is your car and then have the model as a child. If the model is not correctly rotated then you can fix that by rotating the child model object. Your main car object is the one that you should apply rotations to and everything else. That way your forward vector will correct.
I didn't but it appears that UA-cam sometimes automatically does for some weird reason and I cannot even find the comment. But I did reply to your question earlier so not sure if you got it so here it is again :) The most common problems with the AI circling a waypoint is: 1. The waypoint does not have a next waypoint set in the inspector which means that the AI has no clue where to go next. 2. The Z position needs to on Zero for both the waypoint and the car.
@@PrettyFlyGames Alright I got it, maybe it was the second one you mentioned, I'll try checking when I use it again I had to use a different method since I have to finish the project and have it submitted. I appreciate the response, thanks!
Hi! Each waypoint has the Waypoint node script on them but that is all they need. Then you connect the next waypoint in the node script, that way each waypoint know which is the next one.
@@deenniiss8930 create an empty game object and attach the Waypoint node script to it. That is all that you need. You can download the example project here and check out how it works in the scene www.patreon.com/posts/51162606
I'm not sure what you mean, if you put down waypoints in order and just keep the array to a single waypoint then the AI will drive through the waypoints in that order. So to make a random track you'd just lay down waypoints in a round fashion and then you'd connect them to form a round shape. Then the AI will follow that path.
@@PrettyFlyGames does this line: currentWaypoint = currentWaypoint.nextWaypointNode[Random.Range(0, currentWaypoint.nextWaypointNode.Length)]; not select a random waypoint?
@@foreignwarren7361 it will pick a random waypoint if there is more than one to chose from. If there are just 1 waypoint to pick from then it will always pick that one. This code works if there is just 1 waypoint = not random of if there are many = random
@@PrettyFlyGames cool so how do I get it to follow in sequence 1 2 3 4 etc so it goes around a track my track has colliders on the side of the road. So the ai car should just follow round the track not go randomly between points.
@@foreignwarren7361 You place the waypoints in the order you wish the car to drive through them. In Waypoint 1 you assign Waypoint 2 in the next slot. In Waypoint 2 you assign waypoint 3 in the next slot. Then on the last way point lets call it WayPoint 5 you assign Waypoint 1 in the next slot. Then the AI will follow the waypoints in order. To make it easier I've added a new example scene "Round race track example" where you can see how it works with a round track without intersections. Get the update project here: www.patreon.com/posts/51162606
You could instead do distance-to-next-node with an angle between the nodes to get an even better speed reduction which will not require absolute values to be written as guidance per nodes.
The angle check is a normalised dot-produce that checks the cars direction against the node + next node's direction. The more on the side you approach the slower you will go if going too fast, and will also help aid steering as it can tie into that also.
Yes, that would work.
If you like this tutorial series and want to try the game that made it possible then take Total Arcade Racing for a spin on Nintendo Switch www.nintendo.com/games/detail/total-arcade-racing-switch/ Steam store.steampowered.com/app/1264730/Total_Arcade_Racing/ or Xbox www.xbox.com/en-US/games/store/total-arcade-racing/9P0RZ0WCLMFM
📍 Support us on Patreon to help us make more videos like this one www.patreon.com/prettyflygames
These tutorials are pretty cool. I like it a lot. Keep it up man.
Thanks man! With comments like this and all the nice 👍 I'll keep it up for sure
On the waypoints my AI was just staying at first one he got to. I increased the Min Distance to Reach WayPoint and then it worked fine.
Yes, you might need to tune this value to work for your game depending on scale etc.
The best channel
This only works if you know where the curves (waypoints) are.
But what about if you dont ?
Like in those games with Map Creator Tools where Players create their custom circuits.
What would be a good approach to solve that?
Often those type of build your own custom circuits are based on grids where you place pre-build blocks. In that case the still concept works as you would create an AI path per block. Then as the AI enters the block it picks the AI path. Once it enters the next block it picks up the AI path from that block and then it continues like that forever.
That was a super nice game thx for the help.
I think for maxSpeed don't set it as a public number. Instead just call it's maxSpeed from the TopDownCarController. 25:48 in the video. I used this on Awake instead.
maxSpeed = topDownCarController.maxSpeed;
After writing this I see why you set it to 16. Any number under 16 will then be slowed down to that speed. Over and will be max speed for car up to 1000. I was thinking it was going to cap max speed to 16.
So the reason why there are 2 different variables for maxSpeed is that this is the maxSpeed for the AI and not the car. I could probably have used a better name. Later on in the series the maxSpeed for the AI will be used to implement skill levels and some other things.
Great video waiting for next part also dropped a sub
Super glad that you liked it and also thanks for subscribing!
looking forward to the next
Glad to hear that! I’m currently on vacation but will work on the next video when I’m back ☀️
Really interesting, waiting for the next episode! Will you explain also how you implemented different levels of ai?
Thanks!
I was not planning to do that but I'll see if I can squeeze it in :)
My ai only turns circles when it hits a waypoint? I just copied your code, how can I fix this? I am in your discord as swell
Alright, I'll help you through our Discord instead, it is easier that way.
Did you get to fix it? Running into the same problem, I think
@@deoczidGONI no
@@deoczidGONI the problem was that the waypoint was pointing towards itself in the setup that Cris had. Make sure that the Next waypoint slot points to another waypoint. If they still have issues, please feel free to drop by our Discord discord.gg/AyYtKXT and I can give you a hand.
awesome tutorial wonderful
Thanks 🙏
I really enjoyed your videos. Keep it up, chill vibes xD
Thanks man! Glad to hear that you like them.
You skipped like half of the waypoint part, you didn’t show how to make the other script :(
Are your referring to the script DrawPathHandler.cs? In that case yes I did not cover it since it is not essential but you can download the source for it for free here: www.patreon.com/posts/51162606 as mentioned in the description.
@@PrettyFlyGames ok I got it, thanks!
And your draw script? (How to write draw script)
Do you mean the lines for the waypoints? Have a look at the script called DrawPathHandler.cs in www.patreon.com/posts/car-ai-tutorial-51162606
can you give me the scripts plz
You can download the complete project with scripts here: www.patreon.com/posts/51162606
Hello! First of all great work with these tutorials. I have a question, why does my car whenever I press play look up? even if you turn it left or right in the editor, pressing play automatically changes it.
Thanks 🙏
The reason why this is happening is that the car overrides the rotation that was given to it in the editor. To fix this simply add the following to TopDownCarController.cs
void Start()
{
rotationAngle = transform.rotation.eulerAngles.z;
}
@@PrettyFlyGames Thank you so much!
its nice, can you remove bg music of this video? its hypnotising and falling me asleep. :)
Ahh well that is just to make you feel relaxed :D
@@PrettyFlyGames Overall content is good. I liked it already. keep it up. These days I'm learning Unity w.r.t AI.
I am making an F1 Racing game. Is there any way for the cars to brake before the corner instead of when turning?
Yes, it is covered in later episodes so keep on watching 👍
@@PrettyFlyGames Do you know which episode it is because I can't seem to find a tutorial that slows the bots before they start to turn
@@RookieEditzTrendy ohh sorry it was already covered in the first episode ua-cam.com/video/vsPzo7IVTHw/v-deo.html
What you do is place a waypoint a bit before a corner with a limited max speed, then the cars will slow down before they hit the corner.
Hey i just want to ask how do i make ai thats aware of the cars around them and walls so they dont run into them all the timr and also see a car infront is braking and slow down too so they dont ram into the said car
Hey there!
The answers to your questions are covered in the next episodes so keep on watching.
Avoidance: ua-cam.com/video/5SJ6AAI6Wcs/v-deo.html
Path finding: ua-cam.com/video/58eCnaq9sls/v-deo.html
@@PrettyFlyGames hey so ill try the path finding soon but i wanna ask also since my cars are going up and down instead of left to right how can i change the direction of the avoidence vector
Also how can i do pace laps for the game where i can change it from one line to another
@@PrettyFlyGames one more question too
How do i have ai choose a lane thats open in the racing line rather than random lets say theres 3 lanes in a turn and its on the inside but wants to go outside cuz its clear how do i do that
@@talhadevyt if you make sure that all lanes have the same number of waypoints and roughly the same spacing then you can just select which lane to follow. That way you can gave a car in the middle lane either change to the left or right lane.
@@talhadevyt I’m not sure what you mean 🤷♂️
When finding all waypoints I get error "cannot implicitly convert type 'WayPointNode' to type 'WayPointNode[]'". For reference the problem is with the code shown in the video at 16:01
I would appreciate the help
Make sure you use FindObjectsOfType and not FindObjectOfType.
Is there any way we can make the ai avoid obstacles when chasing the player.
Yes, you can combine it with A* pathfinding, check out this video ua-cam.com/video/58eCnaq9sls/v-deo.html
do you have the code just for the part 1??
Answer provided on Discord :)
top grade tutorial!
Thanks 🙏
Hi Fred the man! Are you able to provide this for 3D? I tried it on my game but it just spins LOL. I tried many other ways but no go :(
Thanks!
The same concept should work in 3D but you need to change a few things as the axis are different. I think that there are a few good tutorials already for this in 3D, or maybe I'm wrong?
Im going to give it another stab my man-! My 3D set up is wonky and i believe my car moves on Vector3.Left. I wish i could move the blue, red and green arrows to default without moving my car. I tried a parent object but dont like that.
@@fongaming101 Go for it! But having the model incorrectly rotated will cause a lot of problems for you later on. So I urge you to have a main object which is your car and then have the model as a child. If the model is not correctly rotated then you can fix that by rotating the child model object. Your main car object is the one that you should apply rotations to and everything else. That way your forward vector will correct.
@@PrettyFlyGames amazing answer-! Thanks!
Yay, fixed it with your mentoring! gracias!!! Here is my code in 3D by applying the parent as you recommended. WOOOHOOOO!!
void FixedUpdate()
{
FollowPlayer();
turnInput.x = TurnTowardTarget();
rb.AddRelativeForce(Vector3.forward * speed * velocityForward * Time.deltaTime, ForceMode.Acceleration); //Forward
Rotate();
TireEffects();
}
void FollowPlayer()
{
targetTransform = GameObject.FindGameObjectWithTag("Player").transform;
targetPosition = targetTransform.position;
}
float TurnTowardTarget()
{
Vector3 vector2Target = targetPosition- transform.position;
vector2Target.Normalize();
//Calculate an angle towards the target
float angletoTarget = Vector3.SignedAngle(Vector3.forward, vector2Target,transform.up);
angletoTarget *= -1;
float steerAmount = angletoTarget / 45.0f;
steerAmount = Mathf.Clamp(steerAmount,-1, 1);
Quaternion rotation = Quaternion.LookRotation(vector2Target, transform.up);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, turnSpeed * Time.deltaTime);
return steerAmount;
}
I think you deleted my comment bro, 🤣
I didn't but it appears that UA-cam sometimes automatically does for some weird reason and I cannot even find the comment. But I did reply to your question earlier so not sure if you got it so here it is again :)
The most common problems with the AI circling a waypoint is:
1. The waypoint does not have a next waypoint set in the inspector which means that the AI has no clue where to go next.
2. The Z position needs to on Zero for both the waypoint and the car.
@@PrettyFlyGames Alright I got it, maybe it was the second one you mentioned, I'll try checking when I use it again I had to use a different method since I have to finish the project and have it submitted. I appreciate the response, thanks!
@@bzztbzzt9961 glad that we could resolve the problem
hi, i just wanted to ask, are the WaypointNodes, the one you do ar 14th minute, empty? like you just create empty and you place them around the map?
Hi!
Each waypoint has the Waypoint node script on them but that is all they need. Then you connect the next waypoint in the node script, that way each waypoint know which is the next one.
@@PrettyFlyGames but how do you create them? I understand the way they have the script on them, but how do you create a WaypointNode
@@PrettyFlyGames i made the node script and everything but i didn’t understand how do i create a waypoint node
@@deenniiss8930 create an empty game object and attach the Waypoint node script to it. That is all that you need. You can download the example project here and check out how it works in the scene www.patreon.com/posts/51162606
@@PrettyFlyGames ok, thank you very much!
How can you make the car follow waypoints in order? like round a track, instead of going to random points?
I'm not sure what you mean, if you put down waypoints in order and just keep the array to a single waypoint then the AI will drive through the waypoints in that order. So to make a random track you'd just lay down waypoints in a round fashion and then you'd connect them to form a round shape. Then the AI will follow that path.
@@PrettyFlyGames does this line: currentWaypoint = currentWaypoint.nextWaypointNode[Random.Range(0, currentWaypoint.nextWaypointNode.Length)];
not select a random waypoint?
@@foreignwarren7361 it will pick a random waypoint if there is more than one to chose from. If there are just 1 waypoint to pick from then it will always pick that one. This code works if there is just 1 waypoint = not random of if there are many = random
@@PrettyFlyGames cool so how do I get it to follow in sequence 1 2 3 4 etc so it goes around a track my track has colliders on the side of the road. So the ai car should just follow round the track not go randomly between points.
@@foreignwarren7361 You place the waypoints in the order you wish the car to drive through them. In Waypoint 1 you assign Waypoint 2 in the next slot. In Waypoint 2 you assign waypoint 3 in the next slot. Then on the last way point lets call it WayPoint 5 you assign Waypoint 1 in the next slot. Then the AI will follow the waypoints in order.
To make it easier I've added a new example scene "Round race track example" where you can see how it works with a round track without intersections. Get the update project here: www.patreon.com/posts/51162606