How to create an AI Bot Race Car Controller in Unity tutorial Part 1 - Waypoints

Поділитися
Вставка
  • Опубліковано 30 вер 2024

КОМЕНТАРІ • 90

  • @colonthree
    @colonthree 3 роки тому +7

    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.

  • @PrettyFlyGames
    @PrettyFlyGames  3 роки тому +3

    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

  • @jionni3244
    @jionni3244 3 роки тому +6

    These tutorials are pretty cool. I like it a lot. Keep it up man.

    • @PrettyFlyGames
      @PrettyFlyGames  3 роки тому +1

      Thanks man! With comments like this and all the nice 👍 I'll keep it up for sure

  • @neilfosteronly
    @neilfosteronly Рік тому +3

    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.

    • @PrettyFlyGames
      @PrettyFlyGames  Рік тому +1

      Yes, you might need to tune this value to work for your game depending on scale etc.

  • @Gh0stzZ1
    @Gh0stzZ1 3 роки тому +3

    The best channel

  • @pabloarielnicoli7888
    @pabloarielnicoli7888 2 роки тому +2

    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?

    • @PrettyFlyGames
      @PrettyFlyGames  2 роки тому +1

      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.

  • @smiler10
    @smiler10 3 роки тому +2

    That was a super nice game thx for the help.

  • @neilfosteronly
    @neilfosteronly Рік тому +1

    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.

    • @PrettyFlyGames
      @PrettyFlyGames  Рік тому +1

      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.

  • @gamerchicken5206
    @gamerchicken5206 3 роки тому +2

    Great video waiting for next part also dropped a sub

    • @PrettyFlyGames
      @PrettyFlyGames  3 роки тому

      Super glad that you liked it and also thanks for subscribing!

  • @Gh0stzZ1
    @Gh0stzZ1 3 роки тому +3

    looking forward to the next

    • @PrettyFlyGames
      @PrettyFlyGames  3 роки тому

      Glad to hear that! I’m currently on vacation but will work on the next video when I’m back ☀️

  • @pole
    @pole 3 роки тому +2

    Really interesting, waiting for the next episode! Will you explain also how you implemented different levels of ai?

    • @PrettyFlyGames
      @PrettyFlyGames  3 роки тому +1

      Thanks!
      I was not planning to do that but I'll see if I can squeeze it in :)

  • @Crisinator
    @Crisinator 3 роки тому +2

    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

    • @PrettyFlyGames
      @PrettyFlyGames  3 роки тому

      Alright, I'll help you through our Discord instead, it is easier that way.

    • @deoczidGONI
      @deoczidGONI 3 роки тому +1

      Did you get to fix it? Running into the same problem, I think

    • @Crisinator
      @Crisinator 3 роки тому

      @@deoczidGONI no

    • @PrettyFlyGames
      @PrettyFlyGames  3 роки тому +2

      @@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.

  • @abdullah4653
    @abdullah4653 2 місяці тому

    awesome tutorial wonderful

  • @victorh.fagundessantos6774
    @victorh.fagundessantos6774 3 роки тому +1

    I really enjoyed your videos. Keep it up, chill vibes xD

    • @PrettyFlyGames
      @PrettyFlyGames  3 роки тому +1

      Thanks man! Glad to hear that you like them.

  • @Hadne_
    @Hadne_ 2 роки тому +1

    You skipped like half of the waypoint part, you didn’t show how to make the other script :(

    • @PrettyFlyGames
      @PrettyFlyGames  2 роки тому +2

      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.

    • @Hadne_
      @Hadne_ 2 роки тому +2

      @@PrettyFlyGames ok I got it, thanks!

  • @ellyheijens4668
    @ellyheijens4668 3 роки тому +1

    And your draw script? (How to write draw script)

    • @PrettyFlyGames
      @PrettyFlyGames  3 роки тому

      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

  • @Gamer-yo9jd
    @Gamer-yo9jd Рік тому

    can you give me the scripts plz

    • @PrettyFlyGames
      @PrettyFlyGames  Рік тому

      You can download the complete project with scripts here: www.patreon.com/posts/51162606

  • @mariocucurull7978
    @mariocucurull7978 2 роки тому +1

    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.

    • @PrettyFlyGames
      @PrettyFlyGames  2 роки тому +1

      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;
      }

    • @mariocucurull7978
      @mariocucurull7978 2 роки тому +1

      @@PrettyFlyGames Thank you so much!

  • @AsifZeeshan
    @AsifZeeshan 10 місяців тому

    its nice, can you remove bg music of this video? its hypnotising and falling me asleep. :)

    • @PrettyFlyGames
      @PrettyFlyGames  9 місяців тому

      Ahh well that is just to make you feel relaxed :D

    • @AsifZeeshan
      @AsifZeeshan 9 місяців тому

      @@PrettyFlyGames Overall content is good. I liked it already. keep it up. These days I'm learning Unity w.r.t AI.

  • @RookieEditzTrendy
    @RookieEditzTrendy 8 місяців тому

    I am making an F1 Racing game. Is there any way for the cars to brake before the corner instead of when turning?

    • @PrettyFlyGames
      @PrettyFlyGames  8 місяців тому

      Yes, it is covered in later episodes so keep on watching 👍

    • @RookieEditzTrendy
      @RookieEditzTrendy 8 місяців тому

      @@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

    • @PrettyFlyGames
      @PrettyFlyGames  8 місяців тому

      @@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.

  • @talhadevyt
    @talhadevyt Рік тому +1

    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

    • @PrettyFlyGames
      @PrettyFlyGames  Рік тому +1

      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

    • @talhadevyt
      @talhadevyt Рік тому

      @@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

    • @talhadevyt
      @talhadevyt Рік тому

      @@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

    • @PrettyFlyGames
      @PrettyFlyGames  Рік тому

      @@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.

    • @PrettyFlyGames
      @PrettyFlyGames  Рік тому

      @@talhadevyt I’m not sure what you mean 🤷‍♂️

  • @karlohikes
    @karlohikes Рік тому

    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

    • @PrettyFlyGames
      @PrettyFlyGames  Рік тому

      Make sure you use FindObjectsOfType and not FindObjectOfType.

  • @kilabeann
    @kilabeann Рік тому

    Is there any way we can make the ai avoid obstacles when chasing the player.

    • @PrettyFlyGames
      @PrettyFlyGames  Рік тому +1

      Yes, you can combine it with A* pathfinding, check out this video ua-cam.com/video/58eCnaq9sls/v-deo.html

  • @shogunop5168
    @shogunop5168 2 роки тому

    do you have the code just for the part 1??

  • @ilikecakeandbiscuits
    @ilikecakeandbiscuits Рік тому

    top grade tutorial!

  • @fongaming101
    @fongaming101 2 роки тому

    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 :(

    • @PrettyFlyGames
      @PrettyFlyGames  2 роки тому

      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?

    • @fongaming101
      @fongaming101 2 роки тому

      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.

    • @PrettyFlyGames
      @PrettyFlyGames  2 роки тому

      @@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.

    • @fongaming101
      @fongaming101 2 роки тому +1

      @@PrettyFlyGames amazing answer-! Thanks!

    • @fongaming101
      @fongaming101 2 роки тому

      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;
      }

  • @bzztbzzt9961
    @bzztbzzt9961 2 роки тому

    I think you deleted my comment bro, 🤣

    • @PrettyFlyGames
      @PrettyFlyGames  2 роки тому +2

      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.

    • @bzztbzzt9961
      @bzztbzzt9961 2 роки тому +2

      @@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!

    • @PrettyFlyGames
      @PrettyFlyGames  2 роки тому +2

      @@bzztbzzt9961 glad that we could resolve the problem

  • @deenniiss8930
    @deenniiss8930 Рік тому

    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?

    • @PrettyFlyGames
      @PrettyFlyGames  Рік тому +1

      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
      @deenniiss8930 Рік тому +1

      @@PrettyFlyGames but how do you create them? I understand the way they have the script on them, but how do you create a WaypointNode

    • @deenniiss8930
      @deenniiss8930 Рік тому +1

      @@PrettyFlyGames i made the node script and everything but i didn’t understand how do i create a waypoint node

    • @PrettyFlyGames
      @PrettyFlyGames  Рік тому +1

      @@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

    • @deenniiss8930
      @deenniiss8930 Рік тому +2

      @@PrettyFlyGames ok, thank you very much!

  • @foreignwarren7361
    @foreignwarren7361 3 роки тому +1

    How can you make the car follow waypoints in order? like round a track, instead of going to random points?

    • @PrettyFlyGames
      @PrettyFlyGames  3 роки тому +1

      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.

    • @foreignwarren7361
      @foreignwarren7361 3 роки тому

      @@PrettyFlyGames does this line: currentWaypoint = currentWaypoint.nextWaypointNode[Random.Range(0, currentWaypoint.nextWaypointNode.Length)];
      not select a random waypoint?

    • @PrettyFlyGames
      @PrettyFlyGames  3 роки тому

      @@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

    • @foreignwarren7361
      @foreignwarren7361 3 роки тому

      @@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.

    • @PrettyFlyGames
      @PrettyFlyGames  3 роки тому

      @@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