NavMeshAgent Avoidance in Depth - 5 Key Takeaways for Optimal Avoidance | AI Series Part 32

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how do you make your navmesh agents avoid one another why do my agents stack on top of each other or push each other out of the way these are some of the most common questions i get asked since i started making the ai series and in this video we're gonna find out the answers to all of those as best we can using the unity navigation system hey chris here from mom academy here to help you yes you make your game dev dreams become a reality today's part 32 of the ai series and i'm really excited to dive in deep into obstacle avoidance using the unity navigation system before we go really deep and start looking at all this stuff though we need to understand what does unity use for obstacle avoidance nav mesh agents do have obstacle avoidance and they will try to avoid each other if you've configured them correctly first off an agent needs to have the same or higher priority for it to consider another agent an obstacle by default an agent will have 50 priority and if you set it to be lower they will just ignore all the other agents and walk right through them pushing them out of the way the navmesh agents have something called obstacle avoidance and you can configure this to be either none low medium good or high quality avoidance we're going to take a look at how those influence how the agents actually try to avoid one another a little bit later in the video but it's good to know that unity does have a way for you to control this unity uses the rbo algorithm for obstacle avoidance there's a better one rvo2 that is a lot better in keeping agents from running into one another or pushing each other out of the way unfortunately that's not implemented in unity that's not to say that the rbo algorithm is bad it just does not do it as well as the rvo2 algorithm does if we go to the website for the rvo2 algorithm there's a couple of videos that they have that you can download and look at how the agents will avoid one another we're going to recreate a couple of those scenes in unity so that we can see how does the rvo algorithm behave in these scenarios and what can we do to tweak the settings that unity gives us to make them behave a little bit nicer where there's less pushing and overlapping kind of stuff going on and as always the entire project is available on github you can check it out you can run the different scenarios you can see how it behaves on your machine you can play with all the settings if i didn't cover a particular setting you wanted to see or combination hey and just really quick i want to give a shout out to my patreon supporters i really appreciate it every bit helps the channel grow reach more people and add value to more people and that means more people are making their game development dreams become a reality if you want to help me in that cause you can show your support on patreon patreon.com you can get your name up on the screen you can get a voice shout out starting at the silver tier and some other cool perks special shout outs to raphael and andrew bowen for being the silver tier supporters i am so grateful thank you in this scene for the sake of simplicity we just have one big cube for our floor and at the top left you can see i have three buttons one we'll run a circle scenario one will run a narrow pathway scenario and one will run the one-on-one scenario we're actually going to start with that last one to run one-on-one scenarios because this is the easiest way for us to show the different obstacle avoidance types and then we'll see in those more complex scenarios how these obstacle avoidance types and how the avoidance prediction time and the pathfinding iterations per frame play into how these agents attempt to avoid one another in our script the scenario manager you can see that we have an avoidance type that will change between no avoidance somewhere up to high quality obstacle avoidance i have a simple navmesh agent prefab that is just a navmesh agent there's no rigid body on it anything like that that's the cylinders that you see on the screen right now this floor has a nav mesh surface that is collect objects children and you can actually see at the bottom of this inspector we have a nav mesh surface configuration that we can modify from here and one really interesting thing we're going to get into later is the nav mesh surface bake configuration that you use actually will impact how good your agents are at avoiding one another that was a really surprising takeaway out of this video for myself the settings we're using for the avoidance prediction time and the pathfinding iterations per frame are the defaults that unity gives us to start out with we'll run some scenarios where we modify these and show how they impact the agent's avoidance quality let's take a look at what happens whenever we run the one-on-one scenario with avoidance quality of none we'll see that the agents get an arrow pointing to wherever they're going and we'll see a little red dot appears below the blue agent and a little blue dot appears below the red agent this is where the agents are setting their destination to and the little red arrow shows the agent's current velocity we'll see that the agents run straight to each other and right past each other without stopping at all this is because we're using the no obstacle avoidance that means that they will not consider other agents as obstacles and will simply walk right through them this is sometimes a useful setting in games like zombie games or tower defense games where you need a large quantity of units on the screen that don't really care where the other agents are if we go and up that quality to low quality we'll see that the two agents still walk towards each other and once they're about four units away they kind of go off to the side and slow down a little bit until they pass each other and then they speed back up to full speed if we up the avoidance prediction time to five we'll see that almost as soon as they start moving they do that kind of slow down and going off to the side thing that they did before but they're much farther away from each other than they were before this is because they're looking with our velocity of 2 and our avoidance prediction time of 5 that gives us about 10 units away before they start considering moving out of the way which is as far as they are apart if we set the avoidance prediction time to 0.25 which is slightly lower than what unity recommends as a minimum we'll see that the agents actually run into each other before they start trying to walk away and avoid each other that's because our prediction time is too low and they actually are running into each other before they determine that oh i'm going to run into this other agent if we set to something like 1 we'll see that the agents get very close and then they kind of do this little dance and then move out of the way of each other if we leave the avoidance prediction time at one and up the avoidance type to be high quality we'll see that the agents do not run into each other but they come very close if we up the prediction time to two we'll see that the agents much more smoothly get out of each other's way they don't get so close before they start moving that's a more realistic look and if we up it to five we'll see that again from almost the very beginning they move off to the side if i randomize the priority of each of these agents that's where stuff starts getting to be a little bit more interesting with the randomized priority the agent that has a lower number of a priority will ignore agents that have a higher priority than them so in this case with the blue being 14 and the red being 41 the blue agent does not consider the red agent being an obstacle so they are going straight to their destination the red agent however does consider the blue agent as an obstacle and starts going around that agent now let's start taking a look at the different obstacle avoidance quality behaviors if we set the avoidance quality to be low and then select both navmesh agents where we've unchecked the randomized priority so they'll both have 50 priority we'll see this kind of star appear in front of both agents where the red represents somewhere that the agent should not move and the white represents an area where the agent thinks it can move in this case once they come within range where they notice they will start colliding see that the straight ahead one becomes red and the side ones become white as they get a little bit closer for the blue one the top parts become more red and for the red one the bottom ones become more red so they start kind of drifting more towards the white because that's where they think they can go if we upgrade it to the medium quality obstacle avoidance and run the scenario again we'll see that that star still appears but there's a secondary set that updates much more frequently that helps them avoid each other a little bit more smoothly if we run the same scenario this time with the good quality we'll see there's now 14 large squares and there's still that secondary smaller set that is now also 14. this is the high resolution understanding of the world for the agent so they can more accurately avoid the other agents and if we upgrade it to the highest quality and run the scenario again we'll see that there's an even higher resolution avoidance set of squares on both the static set and the one that moves around now that we have a relatively good understanding of the obstacle avoidance types and how the avoidance time impacts how they try to avoid other agents let's take a look at some of the more complex scenarios if we run this with the default avoidance prediction time and pathfinding iterations per frame and the avoidance type of no obstacle avoidance we'll see that all the agents will run straight towards the center never slow down and make kind of a cool visual effect but totally ignore all the other agents this is always how the no obstacle avoidance will work so let's try again with the low quality obstacle avoidance and we'll see that as the agents get closer into the circle some of them will be a little bit closer in some of them will start slowing down and as we get closer to the center we'll see that they actually start overlapping and pushing one another and that's kind of a weird behavior and not what we would expect based on what we just saw in the one-on-one scenarios the low quality obstacle avoidance ones they avoided one another so why is this happening now let's run the same scenario again and select a navmesh agent to see what's happening we'll see that this agent considers up to eight other navmesh agents for their obstacle avoidance and as we go around the circle all of them consider the closest eight napmesh agents cool let's run the scenario a little bit longer and see what happens as they get closer to the center we'll see that the agent considers different eight now and the ones behind them are no longer considered with a low quality obstacle avoidance it seems that this agent is only considering eight nearby navmesh agents and that's why some of these are pushing one another because those agents are not considering the agents that they're standing inside of for their obstacle avoidance so that's not ideal [Music] let's see what happens if we run the same scenario with a higher quality obstacle avoidance than low once the agents start moving let's select this navmesh agent oops it's still only considering eight though it does have this more granular understanding of the world so maybe they won't push each other no still some get stacked and some get pushed but there's less of that going on this time than there were last time so that is an improvement it's just sometimes they still get stacked which seems to be related to them only considering eight other navmesh agents let's run on the good quality one more time nope still only eight with a more granular understanding of the world let's see what happens there's still a little bit of stacking even less than before but there's still some of that going on so still not a wonderful result but it's getting better let's try it with a high quality when we run the scenario with the high quality avoidance and select navmesh agent will see that this is the problem navmesh agents will only ever consider up to 8 other navmesh agents for obstacle avoidance as the agents come to the circle we see that there is still a little bit of this kind of pushing where they get inside of one another because they're only considering those eight other agents this really granular understanding of the obstacles is working pretty well though because there's very few that actually get stuck and pushed but still it happens unfortunately unity does not provide you a way to select how many agents should influence this agent with the rvo algorithm it's locked to eight regardless of the obstacle avoidance quality if we randomize the priority and keep the high quality obstacle avoidance remember that agents with a lower priority will ignore agents with a higher priority so some of these agents as we select them aren't considering any other agents nearby as obstacles because they have such a low priority and this ends up not being a very smooth run because all these agents that have a low priority just kind of bulldoze through all the other agents that have a higher priority so a lot of agents get pushed and we still have some agents that get stacked on top of each other though the traffic jam in the middle is quickly resolved it's not a very elegant solution just for fun if we turn the obstacle avoidance quality all the way down keeping the randomized priority it ends up being more or less the same result there's still some that get stuck inside of each other many get pushed around and honestly i can't tell the difference between the two they look very similar so far we've only been messing with the avoidance quality let's start adjusting the avoidance prediction time to see what happens using a high quality avoidance since even with high quality avoidance sometimes the agents will stack on top of each other and push each other out of the way if we up this to 5 which is the highest value that unity recommends for most use cases and run the scenario one more time we'll see that the agent starts slowing down a little bit earlier and as they come to the center we already see some at the edge overlapping and as they get very much in the center it's really not good all the agents in the very center get pushed on top of each other though the traffic jam is resolved very quickly there's a lot of pushing that happens so if we try even upping this more to something like 25 we'll see that as the agents come towards the center even before there actually should be overlapping on the edges some of the outer agents are overlapping and in the middle it's still not very good so the solution is not always giving the agents more time to think about what they should avoid sometimes it actually makes the problem worse if we run it one last time where we upped the pathfinding iterations per frame to 5000 in case maybe we just weren't calculating stuff fast enough we'll see that the agents still on the edge start overlapping very early as they come to the very center there's a lot of overlap where they start standing on top of each other and pushing each other out of the way kind of warping as they start considering each other the traffic jam is still resolved pretty quickly but there's a lot of really obvious glitching that's happening as a result since increasing the avoidance prediction time did not help let's see what happens if we reduce it to 0.5 which is again the lowest value that unity recommends we'll see the agents get pretty close in the circle before we start seeing any overlap and there's actually very minimal agents overlapping at the center they have some edges that overlap but generally the agents do not overlap and clear this kind of traffic jam very quickly so we're seeing here that actually the lower prediction time helps the agents overlap less and resolve the traffic jam more quickly with fewer glitches which is kind of contradictory to what i would expect i would expect giving them a higher prediction time would give them more time to sort it out that appears to not be the case let's run the last scenario the narrow pathway scenario what happens here is we get four new cubes that are navmesh obstacles so the navmesh agents will not try to path into them we spawn 25 agents on each corner and send them to the opposite corner of wherever they were so you can see that the red agents are going to the bottom right the blue agents are going to the top left the cyan ones are going to the bottom left and the green ones are going to the top right we're running this scenario with all the default settings again two avoidance prediction time and 100 pathfinding iterations per frame and you can actually see that the hundred pathfinding iterations per frame here is a bottleneck because the blue agents have not begun to move at all the red ones went first then the green then the cyan ones in this particular scenario upping the pathfinding iterations per frame will improve at least the responsiveness of these agents and this is running with no obstacle avoidance so whenever the agents actually pass through they just walk right through each other that's still the same that never changes right if we run it with low obstacle avoidance and leave everything else the same we'll still see that there's that delay before the blue ones come in as the agents come to the middle eventually they start kind of jittering back and forth because they're pushing each other and some overlap on top of each other so this is a really bad kind of worst case scenario where you have 100 agents almost all of which are trying to come through this tiny narrow path they get very stuck and actually it seems like they may never resolve this eventually they will but that's a pretty bad experience right if we up the obstacle avoidance quality to medium let's see what happens here this time they're a little bit more hesitant to get into the middle and there goes again they're all kind of bunched up on top of each other some are overlapping some are pushing still not a great result though it does seem that this one's resolving itself a little bit faster if we run it with a good quality much of the same a bunch overlapping in the middle they're kind of pushing each other some are getting pushed a little bit extra and again this is coming down to because the agents are only considering eight other agents there's too many right next to each other that they need to actually consider in their pathfinding so sometimes they will not be matched with one and we'll start pushing that one out of the way and that's where we get this pushing and overlapping if we run it with the highest quality optical avoidance unfortunately we get the exact same result all of these get stuck in the middle this was the best result in terms of resolving that traffic jam in the middle but there were still some agents pushing one another and still some agents overlapping with one another this did result in much fewer of them actually overlapping and pushing each other out of the way but i wouldn't call that a great solution let's see what happens if we up the avoidance prediction time to be five and the pathfinding iterations per frame to be five thousand you will see that with the 5 000 pathfinding iterations per frame that all the agents start moving at the same time so there is that benefit that doesn't seem to help much we actually are getting more overlapping with the highest quality obstacle avoidance and more pushing than we've got with the default settings so let's try the inverse where we lower the avoidance prediction time to be 0.5 the lowest value that ugd recommends this one actually seems to work a lot better than the other ones there's a lot less overlapping and a lot less pushing than we got on any of the other high quality avoidance type results which has been the best result so far and they resolved that traffic jam pretty effectively if we randomize the agent priority keeping those settings it actually becomes a lot worse with the randomized agent priority there's a lot of pushing other agents that happens because remember that the lower priority agents do not consider any of the other agents so there's a lot of this agent will just push another agent because it doesn't consider it as even being on the nav mesh so if you want really good avoidance your agents need to be the same priority the benefit here is that there is no traffic jam the lower ones just kind of plow through all the other ones and that's it they'll consider the ones that are the same or lower and try to avoid those but the lower ones will just ignore the higher ones let's run this one more time with a low quality avoidance with a 0.5 avoidance prediction time because this worked really well with a high quality avoidance type this one works relatively well as well so in high traffic areas it appears that having a lower prediction time actually helps it resolve traffic jams more effectively with lower amounts of agents pushing each other and with lower overlap now we're going to get into the really weird stuff remember that we have this nav mesh surface configuration in the navmesh surface you have the option in the advanced section to override the tile size and override the voxel size generally the voxel size controls how accurately the navigation mesh is generated from the level geometry the smaller voxel size you make increases the build time more and the tile size controls how local the changes of the world are and they say the small tile size allows more local changes while potentially generating more data overall so far in almost every single video i've done i've left these alone but i thought you know let's just play with them in here so i did here was maxed out the tile size and the voxel size so the tile size only will go up to 1024 and the boxes size will only allow you to go down to 0.01 if we do that run the scenario again the nav mesh gets rebaked look at the difference between how the agents behave now versus the last time i'll run them side by side still keeping 0.5 avoidance prediction time with a high quality avoidance prediction type the pathfinding iterations per frame to 5 000 with this very granular nav mesh we have zero agents that overlap with one another there's very few that end up in a traffic jam in the middle and all the agents very quickly reach their destination this was very surprising to me that the nav mesh bake settings very much impact how well our agents avoid one another by making the agents take slightly different paths with these different bake settings it completely resolves this narrow pathway scenario i'm not saying that this will resolve it in 100 of the use cases i'm saying what we're seeing right here is that these bake settings do impact the agent behavior including the obstacle avoidance behavior somewhat so it is important that you play with these settings if you're getting a lot of issues with agents that overlap one another if we keep the exact same settings and run the circle scenario we'll see that the agents do not overlap at all with the jitter they do have some edges that overlap but there's no agents totally pushing each other out of the way and stacking on top of each other [Music] to recap what we've talked about so far obstacle avoidance in unity does work pretty well most of the time provided you have too many agents trying to get into the same area at the same time because they will only consider up to eight eight other nav mesh agents regardless of the quality of the avoidance that you set up and that's usually when you end up with the agents pushing each other out of the way and getting stacked on top of each other agents with a higher priority will get pushed out of the way by agents with a lower priority so if you want all of your agents to consider each other obstacles you need to have all the agents set to be the same priority and very surprisingly to me if we update the navmesh base settings to increase the tile size and decrease the voxel size if i only do one it does not make the same thing happen that we just saw you have to do them both but if you update both of these and in my case i set them to be the maximum and minimum values respectively and that really cleared up a lot of the pushing that we saw there partially because of different paths being taken and partially because it seemed like the agents stopped pushing one another somehow something was going on with the increased tile size and lower boxes size that allowed them to avoid one another more smoothly so if all else fails that's something you can try in your game but remember that this will both increase memory usage and increase the nav mesh size so if you have really large snap meshes this might not be the solution for you to use and while i didn't specifically call out performance in this the profiler was running the whole time the lowest quality settings so the none actually took between 0.3 and 0.4 milliseconds for it to run if i set it to be the highest quality one it took between 0.6 and 0.7 milliseconds to run generally speaking even when we have all the agents together trying to fight each other to get wherever they're going to go still it was very fast 0.6 to 0.7 milliseconds with the highest quality avoidance is very good but this was with 100 agents and since the entire thing is available on github i highly recommend you check that out run the scenarios see how it performs on your machine and understand the performance on your target platform to see the difference between none low good medium and high i think i said those in the wrong order anyway if you've been getting value as a video or the series please consider liking and subscribing to help the channel grow reach more people and add value to more people there's new videos posted every tuesday and i'll see you next week
Info
Channel: LlamAcademy
Views: 29,326
Rating: undefined out of 5
Keywords: Unity, Tutorial, How to, How to unity, unity how to, llamacademy, unity navmeshagent, unity navmesh agent, navmeshagent, navmesh agent, unity navmesh, unity avoidance, unity obstacle avoidance, unity navmeshagent obstacle avoidance, unity navmesh agent obstacle avoidance, make agents avoid each other, agent avoidance, unity agent avoidance, navmeshagent avoidance, navmesh agent avoidance, agents avoid each other, obstacle avoidance
Id: dHYcio6fRI4
Channel Id: undefined
Length: 22min 4sec (1324 seconds)
Published: Tue Nov 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.