Thursday, 30 May 2013

Unity3d Car Game Tutorial-8 (Adding AI Cars)

Due to some reason I cannot continue making videos on Unity Car Game Tutorial Series, however I will be writing posts on it instead, hope you will appreciate the posts as you did for the videos. So let's kick off then.

In this bit of the tutorial we will be adding AI Cars to our game. Follow the steps below carefully and you shall be successful in adding AI cars and make the race game more enjoyable.
(Try to use a track which is flat and does not include up's and down's, like in the unity3d demo project)

Step 1: Drag the Car transform from the 'Project' section into the 'Hierarchy' section and name it as (say)           'AICar' (Search for 'car' in the 'search' bar in 'project' section)

Step 2: Create a Javascript named AICar_Script and add the code from the link here

Step 3: Replace the 'Car' script of the 'AICar' with the AICar_Script script

Step 4: Now create WheelColliders for the rest 3 wheels as well, as we had did it earlier for the Front Wheel (Simply duplicate WHeelFL by clicking on it once and pressing 'ctrl+d' and rename them as 'WheelFR', 'WheelRL', 'WheelRR') and synchronize them with the 3 wheels (the two images below should help in clearing doubts, if any).















Step 5:  Now drag the wheels and the wheelcolliders onto the hierarchy as in the image below





Step 6: Go to GameObject and create an empty Gameobject and rename it as 'WaypointContainer'

Step 7: Go to GameObject -> Create Other -> Sphere and create a sphere, and move it onto the WaypointContainer gameobject, and place the sphere on the terrain as if it is just kissing the surface, this is our first waypoint.

Step 8: Create waypoints like this all along the way, so that your AI car follows the waypoints (more the waypoints, better the result.) The Images below might help in clearing this point (The spheres or the small dots you see in the image on the right are the waypoints)












Step 9: Now drag this WaypointContainer onto the Waypoint Container of the AICar_Script as shown in the image below





















Step 10: Set the Gear Ratio to the values shown in the image above.

This is it, if you have followed this post correctly then you should have an AICar in your race game.
You can follow the same steps to create more AICars.

Hopefully this post is clear and hope this helped you, comment if you have any issues.

24 comments:

  1. Thanks man. Your tutorials are the best \m/

    ReplyDelete
  2. Sir why its not functioning on me.. </3

    ReplyDelete
    Replies
    1. do u have a facebook or something so we can talk.. really need your assistance.. thx!

      Delete
    2. Well, tell me what is your issue, i will try to help you out..

      Delete
  3. I was looking for this sir. Thank you so much for this :) +1

    ReplyDelete
    Replies
    1. Thanks for the comment and +1 mate :)

      Delete
  4. Thank you for your tutorial Sir,
    but may i ask you question ...
    i have done to try this script, and its work,
    but sometime, the "AI Car" was jump and jump, and sometime the "AI Car" not follow the lines waypoint, maybe you can explain to me why it happend ,

    thanks before

    ReplyDelete
    Replies
    1. Hello Rian,
      If the car is behaving in an unexpected manner then you need to change the properties of the wheel colliders..
      If the car is behaving unexpectedly when taking a turn, then you have to adjust the center of mass in the AI_Car script..
      And coming to the ai car not following the waypoints sometimes, make sure the waypoints are not far away from each other..

      Delete
    2. I'm done ..
      i have solved why "AI Car" was jump and jump, its because my additional script, and when i remove that, the "AI Car" was not jump and jump again ...

      and about "AI Car" not follow the lines waypoint, i also think to put the waypoint not far away from each other, and its work .. But i still not understand, how the "AI Car" can turn smoothly ..

      thanks before "Glued Brain" for your answer my question.
      warm regards

      Delete
    3. Cool! Good Going! Now try to add collision avoiding property to the AI_Car. You can follow the part ten of this tutorial series for that. Happy Coding!

      Delete
  5. Amine Ben Cheikh29 August 2013 at 20:18

    thank you for this great serie of tutorial Sir! I just have an issue: when the car doesn't sometimes follow precisely the waypoints and hits the sides of the track. it stops completely.when i push it with the player car and make it close to the next waypoint, it accelerates again.how can i fix this or how can i make it not collide with the sides of the track in first place.thanks

    ReplyDelete
  6. Hello Amine Ben Cheikh,

    You are not supposed to place the waypoints very far away from each other, make sure you place them as close as you can.

    For the AICar to avoid the obstacles you need to create a custom racing track, because in the demo tracks they have used many meshes, so it is hard to make it work with the default track.

    Having said that you can avoid other obstacles, refer to my post here,

    http://gluedbrain.blogspot.in/2013/06/unity3d-car-game-tutorial-10-adding.html

    ReplyDelete
  7. Amine Ben Cheikh30 August 2013 at 07:53

    Thank you Sir for your reply.Adding more waypoints in sharp turns did the job very well.Just another issue Sir : i made a guitext to display gear and i noticed the car never goes beyond the 2nd gear, is there any way to make it go faster? and as a last problem, sometimes the car do follow the waypoints backwards,is there any way to make sure the car is always facing waypoints when it is following them? Thank you again Sir for your time and your precious help.Best regards !

    ReplyDelete
  8. The AICar script that I have posted her takes care of that problem (the car facing the waypoints always) Make sure that you have not skipped any step. Play with the AICar values like torque and other values you can increase the speed of the the AICar.
    Unfortunately I have do not have the track designed right now, so I cannot tell you exactly, but try changing the values of AICar, you'll get to know what works and what does not.


    Happy Coding :)

    ReplyDelete
  9. thank you Sir for your previous replies,my game seems now to be perfectly working.i just have one problem left , i did the split screen 2-players game.the problem is once i've finished the game and hit play again,the game displays you finished the race in 0 seconds which is a new recodrd.Is there a way i can reinitialize my variables to tackle that problem.many thanks !!

    ReplyDelete
  10. Well, yea I guess this is caused by the "static" variables, make sure you initialize them to their respective initial values to get rid rid of this flaw..

    ReplyDelete
  11. Thanks alot for this amazing tutorials.

    I just wanted to know how to decrease the cars speed, its too fast.

    looking forward for your reply.

    Thanks a lot once again.

    ReplyDelete
  12. Hi, i found 2 car when i search "car" in project. 1 look like player car, 1 completely nothing. What car i should drag into hierachy. Im done anything u said above with the car (i duplicate car already in hierachy) but when i press play nothing happen and car wont move. Can u teamviewer and help me?

    ReplyDelete
  13. the player car should be used...and yeah regarding that error, I too faced that error, but it was due to the soundcontroller, if it is present on the AI car remove that component from it....I guess this should solve the issue...

    ReplyDelete
  14. try changing the values of "ENGINE TORQUE" in the inspector....you can change other values to your need as well..

    ReplyDelete
  15. I followed this tutorial from the beginning and I made everything like in this post but my ai car goes backwards and flies over stuff. what should I do?

    ReplyDelete
  16. رز عبد الله2 February 2015 at 14:08

    hi sir, your link is not avaliable. can you fix it? really i need it
    thanks

    ReplyDelete
  17. GluedBrain i need AICar script, the link doesn't work

    ReplyDelete