Advanced AI in Unity [Tutorial] - Physics, Pathfinding, Editor Adjustments

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone today i'm going to be showing you how you can make an enemy ai so the enemy can actually follow you wherever you go so you can see here i have a game open and this is actually from my devlog series of a game that i'm working on and you can see that this enemy spider is able to easily follow me wherever i go and also when it reaches an obstacle it also even jumps and so example if you go above the spider this fighter will jump so yeah whenever this green line is pointing upwards the spider will jump and yeah i'll exit out of this and show you a few things so if we go over to the right here you can see the correct script is this one right here yep so you can set any target that you want for your ai you can have a certain distance that will be activated from you'll have and how often you want to update and all kinds of other behaviors that will make completely customizable so when you include this in your game you can change them however you want all right let's get started so before we can get started with our path finding we need to make sure our scene is set up correctly so you can see right here i have a simple scene set up and what i'm doing right here is creating a polygon collider so what this will do is allow us to have good collisions in our game so just make sure you have a polygon collider or a tile map collider or just whatever you're using for your game next what we need to do is make sure the layer is correct so we have right here a tile map and that's where the polygon collider is attached to so let's go ahead and create a new layer and let's just call this layer ground alright if we go back here then we can set it to the new layer okay now i'm just setting up a player character with some basic behavior so he'll have a capsule collider in order to collide with the environment around him so let's just make sure this is working in order to make sure it's working we also need to have a rigid body because everything we're doing here is attached to unity's built-in physics engine that they already have so let's see if this is starting to look alright yep and you can see that we have the character and he's correctly colliding with the ground here okay awesome we can now get started with the enemy so i'm going to rename it to enemy right here so it's obvious and the first thing we can do is create a new layer and just create this as the enemy layer alright if we go back then we can set it to enemy perfect and we can also add a collider in this case uh i'm thinking we can go for a circle collider and i'll downsize it to something more reasonable yeah something like that all right it doesn't have to be exactly perfect and then after that what we need to do is we actually need to add a rigid body so go down and select the rigid body 2d and we want to yes have it simulated and we also want a little bit of linear drag here and let me see what else the everything else will have to be adjusted based on your specific game but we probably want to freeze the z rotation so that makes it so the object doesn't like turn upwards and downwards it's always facing the correct direction sort of all right perfect so that's all we need to do for that and next we can actually add uh sorry we can actually add a new script in this case and i am just going to call this the enemy ai script you can call this uh whatever you'd like however so yep create and add you can click that and then hopefully it loads up for you like it's taking a while for my game but alright let's see here then we can click on edit script and you can see here that we have just the general unity script which we're going to be adding a lot to now but actually before we add anything to this new script we just made we actually need to go online and download the a star project so just so you guys know this project that i'm showing you or this whole tutorial series is based on bracky's tutorial series so you can watch his for more information on exactly how the a star pathfinding project works and everything but what you need to do is you need to download the free version and then once you download it you're going to need to uh let's see here import it into your project which i can do just by dragging it in there like that and then clicking import all right now if something like this comes up you can just click reload that's fine no problem and then uh yeah if you go back here you might have some error or warnings hopefully if you have errors you'll need to investigate that but all right perfect so if you click on this we have the whole a star pathfinding project which provides us everything we could possibly need in order to get started and what we're going to do is actually import the correct a star game object that we need so what we need to do is we need to go just add create empty and then we can just rename this to a star and you can see it's at 0 0 here and then we can actually add the pathfinder so what this is is it allows us to create a full grid and any object can access this grid that we'll be creating here in order to pathfind to a specific target for example in our case the enemy right here is going to pathfind to the player so what we can do is we can create a new grid graph and yeah let's take a look at this so you can see here we have all kinds of values inside the editor and oops what we want to do is we want to select 2d because we're working on a 2d game here and then you can see here's the grid again so let's actually change this to like a hundred by a hundred and then we can click on the scale and actually bring that down a little bit okay actually it's not resizing quite correctly so let's try that again yeah so what we need to do here actually is change the node size to let's just say 0.3 as an example and move that up okay and so what this will do is this is the granularity or how specific the path finding will be so you can make this as general or specific as you want to in our case what i have it at right now is probably a bit too much so let's actually try 0.5 okay so it was kind of error erroring out before so i just needed to readjust the values again and you can see it's now actually working correctly so you can see it's correctly uh estimating how many of these blocks are inside or outside of the mesh and you can see here that there's a lot going on down there and that's just because i never actually finished up moving this mesh so let's just do that right now okay not that it matters because it's off screen but it's still nice to have it working perfectly and then if we go back we can actually just readjust the values again and it'll work perfectly i think there's a refresh section in here somewhere but i'm not exactly sure where but anyway you can see that this one is kind of a problem though it's not perfectly lining up as you can see the grid is just slightly outside there which is not great so maybe we do want to have the granularity be a little bit more or what we could do is we could actually move this over a little bit so i'm going to go into here and you can see i have the box collider so if i just move it in a little bit and if we uh where can i adjust it well i can just adjust it here yeah so if i move it back to 0.5 you can see it now fits perfectly right here and so just play around with it to get it to how you want and next we can actually start working on the enemy again so i'll just save this and i'll go back to the enemy next what we're going to need to do is look at our enemy script again and add a new script in so this is the seeker the seeker script is just what we're going to have to use in order to have something that we're following in our game and that'll also give us the gizmos which will be really nice and i will show you later how that all works but anyway we added the seeker script which is just a requirement to get this a star algorithm sort of working correctly and beyond that we have everything we need we just need to write the code at this point so let's open up our script here and right at the top here we're going to have to add using path finding because the actual package is different from the default unity package so we need to basically import that there and i'm just going to delete all this we're going to add it back later but we can create a new header and call it pathfinding so this is going to show up inside the script editor at the top there or back in unity and we want to have a public transform target public float activate distance and we can set a default value i'll just do uh i'll just do 50 for now and so of course this is going to be the target that the enemy is targeting that's the activation distance and the public float path update seconds equals 0.5 so this is how often we're going to update the a star algorithm that we're using and here physics we're going to have a speed and let's default this to 200 and then we'll have a next waypoint distance and we'll set that equal to three so that's how long it how far away the enemy needs to be in order to start moving towards the next waypoint instead of the current one public float jump node height requirement equals 0.8 f so this is how vertical the next node needs to be in order for the character to actually jump and then next what we have is the public jump modifier so this is basically just setting how high and how powerful the jump is for the enemy and then here we have the jump check offset this is more of a collider thing which you just need to adjust it to make sure it's right and then here we have the custom behavior so this is useful for if you want to have perhaps really dumb enemies or something so so we'll have follow enabled and this can be defaulted to true so if this is false then nothing in the script is going to do anything so perhaps i don't know you have your enemy die or something so you need to disable it and then the dump jump enabled if you want that and then direction look enabled equals true so that's to see if it will change directions or not and i by that i mean the sprite actually so you can see right here it looks really nice and we can adjust all of this straight from the editor so you can add this to a new enemy of a different variant and then you can change all of the values to make it completely original so yeah perfect let's continue now we're to the private variables actually so we want to have a path and that's one of the pathfinding features so we'll use the path that they provide us and then we want the private end current waypoint and that will be zero rule is grounded it goes false for the jumping of course and then seeker which of course is the other script that we have in here and then the rigid body we'll just call it rb so we can get started with making the start script here and we can just find the seeker so that we can use that correctly and then we'll also get the rigid body 2d perfect and then next what we want to do is we want to do invoke repeating that that a quick line of code here which is also from the brackies tutorial something that i was uh that i didn't really know about but what this does is it will keep on repeating the same script over and over again based on sort of like a co-routine sort of deal and yeah it'll do it every single path update seconds which is what we set up here because using the a star algorithm can be quite intensive for your computer so it's good to not have that running all the time next is the fixed update so what we want to do is we want to see if the target is in distance which is another thing that we set up there we haven't written this function yet but we will pretty soon here and we want the follow to be enabled if both of those cases are true then we're going to follow the path this too is something that we'll add and then we also want to make update oh sorry update path update path so this is another set of checks that we're going to need to do so first of all if the follow is enabled and also the target is in distance just like before and we need to make sure that the seeker is done so what this means is the object has been found that we're trying to seek basically and we want to do the start path and we want the position of the rigid body so the position of ourselves basically we need to find the target's position and then we have on path complete all right perfect all stuff that we will add soon don't worry so then we have a path follow which is what we started up again and this is the really long script so actually i'm going to copy it in piece by piece because it's really long so first of all we just need to make sure the path isn't null and also the waypoint is not already over like we haven't already covered all the waypoints so then after we do that then we can see if we are grounded and we make a raycast and this raycast just returns true if it is colliding with something which means we're on the ground then and then here we have a direction which is just calculated and then we want to also have a force applied well right here it's just created but we will apply it just based on the direction we need to go and then next we want to add the jump and sorry i will zoom out so you can see everything here and then for the jump we want to make sure the jump is enabled and grounded and if that's true then we can add the force for our jump after that we will actually do the add force for the movement direction and then we will calculate if we need to head towards our next waypoint which could be true or false depending on how close we are to the current wave point and finally we just need to do some graphics handling to see if we want to flip the sprite depending on if it's moving to the left or to the right all right yeah that should show everything perfect so below this we just have two more scripts to write or two more functions to write so first we have the private pool target in distance oh and this is another long line that i'll just copy in so it's just returning to see if we're inside the activate distance so that means that it's within the scope of the enemy and then the last thing we need to do is have the on path complete so inside here if we don't have an error then the path equals the p and the current waypoint is back to zero all right perfect so that's the entire script finished and let's actually go back and press play and see what happens okay so it's not working right now let's figure out what's going on right so the first thing we have to do is actually have the enemy have a target right now it has no target so we can just drag in the current player as the target really nice and simple yeah so you can see here that he is getting a target line for us however he's not moving well he is moving just very slowly so that's an example of like what i was talking before where we're going to have to actually change his uh values but you can see that when i go above him he is jumping which is pretty awesome all right let's get working on that so let's try changing his speed to 500 and just see what that does real quick yeah so you can see that's starting to look a little bit better and actually yeah so if i go to the other side of him he will turn away from me and you can see i am colliding with him which is uh pretty interesting but you can see for the most part by just changing a few little values everything is working pretty close to correctly you can see that there are still some issues with this for example like his jumping is a little bit weird but that's all based on adjusting the values over here so let's try to fix his jump modifier for example let's change that to point yeah 0.1 and let's see if that is a little bit better yeah so that's looking a little bit smoother however he should also have a little bit more uh gravity perhaps so we can have a higher jump modifier but also a higher gravity so he doesn't like float yeah however his speed also seems to be affected by that so let's change that to 9000 or 900 okay that doesn't affect it another thing i will mention is we should also change the collision detection from discrete to continuous so sometimes the objects can actually fall inside of the tile map which is really not good so yeah i've adjusted a few values here so let's see how things start working out uh we still seem to be having some problems but he can jump at some points and he kind of can fly which is a problem but yeah it's all about just adjusting these values now so the activate distance remember you want this to be a really high if you want to make it so your enemy can see you from far away but really low if you want for example the enemy to be right next to you and then they notice you and then they can start chasing you and also remember all of these can be changed through a script since they're public variables so you can have a different script that can make an enemy perhaps like way more powerful like if you get close to them like right when you get close to them all of a sudden you can increase their speed and increase all of their different variables or you can also have it so if an enemy is on low health you can have the jump disabled or something like that just different ideas for you so there are two more important things i need to mention real quick the first one is the collider needs to be centered at exactly zero zero so there needs to be no offset otherwise you'll have problems with your vector here and so you can see i made some changes to the script right here which i'll let you copy and change for yourself because actually the physics raycast that i had was not exactly correct and this will give you the correct collision value alright awesome that's the end of this tutorial so you now have a enemy that can chase the player around and even jump although it's not perfect you can go ahead and start changing some of the values here and look into other physics systems to see how they do their jumps and everything like that but this should be a good start for you
Info
Channel: Etredal
Views: 15,054
Rating: undefined out of 5
Keywords: Unity, AI, Pathfinding
Id: sWqRfygpl4I
Channel Id: undefined
Length: 24min 41sec (1481 seconds)
Published: Mon Sep 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.