Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How we created our vehicle AI for 3 different game types

Discussion in 'General Discussion' started by Wigen, Dec 16, 2013.

  1. Wigen

    Wigen

    Joined:
    Aug 31, 2013
    Posts:
    33
    Artificial Intelligence (AI) for Super Truckin'

    With AI comes many complications. We not only wanted AI for racing, but we wanted it for all of our game types. I would like to go over what we needed for the game type, what problems happened and how we battled it. If you'd like to see some code let me know! Want be in the beta? contact us! https://twitter.com/in8bitgames

    -Soccer-
    Need: Each AI follows the ball to knock it into the opposing goal.

    First thing we needed to add was how to track a single way-point. This is pretty standard and allowing our trucks to follow a way-point was pretty straight forward.


    You want to hit a ball... you make the ball the way-point and your done. Right? Wrong! this will allow the AI to just smash into the ball with no clear direction. The soccer field will be like a 5 year old playing pool and smashing the ball off every wall in hopes to make it in the pocket.

    So we thought about adding direction. Adding this was a little bit more complicated, we had to basically get the AI to get behind the ball and aim it towards the goal. Math cant do this? or can it! We need to know the goal and ball location, the direction in which to hit the ball towards the goal and some math magic to get the arch to go around the ball. Our first test results with this logic.


    Whew... we started playing with it... and wow... this made it a lot harder to play against. They would steal the ball away from you and try to knock it towards their goal. It is not perfect but it works pretty well.

    The problem with this approach is it acts as if the target is stationary for that moment in time. By the time the next update happens the trajectory changes. This makes the AI not as accurate when the ball is moving quickly.

    That is all it really took to get AI working for soccer. Can we make it better? Oh yes, there are many ways to improve it. We could look at the trajectory of the ball, and speed of the vehicle and account that in the algorithm to where it needs to hit the ball. Eventually we will add this but for now the AI is already pretty challenging. If you guys want it to be harder then we wont want to disappoint.

    -Hay-
    Need: Each player tries to go for the hay and knock it out of the arena

    We now have single way-point navigation, all we need to really do is turn off ability for aiming it towards a soccer goal. Sweet! Done and done... until we tried it....


    They became experts in finding the hay, and knocking it out before you ever have a chance to get to it as well. They were impossible to play against and it wasn't fun.

    So we added Object avoidance Which i will explain later. not only are they trying to always hit the hay, but they are actively trying to avoid it as well. This nifty trick made them just easy enough to be playable. They don't score every time they get the hay just like a real player would.

    -Racing-
    Need: Each player follows the track.

    So adding onto the single way-point system we already have. we needed to add more to be able to allow the AI to follow a track. Awesome, easy enough. Just make the way-point a list of way-points and when it gets close to the current way-point make it go to the next one.



    So now we have an AI set up for our racing level but this is when we saw problems we were not expecting.

    The problem we had with this approach was we have several areas that a vehicle can get knocked into (such as the quad jump and hills they can fall off of) and how does the AI react when this happens? Right now they will keep trying to get to the next way-point and get stuck on a wall. So how we battled this was to implement zones. This allowed the AI to validate that this is the zone it should be in, and if not to react by either going to a new way-point or follow the track where it dropped like a normal player would.

    Bad behavior:



    Good behavior with Zones:



    AWESOME! it looks like we have a way-point system and a zone system that the AI follows and if knocked out of either system it can figure its way back. We now have pretty amazing race AI.



    -Object Avoidance-

    I believe this needed its own section because we have now integrated this behavior in each of our AI's.
    • In every Level (especially racing) the trucks would pile up leaning into each other as they go to a way-point, ball or hay. This caused the trucks to crash and slow down and didn't look fluid. I could easily pass them when they are in this state.
    • Early tests on the Soccer level you would see the vehicles stuck against a wall because they were trying to go through the wall to get to the other side of the ball.
    • Testing the hay level they would pile up more than hitting the hay as well as clipping edges of jumps and causing them to lose control all the time.

    The answer to these issues is to make the cars see. we basically gave them eyes so that they can listen to things around them and react. This changed our game. This made the AI react in ways that were more what a player would. It felt they were more human when obstacles popped up.



    we added ray's in front of the vehicles that listen to touching objects such as cars, sides of jumps, and walls. Left ray is touched turn to the right, right ray is touched turn to the left.

    We could add the barrel mine to the things to avoid but this gif wouldn't have been as awesome.


    It has been a lot of trial and error. Testing constantly with and without people. We think we have solid AI for each level and each having completely different behaviors. Are we done with the AI? nope! We do plan to constantly improve and balance each level to make them as fun as possible.

    Thank you guys for reading!
     
  2. MarioRuiz

    MarioRuiz

    Joined:
    Nov 7, 2009
    Posts:
    161
    It's great to see people sharing this kind of stuff, and got the same problem once and also solved it creating "zones", Excelent!!
     
  3. squared55

    squared55

    Joined:
    Aug 28, 2012
    Posts:
    1,818
    Very interesting read. I demand more!
     
  4. Wigen

    Wigen

    Joined:
    Aug 31, 2013
    Posts:
    33
    @MarioRuiz Thanks! Fun to know that other people solved it the same way. What game did/are you working on that needed it?

    @squared55 Thanks! Glad you liked it! we plan to make more dev logs like this! and hope you guys enjoy them just as much! We have a lot more dev logs we'd like to post such as using a tablet/pc and using your android/iphone as the controller for the game or the powerups and the creation process behind them. Paragon looks awesome! great job on it. cant wait to play it!.
     
  5. david55527

    david55527

    Joined:
    Mar 24, 2013
    Posts:
    58
    Excellent writeup, I've just started designing the AI for my current project and this has given me some ideas on how to approach it. Thanks!
     
  6. Wigen

    Wigen

    Joined:
    Aug 31, 2013
    Posts:
    33
    Glad you liked it! and glad it helped you with ideas :) Thanks!
     
  7. DaveyD

    DaveyD

    Joined:
    May 7, 2013
    Posts:
    57
    Wow, this is AWESOME!! I'll save it for the record. Thanks a lot!!

    P.D.: I always thought you had to use a NavMesh for that.
     
  8. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,386
    Really cool write up! That looks like some really great work overall and the motor for the trucks is great.

    I'm working on some 3D physics driven flight AI and it's coming along but this sort of stuff is really nice to see and basically a goal for me in terms of quality and consistency. I'm not as far along as you but have run into a lot of similar issues. I'd love to see more editor shots, more about your avoidance system and the basics of your vector setups - especially how you did the soccer part, I'm really struggling with doing the vectors and establishing a safe point for an agent to go around or avoid something, seeing your debug lines is helpful, can you share your vector basics for achieving that?

    For me personally, pure code is hard to interpret since I'm doing it with FSM's in PlayMaker and can't code much at all, but any high level pseudocode would probably be easier to understand unless your regular code is pretty straightforward.

    Thanks for the post!
     
  9. SpaceMammoth

    SpaceMammoth

    Joined:
    Jan 2, 2013
    Posts:
    220
    Really nice article, I like the elegance and simplicity of your approaches. I've got a question - can your model accomodate different skill levels of the AI? Its sometimes nice to be able to put in trucks that are bad drivers and make mistakes, occasionally crashing - particularly for the easier difficulty levels.
     
  10. Doddler

    Doddler

    Joined:
    Jul 12, 2011
    Posts:
    265
    Just off the top of my head, you could probably have bad vehicles make mistakes reading some waypoints, like offsetting them by a random value, to get eccentric behavior. If you picked which waypoints this occurred at you could have them only make mistakes at corners for example.
     
  11. Wigen

    Wigen

    Joined:
    Aug 31, 2013
    Posts:
    33

    Basically I grab the goal and ball position and calculate the direction needed to hit the ball. I use the red dot as the "way-point" that the vehicle needs to follow. Add a factor of how far i am away from the ball to add to the direction so the further away from the ball i am the further the waypoint is out. I then check if the ball or player is closer to the goal and figure out an offset to make the waypoint go up or down to give it the curve.

    Its all relative to how far i am from the ball. so the further away i am the further the curve goes to get behind the ball. The values are all changeable in the editor so i can play around with how much the vehicle can curve or even have better aiming.

    as for the avoidance it basically ray's that check if something that is in the layers its checking for are touching, if it touches add a turn factor to whatever it was originally going. if i was turning .5 and the avoidance factor was .5. when the ray has a collision i then have a turn factor of 1. if it was turning -.5 and it collided i would be turning at a 0, so i wouldn't be turning at all. Ideally i want this to be on a curve so when it starts to touch something it turns at .1 and if it keeps touching it then have it go up. i will possibly add this in a new update but for now it works pretty well.
     
  12. Wigen

    Wigen

    Joined:
    Aug 31, 2013
    Posts:
    33
    yes it can, we actually have 2 different ai's running for testing, The ai script also has turn factors on how fast or slow it can take turns to waypoints. so it would be easy to just have some preset values for easy normal and hard difficulties. or even add a - turn value for object avoidance so they will smack into other vehicles when they see them. or even remove the avoidance all together.


    yup, something like this should be relatively easy to add once everything else is in place.
     
  13. Wigen

    Wigen

    Joined:
    Aug 31, 2013
    Posts:
    33
    Something we have also played with in working with the AI. On the race level they were just too good.. even when you use turbo they were just too good. We have been playing with the evil that is rubber banding and its working out well. The AI has a limit on how far ahead they can be from you and how far back they can be as well. you as players do not rubber band, just the AI.

    It feels more like a race with everyone around the same position trying to get to first place.
     
  14. HPMPRacer

    HPMPRacer

    Joined:
    Feb 16, 2014
    Posts:
    28
    Excellent post - thank you for sharing!
     
  15. lockbox

    lockbox

    Joined:
    Feb 10, 2012
    Posts:
    519
    Thanks for the dev diary write up and sharing your work! Excellent!

    Just some thoughts....

    There are many options for setting up the driving opponent AI. It really depends on style and type of players you're after. What you don't want to have happen, is that the player notices what you're doing and take advantage of that mechanic to win in the final lap. Or if the player just stops or comes to a crawl, will your AI cars do the same? That would be funny.

    What you could do is have low, medium, and high difficulty levels, and set the speed of the vehicles based on level of difficulty - and then adjust those values based on player performance. That gives the player the opportunity to catch up if they fall behind without implementing a flocking type racing behavior. It's okay if the player doesn't do so well as they are learning to play it, but as they improve, you want your AI to respond accordingly. As the player's skill level improves (i.e. gets close to the next difficulty level speed), you can prompt them to say/ask, "We notice you're doing really well! Do you want to switch the level of difficulty to [medium/high]?" Or if they started on medium/high and they don't do well after X number of races, you could automatically drop down a level (or two) without telling/asking them and then automatically increase level or adjust speed to match their performance.

    As with all racing games, you just need to make it challenging to win. Coming in 2nd or 3rd is never a bad thing. It motivates players to want to finish first - but if it becomes to easy to do that, then it becomes boring.

    Keep up the good work! I'm looking forward to more updates. :)
     
  16. Wigen

    Wigen

    Joined:
    Aug 31, 2013
    Posts:
    33
    Thanks for your input lock, That is actually something we considered and may be something we add in the very near future. We do have a style of rubber banding for the race AI to make it easier but still challenging even if you played perfectly or did real bad the AI stays around your current place. If you are in last place the AI slows down a little bit so you can get closer and once you are close it stays at 100%. This is by all means not perfect whatsoever but it gets a playable build and a fun ai to play against. One thing the computer does not do is take advantage of the level design and jump over areas to get to a checkpoint quicker, and while a player can do this the computer wont unless it gets knocked into that area. (something we will change when/if we add difficulty)

    We are going to release a minimum of 3 game types at release with racing being one of them. If there is interest in more racing we definitely plan on making the AI better, adding difficulty, and adding way more race tracks, but if we find that people want more of the fun levels instead.. then we are going to concentrate on making those more awesome.

    Thanks again!
     
  17. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    That's really cool. The scene with the truck, hay, fence reminds of Tractor Tom.

    In May I'm using a 3rd party flying asset and I was wondering how to do 'herding', as in a sheepdog herding sheep. It looks like this code could be adapted for that scenario into the 3rd party package.

    I can do some beta testing if you need a iPod touch 4th Gen (iOS 6.3 - iOS 7 it won't do). I also have a larger ultra-cheap Android 4.x to test with too. I'm not counting my PC (Slate / i5 / Intel HD Graphics 1st Gen) because Unity doesn't support the touch and gyro yet it has or my old MacMini (Lion / Core2duo / Intel 945 Graphics).

    PM me if you need testing help.
     
  18. dibdab

    dibdab

    Joined:
    Jul 5, 2011
    Posts:
    976
    it's a nice system approach. I guess modular. vehicle prefab w physics, damage and event trigger. and rigidbody physics for terrain objects (ball etc). now you got to come up with levels. you got truck soccer one on one?

    I had this rocket shooting player and waypoints w zones (for race position) in CrashNBurn military base test track. might reimport into 4.3 see whats still workin.
     
  19. Wigen

    Wigen

    Joined:
    Aug 31, 2013
    Posts:
    33
    hah, I can see where you would get Tractor Tom..

    herding can be very easily done with this approach. I really just have an AI script that's different for every level and I attach it to the things I want to have the behavior. having the herd run away from the players is easy enough, and if they have things like object avoidance they wont run into other sheep.

    We are getting our build ready for testing, just finishing up a few UI things on both phone and pc. I'll let you know once it is available :).



    We have 3 levels currently playable, Soccer, the Hay one, and Racing. Each can have AI or not and you can pick how many AI vehicles you want. We have plans for many many more levels but we wanted to get our structure done first. Every level we make after this should be extremely quick. (AI, level model, scoring.. the rest should be automagic)