Simple Car AI Driver in Unity!

Поделиться
HTML-код
  • Опубликовано: 24 дек 2024

Комментарии • 112

  • @CodeMonkeyUnity
    @CodeMonkeyUnity  3 года назад +7

    🌐 Have you found the videos Helpful and Valuable?
    ❤️ Get my Courses unitycodemonkey.com/courses or Support on Patreon www.patreon.com/unitycodemonkey

  • @CodeMonkeyUnity
    @CodeMonkeyUnity  3 года назад +2

    💬 Let's build a Simple Car Driver AI
    Great for any Driving or Racing Game where you want a Car to go to a certain position
    I used this when making my Racing RTS ruclips.net/video/mNoyPz3LCy0/видео.html

  • @BlueGooGames
    @BlueGooGames 3 года назад +14

    Brilliant as usual! I’ve been working on a space ship AI for our game using pid controllers and raycasts and it’s pretty fun!! 😀

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +4

      Nice! I hope your development is going well!

  • @BimzyDev
    @BimzyDev 3 года назад +4

    I'm loving the great variety of content on your channel! 😀

  • @sam_pyrtuh
    @sam_pyrtuh 14 дней назад

    Thank you for this tutorial. It has been very helpful for my commercial projects.

  • @РусяРоджер
    @РусяРоджер 3 года назад +4

    you can multiply speed by dot product so they will make sharper turns

  • @dartutorials7859
    @dartutorials7859 3 года назад +12

    I love AI stuff! I also do a lot of AI things in unity lately i work with procedural generation and AI its so much fun! 👍👍

  • @chidebenosiri4662
    @chidebenosiri4662 3 года назад +9

    Thank you for the tutorial, in Unity 2020.2.2f1, my car was experiencing an issue whereby the car would be steering and not going straight to the point. I fixed with this:
    if (AngleToDir == 0 || (AngleToDir < 10 && AngleToDir > -10))
    {
    TurningAmount = 0;
    }
    I gave the car a range between 10 and -10, and if it is in between the range, it should not steer, also if the AngleToDir is 0, then it shouldn't steer. Hope this helps!

    • @gustavfranklarsen908
      @gustavfranklarsen908 Год назад +1

      I was experiencing the same issue, and was expecting that I might have to make the car "less sensitive" to the angle - this solution worked perfectly, so thanks a lot! :)

  • @-.._.-_...-_.._-..__..._.-.-.-
    @-.._.-_...-_.._-..__..._.-.-.- 2 года назад

    Man, I was trying to use NavMesh for the whole thing and pulling my hair out. I make things too hard for myself sometimes. Thank you for steering me back on track!

  • @lee1davis1
    @lee1davis1 3 года назад +1

    Adddd to my unity must watch playlist. A lot of great logic here thanks

  • @kyleb104
    @kyleb104 Год назад

    One small suggestion is to change turning to an inverseLerped value of angleToDir. Otherwise it will be constantly be making full corrections to the left or right, which will slow the vehicle to a halt if you're using wheel colliders with sideways friction.

  • @NAME494
    @NAME494 3 года назад +1

    your tutorials are really useful! ty!

  • @StellarMirage
    @StellarMirage 2 года назад

    Thanks Code Monkey!! This is so much better and more customizable than ML. Forget ML and make your own. I very much needed the how to steer part. I wasn't able to get it. Thanks Code Monkey for helping me complete racing project :)

  • @trod146
    @trod146 3 года назад

    Awesome! Thanks for sharing this with everyone!

  • @TeaPea102938
    @TeaPea102938 2 года назад

    Nice, thank you! This is helping me make Ai cars in my bike game

  • @gustavfranklarsen908
    @gustavfranklarsen908 Год назад +1

    Great tutorial, this was literally exactly what I was looking for! Perfect for simple racing enemies :D My only comment would be that I had a minor issue with the car turning left/right all the time and never going completely straight (sort of zig-zagging towards the target). Chidebe Nosiri pointed out a solution that seems to work for that, by simply making the car less sensitive to the angles. Once again, great video!

    • @rb76768
      @rb76768 Год назад

      Thats dependet on the driving phisics you have, the more arcady and stable, the more the AI will be able to handle it stable!

    • @rb76768
      @rb76768 Год назад

      Instead of an input 1f you can create a dynamic input by divindng the angleToDir/70f so depending on how sharp the angle is it turns more or less wich looks smoother

  • @elektra81516
    @elektra81516 3 года назад +20

    CDProjekt Red: "I'll pretend I didn't see that"

    • @SubjektDelta
      @SubjektDelta 3 года назад +1

      it takes a little more work than that LOL

    • @NAME494
      @NAME494 3 года назад +2

      @@SubjektDelta but he is only o e person xD

    • @SubjektDelta
      @SubjektDelta 3 года назад +4

      @@NAME494 I guess you have never worked in a big game dev company

    • @a.thiago3842
      @a.thiago3842 2 года назад

      HAHAHAHAHAHAHAA Excellent. Unfortunatelly most of people doesn't understand irony. ....

  • @BanCorporateOwnedHouses
    @BanCorporateOwnedHouses 3 года назад +2

    I'm having two issues. Mouse3D and GameHandler don't exist in the current context

  • @theapexman443
    @theapexman443 Месяц назад

    How would you go about integrating this with pathfinding? Ie if I have buildings in the way and I want the car to pathfind around them?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Месяц назад +1

      For driving you normally want Flow Field pathfinding, then the car just follows the flow field arrows
      But really any kind of pathfinding works, you just make the car turn left/right to go towards the next point in the Path

  • @ambiguouscompany
    @ambiguouscompany 2 года назад

    Please help. For some reason my angleToDir calculation keeps oscillating very quickly between the same angle of a positive and negative value. I have debugged all the parameters within the equation and I still cannot figure out why this is happening...

  • @Lutte_
    @Lutte_ 3 года назад +1

    Hello Code Monkey! I have a question for you, i thank you so much for this tutorial i learn to make something similar, but when my car is following the target, the car start to shake like if his turnAmount was to -1 to 1 to -1 to 1 everytime , you know how i can solve this?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +1

      Calculate the dot product and define a certain tiny minimum amount where below that you don't turn, it will prevent those micro adjustements.

    • @Lutte_
      @Lutte_ 3 года назад

      @@CodeMonkeyUnity thank you bro, you are awesome, dont let try be better♡ greetings from argentina

  • @Mikes-Code
    @Mikes-Code Год назад +1

    Who remembers Carmaggedon?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Год назад +1

      Awesome game! I definitely remember playing it as a kid, it was one of those "forbidden" games, kind of like GTA

    • @Mikes-Code
      @Mikes-Code Год назад

      @@CodeMonkeyUnity I played the pixelated version, and back then was amazed with the graphics 😆

  • @anirbankarmakar2037
    @anirbankarmakar2037 3 года назад

    Great video man, the tutorial is really awesome. By the way can you make some tutorials on procedural animation. There are not much tutorial about this topic in the internet.

  • @thelumberjack8838
    @thelumberjack8838 3 года назад +1

    This is great. Can I use it in another game engine also could you add a to use context based steering for the car to avoid other cars?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +1

      Sure you can implement that same logic in a different engine, in the end it's all math.

  • @hamza-trabelsi
    @hamza-trabelsi 3 года назад

    Man you are the best

  • @thrifash
    @thrifash 3 года назад

    Instead of doing a Vector.distance couldn't you use the dirToMovePosition sqrMagnitude or magnitude before normalizing? Would this be better or equivalent in performance?
    Thanks for the awesome video.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +1

      Sure that would work. Highly unlikely to have any noticeable performance impact unless you have thousands of AI cars.

  • @trancuong6276
    @trancuong6276 3 года назад

    nice tut thanks u so much ez and useful :3

  • @bakartu
    @bakartu Год назад

    I put the CarDriver on my car but it doesn't work. Do i need to add anything else?

  • @mayank4687
    @mayank4687 3 года назад

    0 dislikes very cool!

  • @nixonrulez
    @nixonrulez 2 года назад

    I have edited my comment. You are right. Thank you for your response.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  2 года назад +1

      Uh? Adding support for elevation needs to be added to your Driver script, not the AI script which is what this video is about.
      The AI doesn't care if the target is above or below, it just goes forward, left and right. As long as your Driver script supports elevation this AI will get there.

  • @skulfio5179
    @skulfio5179 3 года назад +1

    Police Obstacle Avoidance Please

  • @illuzorr6134
    @illuzorr6134 3 года назад +1

    CarAi works strangely for me with blender models, they start to move sideways, there were also problems with scripts, but I fixed them. Can anyone please help? Is there another AI similar to this that is capable of avoiding obstacles for cars? I need for my rts.

  • @ahmedaghadi8281
    @ahmedaghadi8281 3 года назад

    Did you removed the playlist which contains complete tutorial for beginners for unity?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад

      It's here ruclips.net/p/PLzDRvYVwl53vxdAPq8OznBAdjf0eeiipT

    • @ahmedaghadi8281
      @ahmedaghadi8281 3 года назад

      @@CodeMonkeyUnity Thx but when I search something like "Unity tutorial for beginners" then your playlist comes up which has more videos like more than 30 or something like that. Okay, no problem! You would have some issues with that playlist I guess.

  • @ra_XOr
    @ra_XOr 3 года назад

    Can I ask about the PC requirements to run such learning simulations? How long such example could take? Is there a way to benchmark mark and understand what type of hardware I need specifically for AI simulations? Like Processor, Ram and GPU? Appreciate your great and wonderful contents ☺️☺️

  • @fuegogames743
    @fuegogames743 2 года назад

    How can we just make the car move randomly on the ground without any targetPoint or path to follow? Like a fake multiplayer game?

  • @detrimite6504
    @detrimite6504 3 года назад

    You should make a multiplayer tutorial

  • @madhusudanpanda6396
    @madhusudanpanda6396 3 года назад

    Superb

  • @random_precision_software
    @random_precision_software 3 года назад

    It won't let me download the files, its saying I never to validate my email..I've done that ages ago and there's no new email in my inbox?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад

      Maybe your mailer rejected a newsletter or reported it as spam, when that happens the email gets unvalidated to prevent it from sending emails to people who dont want them.
      Try adding contact@unitycodemonkey.com to your whitelist and request another validation

    • @random_precision_software
      @random_precision_software 3 года назад

      @@CodeMonkeyUnity Its OK on the my PC ,need to wait for 7 days tho lol

  • @gamermine2006
    @gamermine2006 3 года назад

    U should make tutorial vehicular combat like twisted metal

  • @jeremiasruizdiaz9040
    @jeremiasruizdiaz9040 3 года назад +1

    Does anyone know How I can try make game similar to Fire Emblem, I would like to try make a game with tose same machanics bit I don't know where to start. Dice I'm New to programming

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад

      That's a pretty complex thing to do if you're new to programming, I covered a Grid Based Turn Battle System here
      ruclips.net/video/mONHucoYASU/видео.html
      ruclips.net/video/0QU0yV0CYT4/видео.html

    • @jeremiasruizdiaz9040
      @jeremiasruizdiaz9040 3 года назад +1

      @@CodeMonkeyUnity Thanks brother, now I'm going to watch the videos to see if I get it, another question, do you know how I can make a button in the same action menu, that is, attack, special abilities, etc., that simply appears when my character has little life to transform?

  • @stealthinator5774
    @stealthinator5774 3 года назад

    I think it's about time you switch to visual studio dark theme

  • @KwissBeats
    @KwissBeats 3 года назад

    why give the projectiles when it's incomplete/broken?

  • @krishnaagrawal7368
    @krishnaagrawal7368 3 года назад

    Can someone help me?
    I used to play a game in my phone 5-6 years back. It was a roller coaster game with different levels. In levels some tracks are missing, some tracks fall when you pass of them and there are passengers who fall it you ride carelessly.
    If you know such a game, please tell me.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад

      I believe RollerCoaster Tycoon has some mobile version

  • @DARamosYT
    @DARamosYT 2 года назад

    Very helpful basis. Thinking of calling these ai drivers "monkeybrains".

  • @EmreM.
    @EmreM. 2 года назад

    whats inside the CarDriver script file? Could you share it?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  2 года назад +1

      It's included in the project files linked in the description. It basically just moves the car along the transform.forward and rotates that game object, it's pretty simple

  • @RoyalStudioYT
    @RoyalStudioYT 3 года назад

    Nice!!! like

  • @karar4119
    @karar4119 3 года назад +1

    Yes....very simple....

    • @Hietakissa
      @Hietakissa 3 года назад

      ah yes, I could make this in my sleep while struggling with very basic movement

  • @diliupg
    @diliupg 3 года назад

    scripts have errors. can't attach to cars.

  • @craftlofer9974
    @craftlofer9974 3 года назад

    im trying to make this in 2D, and the car will just keep going backwards no matter where i place the target, is there a fix?
    edit: looks like it works on the z axis, ill see if i can solve it
    edit 2: ill leave this comment here for who does it in 2D, just change the forward in dot to up and it should work

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +1

      Yes for 2D you need to convert the logic from XZ to XY

  • @nGAGE0nline
    @nGAGE0nline 3 года назад +1

    Though nice, I really do start to feel that the term AI (or Artificial Intelligence) is starting to lose its meaning. I guess that's always the case though in the context of showcasing some "basics". Weird though that we now start to consider ML to be what AI stands for :D

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +1

      You give it a target point and the car figures out what actions to take to get there, that's Artificial Intelligence, a non-intelligent entity couldn't figure that out
      The issue is that AI has become a buzzword that some people use in a much more limited way than the words actually mean.
      This is Classic AI, I've also made a Car Driver using Machine Learning AI ruclips.net/video/2X5m_nDBvS4/видео.html

    • @nGAGE0nline
      @nGAGE0nline 3 года назад

      ​@@CodeMonkeyUnity I guess what I was trying to say is: AI seems more automation than intelligence. Sure, there may be some wheight-based and/or priority based decision making along the way. Perhaps even complimented with some prng in there, but given the same parameters it will always behave the same way. It's a great way to approximate the appearance of intelligence. In that sense, I fully agree: It has become a buzzword. It was the best we could do at the time we started giving NPCs behaviours, but is it still accurate to call it AI, now that we have ML I wonder?
      MachineLearning, within the confines of its rules, can/will evolve and change its behavior over time. To me, that's much more "intelligent" than what we attribute to AI. What we call AI, is more a reflection of how much effort was put into the development of the behavioural rules of "automation".
      Anywho, I very much love your videos and my comment was not meant as a critique of the content. I was merely posting a thought that's been going through my head. I apologize if it came across as such.

    • @mohammadsadeghlavaie5560
      @mohammadsadeghlavaie5560 3 года назад

      A friend of mine once said:" AI is just a bunch of ifs"

  • @roshanthapa1297
    @roshanthapa1297 3 года назад

    Good 👍

  • @alword
    @alword 3 года назад

    Cyberpunk devs: 📝

  • @unoriginalcringygaming3002
    @unoriginalcringygaming3002 3 года назад

    Nice

  • @MDProgramming
    @MDProgramming 2 года назад

    Yep

  • @deletesky
    @deletesky 3 года назад

    how do i convert 2d platform to 8 directions

  • @theturkish1373
    @theturkish1373 3 года назад

    great

  • @cpcaraho7046
    @cpcaraho7046 3 года назад

    Hey brother please make a video on train androod controler with realistic acceleration and brake and orbit view+train selector and route selector

  • @tonyhamilton7785
    @tonyhamilton7785 3 года назад

    How do you convert this project to mobile? Thanks

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +1

      The car doesn't care what platform it's being used on, there's no input here, you can set the target position in any way

  • @Emperors_A7MET
    @Emperors_A7MET 3 года назад

    Love from pakistan

  • @babycake10
    @babycake10 3 года назад

    ilk yorum

  • @SubjektDelta
    @SubjektDelta 3 года назад

    so much hardcoding AAAAAHHHHH

    • @lee1davis1
      @lee1davis1 3 года назад

      Really? You think this is hard? CM teaches the basic. Still, always to see other ways to do things.

    • @SubjektDelta
      @SubjektDelta 3 года назад

      @@lee1davis1 hard coding = setting variables at runtime. for example the forwardAmount in the Update function.
      no hard coding would be referencing them outside of functions and then use the reference.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +2

      What's hard about this one? It's all just a bunch of simple logic, it should be easy to follow along with the video

  • @dation0000
    @dation0000 3 года назад

    you move too fast, talk too fast, don't explain things in multiple ways. for people like me in the #lowIqGang

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 года назад +1

      Take your time, this is your learning journey, use the pause button as much as you need, just focus on learning.

    • @robindictus5471
      @robindictus5471 2 года назад

      @@CodeMonkeyUnity damn you handle that sort comments really well

  • @aldrinjosephjude5901
    @aldrinjosephjude5901 2 года назад

    what is a GameHandler in CarDriver.cs

  • @regisdejesus1140
    @regisdejesus1140 Год назад

    #CodeMonkey estou usando unity 2022.2.6 e da erro Assets\CarDriverAI\Scripts\CarAITargetMouse.cs(13,40): error CS0103: The name 'Mouse3D' does not exist in the current context e Assets\CarDriverAI\Scripts\CarDriver.cs(100,43): error CS0103: The name 'GameHandler' does not exist in the current context
    criei private Mouse3D; e esses erros acima some e aparece outro Assets\CarDriverAI\Scripts\CarAITargetMouse.cs(10,20): error CS1519: Invalid token ';' in class, record, struct, or interface member declaration
    como resolver isso,por favor ajude-me.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Год назад

      O Mouse3D foi criado aqui unitycodemonkey.com/video.php?v=0jTPKz3ga4w
      O ultimo erro, tens um ; num sitio onde não é suposto

    • @regisdejesus1140
      @regisdejesus1140 Год назад

      @@CodeMonkeyUnity obrigado por responder, desejo sucesso no seu canal.