Unity DOTS NavMeshQuery - Pathfinding

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Nish agents are not yet supported by unity dots so is there any other way that we can make AI work you can still create your own PA finding Solutions but if you want to start quickly prototyping games you can use snow mesh cury which is part of unity's experimental AI to my scene I will just add some ground some obstacles as well as a player and an enemy so that's my scene I have all of the environment objects under the environment parent on which I put the no mesh surface this component which is just part of unity and on all of the environment objects that we have under it I put the no mesh modifier and I ticked overr right area on the ground I selected that the area type is vable and on the obstacles I selected that it is not vable now when we select the parent where we have the no mesh surface component I also set the collect objects to no mesh modifier component only so it is selecting only the objects that we have marked with the Nish modifier component and we can just hit bake so now we can see where the AI will be able to work we can also go to window Ai and open the navigation window where you will be able to change the radius of the agent and so on I have also created a subscene under which I have the enemy with a player and they have no additional components on them to use the data oriented technology stack with AI we'll again need some of our classic packages we'll need AI navigation burst collections entities entities graphics and the mathematics package and because the no mesh cery is still experimental and there are some features missing we'll need to download the PA utilties script and you can find it in the description on GitHub so just copy all of this code create new script and put it into it and we can finally start coding so I will create the Nish agent component on each of the NES agents we want to be storring the entity that it should be following we also need to know if the NES cury has already calculated a path for it and when we calculate the path it will give us a list of way points so each of the norish agents needs to know on which Waypoint it currently is then we can also give it some movement speed and I have a variable for the next PATH calculate time which will just tell us when it should calculate the next PATH because we probably don't need to be calculating the path each frame and as I was talking about the waypoints we'll need to store them somewhere on the AG of but in dots you don't usually use lists instead you use something called Dynamic buffer to create a dynamic buffer we need to know which types it will be storing so I have created new Waypoint buffer which is again struct and it is inheriting from I buffer component data and it is just holding float free for the position of the Waypoint in here you could also Define multiple variables for one of the way points but right now we just need the position ition and later we'll add the dynamic buffer to the no mesh agent which will be holding multiple of these Waypoint buffers and now let's create the Nish agent authoring script we have our classic authoring script which will allow us to set the target transform of the Nish agent as well as its movement speed the important thing here is that when you are getting the entity of the authoring entity you set the transform usage Flex to dynamic because we'll later need to access the transforms of the noes agent then I'm just adding the no agent component and I'm setting the target entity and move speed when I'm setting the target entity we need to convert it from the transform to entity and again because we will need to access its transform I'm setting the transform usage Flex to Dynamic and what may be new to you is that I'm adding the Waypoint buffer to it so that later in the navigation system we can just access it and add waypoints into it now we can go go back to Unity and add the no agent authoring script to our enemy I will set the target transform as the player and just give it some movement speed and when we select the enemy entity and go to the runtime mode in the inspector then we can see that down here we also have the Waypoint buffer which currently is just containing zero elements but we can easily add elements into it which are just containing the positions of our waypoints but don't worry you won't need to set these way points by your self it will be all calculated by the Nesh cury I created new NES agent system in which you'll be calculating the path from the enemy to the player and we'll also be moving with it I have added many name spaces that we'll later need to use such as Unity engine entities the important one is Unity engine. experimental. which will give us access to no M cury we also have collections mathematics transforms and burst I addit burst compile attributes to all of these functions as well as the main struct the first function on update will be just going through all of the entities checking if they are actually the Nish agents and then it will be deciding if they should either move or if we need to calculate the path we have our classic for each Loop where we are going through all of the entities in the scene and we are only interested in the entities that have the norish agent component as well as the local transform here when I'm defining the variables I'm using graph RW which means that we will be able to read from the value but we'll also be able to set it some value I'm also calling that with entity access so that we can save the entity into a variable so we have a variable no agent transform and entity first I'm getting the dynamic buffer from the enemy which we have already added in the authoring component when we are adding the buffer so here we are just getting it using the state. entity manager that get buffer from The Entity and then because as I said we don't want to be calculating New Path each frame I'm just checking if the next PATH calculate time of the NES agent is less than the eleps time which means that we should calculate New Path if this is true I'm just adding some time to the New Path calculate time I'm setting the path calculated to false and I'm calling the function to calculate New Path and if this is not true it means that we don't need a new path and we can just keep moving on the old one so I'm calling the move function now we'll get to calculating the path where most of the code that I will be writing is taken from the forging station YouTube channel so you can take a look at it I just wanted to show you a simple version of noes cury so the code may not be the most optimized but in the next Parts we'll also make it jifi I have eded few more parameters to this function that we all need such as the reference to the no agent component the local transform the dynamic buffer and the state which I'm all inputting when I'm calling the function but we already had access to all of these variable before then the first step to using noes query is to actually create instance of it so I'm creating new NES query called cury to create it we need to get access to thees world so I'm just getting it using the that get default World command then we need to allocate it some memory even though we are not using a job I'm using the temp job because when I was using just the temporary allocator it was giving us an error and I'm setting the path node pool size which is the number of nodes that will be temporarily stored in the QA during search operations then I'm also storing the front position which in this case is just position of the no agent so position of the enemy and the two position is position of the player that we have in the component named Target entity so from the target entity I'm getting a local transform and from it I'm accessing the position and when we are setting the start and end position of the no mesh cury we are not actually working with float freeze instead we are working with no mesh locations so I will just create those so I'm getting the from location and two location using cury map location where we just need to input the position the extends and last one is the index of the NES agent for now it is just zero because when we go back to Unity we can see that we have just humanoid type now we'll check if those locations are valid and we'll begin finding the path I have created pu Q status just called it status so later when something goes wrong we have a status from which we can get some information about what went wrong first I'm checking if the from location and two locations are valid and then as the unity documentation says we need to call the begin find path update find path and end find path in this order order so I'm setting the status to cy. begin find path inputting the from location and to location then if the status is in progress which means that we can continue I'm setting the status to q. update find path where I'm inputting the iterations this number is important because if you have really long path you may want to increase this number and if updating of the path was successful we can end finding path we can get the path result and lastly find the straight path using the path utilities so if updating of the path has succeeded we can end def find path and then I'm creating all kinds of native arrays and variables but most of these we won't really need them they are just for the fin straight path function the most important native array of all of these is the result which will be storing all of the no mesh locations another important part is the get PA result which we are calling on the cury which will get us all of the polygon IDs but from this we can't actually get the locations so we need a help of the path utilities on which we can call the find strain path function which you can see that it is taking quite a lot of parameters and after the returning status has succeeded we can do anything we want with the locations so I will just add them to the dynamic buffer of the NES agent so if the returning status succeeded we can clear the wayo buffer because we'll be adding New Way points to it then I'm going through all of the no mesh locations in the result array I'm checking if the location. position is not equal to vector. 0 if this is true we can just add it to the buffer so Waypoint buffer. add new Waypoint buffer and I'm setting the Waypoint to the position of the location after this the NES agent should start on the first Waypoint and the path is calculated and I'm also disposing all of these arrays and lists as well as the cury and as we start playing the game and select the enemy down in the dynamic buffer you should see all of the white points where the enemy needs to go in order to reach the player so if I try moving with the player those values should be changing and you also want to make sure that the player is actually on the ground and not flying in the air otherwise it is not going to generate these way points so now the way points should be different yep now we have just two and if I would move the players somewhere over here there should be many more way points yep we have like eight of them and lastly we'll get to the move function where we'll actually make the enemy follow the player to the move function I have also added some input parameters that we'll need such as the no mesh agent transform Waypoint buffer and the system state which is the same as in the calculate path function first we need to check if the enemy has already reached the current way point so I'm checking for the distance between the enemy and the current Waypoint if this is less than let's say 0.4 I'm checking if there are some more way points available otherwise if it is the last wayo we don't want to be moving further and then we can just calculate the move Direction so I'm subtracting the current position of the enemy from the position of the current Waypoint based on this I'm then moving the enemy so I'm normalizing the direction multiplying it by the Delta time as well as multiplying it by the movement speed of the enemy and to get the angle at which the enemy should be rotated I'm using the ma. degrees function because the output of the ma. 8 and2 is in radians so we want to have it in degrees in the ma. 8 and2 function I'm just reversing the x and z and then to set the rotation to the enemy I'm using the slurp function into which we input the current rotation the rotation that we want it to be and the Delta time I know that this is quite a lot of code just for some basic AI but it is the simplest that it can be and it definitely is not optimized very well because it is still just running on the main threat so in the next part we'll take a look at how we can make it more optimized and we'll also use jobs and now we can see that the enemy is starting to slowly follow the player and when I take the player and try to move him somewhere else yep the enemy is recalculating its path now let's see how it works with multiple enemies so you can see they are all chosing a bit different paths and they are all slowly getting towards the player now I could move with the player at any point and the enemies would just generate new path to the player so this is really the simplest way that I could think of of how to implement no mesh agent with unity dots if you have any suggestions about what I should make my next tutorials let me know down in the comments I hope that this video was useful if you have any questions you can also drop them down to the comments don't forget to like subscribe and I will see you in next videos bye thanks for watching this video till the end if you are looking for a Unity C or bold tutor then I am here for you so feel free to send me a message to my Gmail and take a look at my website for more info I can help you with your personal projects or teach you anything about game development you would want to know you are welcome
Info
Channel: Freedom Coding
Views: 1,791
Rating: undefined out of 5
Keywords: Unity, Unity2D, UnityEngine, GameDevelopement, IndieGameDev, PCGames, Coding, C#, Easy, Tutorial, Education, Unity3D, Programming, Bolt, UnityTurorials
Id: boT42zuWjwc
Channel Id: undefined
Length: 14min 22sec (862 seconds)
Published: Mon Dec 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.