Building a Traffic System in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

That would be very helpful thank you

👍︎︎ 1 👤︎︎ u/NikVundle27 📅︎︎ Aug 05 2019 🗫︎ replies

nice vid and good/fast presentation. subbed. you can do mini series on same topic with 2 or 3 videos max. so you can dive into details and also change the topic shortly after the series for non interested viewers. for example, this video topic is good but it feels like it cut half. thanks.

👍︎︎ 1 👤︎︎ u/razzraziel 📅︎︎ Aug 06 2019 🗫︎ replies

Clear explanation, and nice visuals to look at! I appreciate the post-process on prototypes. I always find that its easier to enjoy developing something that looks nice. Subbed

👍︎︎ 1 👤︎︎ u/Gizambica 📅︎︎ Aug 06 2019 🗫︎ replies

Love the quality of your videos. Thanks for sharing!

👍︎︎ 1 👤︎︎ u/AmAutoma 📅︎︎ Aug 06 2019 🗫︎ replies

That vid is so well done, i wanted to subscribe but I already am :(

👍︎︎ 1 👤︎︎ u/McRiP28 📅︎︎ Aug 06 2019 🗫︎ replies
Captions
does an action-adventure game like Marvel spider-man an open more driving sandbox like Grand Theft Auto and an open-world RPG like assassin's creed all have in common cold-blooded murder well yeah actually I suppose they do say what you want Spidey but there's no way that guy would survive that fall what I'm actually talking about though is artificial intelligence or more specifically traffic based AI now it's worth mentioning that many of these games have incredibly complex systems for handling AI that would be way too difficult and complicated to cover in a single YouTube video however you might be surprised just how easy it is to make some believable traffic AI with just a few lines of code and some elbow grease not literally just you know there's going to be a lot of clicking involved if the environment affords it and would commonly accommodate other people such as a town city or maybe even a Paris Fashion Show adding a level of perceived autonomy and guided movement to your NPCs can act as a really nice backdrop and really help bring your project to life so let's just think of today's episode as an exercise in virtual set dressing hi there I'm Matt and welcome to game dev guide in this video we're going to take a look at how to use a simple Waypoint system to build a basic AI behavior for pedestrians and traffic we're going to look at how to build and draw a waypoint system complete with easy visualization and manipulation in the scene view we're also going to look at how to use these waypoints to move our NPC characters and also build support for these waypoints to be branched to allow for more natural movements so with that in mind let's get started I don't mean to introduce you to Eric Eric likes going for walks so we want to give Eric's some places to walk I've set him up with a super simple character controller as you can see in the script here we can set a destination for Eric to walk to and he'll animate his little pail yourself over to wherever we tell him to go when he gets within a set distance of his destination the flag the destination has reached we're going to start by building a super basic connect-the-dots style Waypoint system that Eric can walk between but this is me so rather than just making a bunch of game objects manually we're of going to build a tool that lets us place edit and visualize waypoints let's create two new scripts one called Waypoint and the other will call Waypoint manager window and place it inside of an editor folder in our Waypoint script let's create two references to a Waypoint one will be the previous Waypoint and the other will be the next Waypoint we'll also create a float called width which will cap at a range of 0 and 5 for now let's also add a method called get position this will return a random point based on the Waypoint width and basically we'll give some degree of freedom for our characters to move between when they're moving towards a Waypoint in our waypoint manager window scripts will create a quick static method to open the window we're then going to build a simple window that allows us to easily manage our waypoint system let's create a public transform could Waypoint routes this will be an object we assign that we'll use as the parent for our waypoints and on GUI let's create a serialized object for the window so we can easily draw its properties then let's create a property field for our Waypoint route and make sure to apply the modified properties and let's add a message to make sure it's assigned if it isn't we'll create a help box message if it is we'll create a method could draw buttons which will enclose in a vertical layout and here we'll create a button and a method for adding away points and here we'll create a new game object and generate a name for it based on the child count of our Waypoint route we'll also assign the Waypoint component then let's set this new Waypoint as a child of our Waypoint route and we want to make our Waypoint system automatically linked waypoints when we create a new one so let's get a reference to the Waypoint component and if the route transform has more than one child we'll assign the previous Waypoint like so just for some nice quality of life features we'll also set our new Waypoint facing the same direction as the previous one and finally we'll select the new waypoint as we probably want to edit it now if we go back into unity and open our window let's create a new game object here which will cool waypoints then let's assign this into our window and now each time I click this button we get a new child added to our Waypoint group and each child has our Waypoint component attached so this is a good start we've got a tool that will easily allow us to spawn new waypoints the next thing we need is some easy way of seeing how each of our waypoints are connected something ideally that will help us tell which way point is attached to which and the relationship between them it will also be good if we could easily just select the Waypoint here in our scene view rather than having to find a specific Waypoint in the hierarchy so let's create an editor script that uses some gizmos to help us achieve this in our editor folder let's create a new script could Waypoint editor at the top here will create an initialized onload attribute then let's add a new static method called on draw scene gizmo which will take in a waypoint and a gizmo type as parameters we'll then add the draw gizmo attribute and we'll add gizmo type non-selected gizmo type selected and gizmo type pickable this way we're telling unity to draw the gizmo regardless of whether or not the game object is selected and by declaring this as pickable were also telling unity to make the gizmo selectable at the start here let's set the color of our gizmo depending on if the game object is selected or not so if it is selected it'll be a solid yellow color and if it's deselected it'll be slightly transparent then we'll draw a sphere in the center point of our transform next we want to draw a line the width of our current waypoints walkable area and now let's draw a line from our current Waypoint to both the previous Waypoint and the next Waypoint [Music] now if we take a look back in unity we should be able to see that our weight points are now visualized in the scene view so this is pretty good however if I want to add a waypoint between two existing waypoints it's a little awkward so let's fix that by adding a couple of contextual buttons to our editor back in our Waypoint manager window let's get a reference to the currently selected game object and check if there's a waypoint attached if there is let's create three new buttons and methods our first button will label as create Waypoint before our second create Waypoint after and our third remove Waypoint then let's make a method for all three of these and assign them to our buttons in our create Waypoint method let's create a new Waypoint and get a reference to it then let's also get a reference to our selected Waypoint and we'll position our new way point with our selected Waypoint we'll check if the selected Waypoint has a previous Waypoint assigned and if it does we'll set that as our new waypoints previous Waypoint instead and set our new Waypoint as the next Waypoint [Music] finally let's set the sibling index of our new Waypoint so it's in position nicely in the hierarchy a create way point after method is pretty much the same except that we're swapping out the next Waypoint reference instead so let's just go ahead and do that finally in our remove waypoint method we'll swap out the reference on our currently selected waypoints to the previous and next Waypoint before destroying a selected game object so now we've got a pretty flexible tool for editing our waypoints and a nice visualization for them so with that let's go through and create a little Waypoint loop farik to walk around [Music] we'll assign our last Waypoint back to the first one to close the loop here and [Music] now we just need to write some logic to move between the waypoints so let's create a new script called Waypoint navigator in here we'll get a reference to our character movement controller and a current Waypoint on awake we'll assign the controller and then we'll set the destination of our controller to the position of our currently assigned Waypoint then in our update method we'll check if the controller has reached the destination and if it has assigned at the next Waypoint and set the destination from that Waypoint so all we have to do now is assign a waypoint here and then click play we can see that he makes his way around the waypoints go Eric go you ten points for basic AI hooray now obviously you came here for more than a guy just following some waypoints so how about we make a whole load of guys follow some waypoints let's create a new script on our Waypoint route called pedestrian spawner we use this to create multiple versions of Erik and assign him to a random Waypoint at the top here we'll create a public reference to our character prefab then we'll set a variable to determine how many of them to spawn and let's create a KO routine called spawn which will iterate through our way points for every spawn number we'll simply instantiate the prefab then get a random child from our waypoint list and set that as a current Waypoint for our character and then move them to that position so back in UT if we set the number to say 100 and hit play we'll get a little bit closer to creating some actual traffic we fly around our little square here we can see that we've got loads of Eric's walking around in circles hooray one of the small touches I've added to Eric is a marginally random speed upon spawning this makes the traffic move in a little bit less uniform way and makes it a little bit more realistic because obviously not everyone is going to be moving in speed so this is good but it could use a bit more variation after all not everyone in traffic is heading in the exact same direction so let's start by adding support for our character to walk the other way around the waypoint system here in our Waypoint navigator script let's add a number for Direction so let's randomly generate this number when the script starts and then down here when our direction is zero we'll assign the next Waypoint as our current Waypoint but if the direction is set as one we'll assign the previous Waypoint as our current Waypoint and now if we take a look we have a much more natural flow to this sidewalk we're making our way to a more realistic pedestrian traffic system so it's nice that they're walking in circles but we really want to give the impression of a level of autonomy to our little guys the problem really lies with the way we've built our current way points we want points in our system where we can give our guys a chance to break off and head down a different path so we need to add support for branching let's go into our Waypoint script and add a new list of waypoints code branches and our Waypoint editor let's visualize our branch by drawing a blue line to it and then in our editor window let's add a button to add a branch with ease and so creating the waypoint here is pretty much the same once that's done we then assign it as a branch to our old way point next let's head over to our Waypoint script and add a new float to it called branch ratio giving it a range of 0 to 1 with a starting value of naught point 5 in the update method of our Waypoint navigator script let's check if there's a branch on our current Waypoint and if it is let's do a random check against the branch ratio to determine whether the branch should be taken or not so then if we're choosing to branch let's randomly pick a branch as the next Waypoint position the part of this will mean that we might run into some dead ends so we'll now need to handle that too let's check if the next or previous Waypoint is null and if it is we'll change direction [Music] [Music] now back in unity we can create our brunch let's grab the node by the crossing here and add a new brunch will then pop it into position and create a new Waypoint and move it to the other side of the road then let's create another branch here and start placing some more waypoints so if I just play this we can see that now when our characters reach this point sometimes I'll split off and begin on their new path annoyingly though we end up in a situation here where they turn back on the crossing because they don't definitely take the branch off and this is where the Waypoint ratio comes in at the end of each crossing I want to guarantee that they go back onto a track so I'll select this exit branch here and give it a ratio of 1 now anyone reaching this node will always branch onto the sidewalk on the other side we'll move our entry node over and create a new exit branch node and also set its branch ratio to 1 now as you can see here we have an entry node onto our crossing from the sidewalk with a random chance of branching and another exit node off of our crossing which will always be branched from using the Waypoint system on a crossing like this with entry nodes and exit nodes means that anyone that has branched on to our crossing will now successfully leave it as you can see here the branch ratio gives us more freedom over how our eye uses branches and allows us to create more advanced control over our traffic flow so I'm really happy with this all that stuff to do really is go through and add a few more branches and continue these waypoints eventually we end up with something like this it's just a little bit of work we've got a nice and varied pedestrian traffic system now obviously there's a lot more we can do here for instance if you wanted to take it further perhaps you could add some variation to the waypoints maybe have some spots where people stop and look at buildings or perhaps adding some animations so people do different things and move in different ways there's a lot more to explore to really flesh this out but hopefully this is a great starting point for you and that's pretty much it for this video so let me know if you enjoyed it by hitting the like button below and leaving a comment definitely let me know as I'd like to make another video in the future taking a look at some more complex behavior for this traffic system by perhaps adding vehicle traffic and having the to interact so pedestrians will wait to cross the road and vice versa there's a lot more I can do here so I may also look adding some more complex interactions in to additionally I put quite a bit of time into building the demo scene and finding the best way to present all this information including spending quite a bit of time messing around with the post processing stack to achieve this tilt-shift miniatures effect so I'm definitely likely to explore how to do that in a future video if either of those are something you're interested in seeing then be sure to subscribe to the channel because you'll be among one of the first to see a new video whenever it goes live additionally if you want to see more videos like this then feel free to check out some of these other videos already on the channel otherwise as always thank you very much for watching and I will see you again next time
Info
Channel: Game Dev Guide
Views: 163,708
Rating: undefined out of 5
Keywords: unity, learn, games, gamedev, how to, making games in unity, tutorial, unity tutorial, learning unity, unity3d tutorial, programming, making games, vehicles, pedestrians, spiderman ps4, marvel, traffic system, traffic ai, waypoint system, pedestrian traffic
Id: MXCZ-n5VyJc
Channel Id: undefined
Length: 17min 55sec (1075 seconds)
Published: Mon Aug 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.