Creating your first animated AI Character! [AI #01]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
kia ora welcome back i'm the kiwi coder and this video is the start of a brand new ai series i'll be doing in unity uh so here you can see a ton of ai characters all following the player so each character basically is selecting an animation to play based on the speed of a navmesh agent and i'll show you some tips just to minimize like foot sliding on your your ai characters so they're not doing anything like particularly smart they are just sort of like blindly following the player here but it wouldn't really be a an ai series without starting starting here cool yeah uh so let's get into it then okay so in the scene here i've just got a um the basic sort of third person character uh setup which i've covered in my third person series if you want to know how to create this character then uh please check out that series um but we're not actually going to be using this character for the ai we're going to be building one completely from scratch mainly to make this series uh sit a little bit more stand alone but also um just to try out some different techniques and stuff um so what we're gonna do is create a ai character which will follow our player around the level because that's basically the starting point for all ai stuff so this wouldn't be a series without that feature um so what we need to do is just uh create a brand new character by dragging in an fbx file i'm just going to rename this to like ai agent and just drag it down here and i'm also going to unpack it just to lose the references to to this model um okay so this this ai agent doesn't come with much it's just got an animated component with no animator controller it's got a skeleton and a skin's mesh and that's basically it um so first thing to do is i just want to differentiate it looks wise from our main player character so i'm just going to duplicate the material by hitting ctrl d and assigning dragging that onto the player here and i can actually adjust the uh if i adjust the tiling offset of the character then i can get some different colors because it will just end up using different pairs of colors from from this texture here um so i think i'm going to go with this for the the look of the ai agent and so i'll just rename this material to like uh blue gray cool so that is the kind of basics for the the character the next thing to do is actually add a navmesh agent component and what this does is just uh lets the character navigate a nav mesh so speaking of nav mesh the next thing to do is generate a nav mesh for this entire level so to do that just go to the window ai navigation uh bring up this new panel switch to the bake tab and just hit bake and you should have something similar to this so the pink areas here are areas where the the agent uh cannot walk and the blue areas are places where it can walk and you can actually adjust the size by adjusting the agent radius here and that will um that will change the the size and how close it's it can get to walls uh but i will leave it at 0.5 for now um the other thing is the step height which controls how it can walk upstairs so at the moment it's able to navigate upstairs but if i decrease the step heights then it no longer is going to be able to climb up these stairs but i'll just put that back to 0.4 um because i wanted to be able to climb those stairs cool so that's just a really quick overview of the navmesh i'll probably go into it more detail later in the series but for now that'll do so if uh if we're actually into play mode now uh you'll notice that the ai basically doesn't do anything so this nav mesh agent it needs to be told to do something before it will work so we need to create a new script and [Music] i'm going to call it ai locomotion and what the script is going to do is basically talk to the navmesh agent component and uh tell it what to do so if we open up the script uh the first thing to do when using ai stuff is include the unity engine ai namespace and i'm going to create a new variable called nav mesh agent and it's on the same game object so we can just use uh get component nav mesh agents cool so very simple way to make the ai follow the player is um we can just hit the destination for the nav mesh agent uh to be the player's transform and that will the nerve mesh agent will automatically plan a path and follow that path towards its destination so to do that we just set the um destination of the agent equal to the player's transform so just need to create a new public transform here and i'll just call this player transform and we just assign the player transform.position to the agent's destination here so the last thing to do is actually just assign the player to the player transform and now if i hit play then we should have the player uh basically or that agent following the the player um which is yeah a good start it looks uh pretty creepy as uh normally does so the next thing to do is actually just add some animation to this uh to this ai agent um so to do that what we're gonna do is uh create a new animation controller uh first of all for the ai so if i just create a new animation controller here and i'll call this ai controller and just assign that to the ai agent and um oh that didn't actually assign it i need to assign it here like this so if we open up that then what we're going to do is use a new blend tree and we're going to blend between an idle walk and run animation just in one dimension and the speed so i'll just rename this parameter here to speed and what we're going to do is take the speed of the ai agent and feed it into this this animator and that will let the animation select an anime animation clip to play and blend between them based on the speed that comes from the ai agent so we just need to add a ton of like motion fields here so i'm going to use four different animations to blend between the first one is just going to be the characters idle animation which will be at zero speed of zero the next one is going to be a walk animation then a jog animation which is called straightforward in my case and a sprint forward animation and um now when we feed the this should really be enough yeah so if i just uh go back in and open up the ai locomotion script last thing we need to do is just get a reference to the animator here and uh just inside start again just call get component animator and now all we need to do is just set the um the value of that speed animation variable to the agent's velocity dot magnitude cool so now if i hit play we should see the ai player um basically trying to follow uh the player and it's it's doing something a little bit weird and what's happening here is these two things conflicting um on the one hand if i just pause this for a minute on the one hand we've got the ai agent uh it's it's um the nav mesh agent it is controlling the position of of this game object here as the the green capsule sort of simulates towards the player and then there is another thing that is trying to control the position and it's when we play an animation because we've got root motion set that's also moving the position of the game object so we just need to uncheck root motion and yeah now the uh the player or the ai agent kind of it looks okay but uh there's a fair bit of like foot sliding as you can see here and we can fix this up and um so if i just come out of play mode or just deselect root motion the way to fix this up is if you open up your blend tree these threshold values here they should actually correspond to um well it depends what value that you're feeding in so in this case it's a speed value which is coming from the ai agent so um this should correspond to the speed of the root motion of your your animation clip really and to do that uh you can actually use compute thresholds velocity z and you'll notice that all of these these values changed so this sprint animation it now has a threshold of seven so when a uh speed value of seven is fed in from the ai agent it will select the sprint animation and uh when a speed value of zero is fit in then it will select the idle animation and that basically uh should prevent a lot of the foot sliding for the most part um so yeah now the character looks a lot better there's still there's still going to be a little bit of foot sliding because we're not using root motion to control the ai agent um but yeah it it looks good but um you can already see that i'm able to walk faster than the ai character is so i could like sprint away from it even though that we're using the same animation clips and that's basically because this ai agent has got a max speed so this max speed value here should be set probably it is sort of up to you but i like to pick the maximum value here that you have for your fastest animation that just means that the ai agent is actually able to reach the top end of its animation range if that makes sense so if i just paste that value back in here and if i walk backwards now you can see that the um the ai is actually following me much more closely now um there is still a little bit of kind of weirdness going on um when you sort of walk around it and stuff but that is we can actually fix some of that um so let me just come out of play mode and i'll just paste that value in here to remember it um yeah so the next thing to kind of figure out is that that turning so what i like to do is actually um put the character up here and uh if i push play then and escape we can actually watch the um the ai agent sort of bump into walls and try to navigate this area and it's a little bit hard to kind of debug what this why the agent is sort of doing that so one useful thing that i find to do is actually create a debug component um so i'm gonna call this debug nav mesh agent and what we're gonna do is just draw a bunch of the uh the properties of this navimesh agent into the scene using uh gizmos so if i just open this up now so the first thing is just add the using unity engine.ai namespace and we need to get a reference to the nav mesh agent here and inside start it's just going to call get component nav mesh agent like this if i can type properly uh okay there eventually cool so instead of update just need to change this to on draw gizmos and this is like a special function for doing debug rendering which will appear in both the game view and the scene view which is quite handy so what i want to debug draw is these properties on the agent so there's things like velocity desired velocity and the other one i'm going to debug render is the path so just need to create like three different flags for each of these so um like velocity public all desired velocity public bull path and inside on draw gizmos we can just check if that flag is set then use gizmos dot color equals color dots green and gizmos dot draw line and what i want to do is just draw it from the agent's position to the agent's position plus the agent's velocity like that cool so that will draw a green line in the direction of the velocity of the agent so you can do a similar thing for the desired velocity um there's also a desired velocity property on the agent and finally um just for the path it's a little bit um more complicated um first i'm just going to get the agents path from the agent dot path and what we're going to do is draw a line between each corner of the path and draw a circle at each corner of the path so to draw a line between it we just need to get a previous corner and we'll just start that from the agent's position and now we just need to loop over each corner in the agent path dot corners and what we can do now is get the the current corner well i guess we have the current corner here um so we can just call like gizmos dot draw line from the previous corner to the current corner and gizmos dot draw sphere i think draw sphere at the current corner using the radius of 0.1 and finally just set the previous corner equals to the current corner um one more thing is just also set the color equal to i think i'm going to use black uh well it's not gizmos uh color dot black for the the path here cool so now if i hop back into unity um should have a ton of new properties here and if i go into play mode now um and i can basically toggle on this path property here and we can see the path of the agent and i can also toggle on these two things and this is basically what i wanted to show oh i forgot to change the color of the desired velocity i'll just set that to red so i can actually show you the difference between the two and come on unity go to play mode and yeah so if i just toggle the path and all three of them on now you can see that the um the red line is the desired velocity and the green line is the actual velocity and you can see that the um the green line takes some time to actually align with the desired velocity that basically means the um the character is not turning fast enough so i can show you this again if i just drag the player across here the ai will play on a new path and yeah you can see that that green line is it's taking time to align with the red one which is why the player is bumping into the walls and it's really obvious there and especially coming off this ramp so to fix that it's really easy all we do is just increase the angular speed of the character so 720 degrees per second seems pretty fast but the acceleration is also really important so i found bumping that up to something like 40 is as good for a character because characters can turn very quickly on the spot um if you have like a drone or a vehicle maybe you want these values a little bit lower um but yeah so if i just drag this across here now we can see that the um the red and the green lines they they align with each other much more um much more quickly and it just gives a lot more accuracy to the agent's simulation there is still one tiny annoying thing to fix and you may have uh noticed it um when the character runs through these small passageways here it uh it slows down like quite a lot and it kind of gets stuck on the corner of the uh of the terrain and basically there's tons of unity posts on this and they haven't fixed it it's to do with the obstacle avoidance so it's basically picking up the walls as obstacles which i find a little bit strange because these are already baked into the nav mesh i don't know why it's doing obstacle avoidance on the same objects that are baked into the nav mesh that doesn't seem right to me but the solution in my case is to turn obstacle avoidance off uh because i'm not going to be looking at it for a while i don't need it just yet it's pretty advanced sort of feature um depends on your game of course but yeah for me it's it's fine to have it off and yeah you can see that the um the agents just now quite smoothly can get through these these gaps here without slowing down there are some other properties and stuff that you can play around with like auto braking uh the stopping distance is actually quite good basically means that the um the character ai player will before it gets to the end of the path it will slow down and it will slow down with the stopping distance at one meter it will start slowing down which uh which basically can mean like if i say that to something like five for example just means that the um the agent will stop before it reaches the end of the path which uh can look quite good in certain circumstances uh so if we just wait for this to finish then i think yeah so now it's stopping like five meters off cool so the the final thing is um so currently we're just sitting the uh the position every single frame which is okay for uh sort of basic just testing purposes but it's quite an expensive operation and you can actually see what happens if i um if i walk sideways see how the player is like glitching like really hard might be hard to tell on youtube i'm not sure yet but it's basically like strobing and uh the reason for that is just because of this uh this operation here this is actually quite expensive it triggers a path plan um on the nav mesh uh every frame which we definitely don't want to do so we want to limit the amount of times that we do this so i'm going to create two new uh properties one called like max time and one called max distance and what we're going to do is basically create a timer and i'll actually initialize these as well one second and one meter and basically every update i'm just going to reduce the timer by delta time and then if that timer gets below zero only then do we uh trigger a path plan but we can actually do something a little bit more and also check uh how far away that we are from the um how far away our current destination is from the player because if the player is not moving much then there's no point sort of planning a new path so we can just check that by taking the player transform dot position minus the agent.destination destination and you could just use magnitude and then do like if distance is uh greater than the max distance or it's probably should be called min distance but whatever um then plan the path but a slightly more efficient way is actually use the squared magnitude and you can just do squared here and that just it just means that you need to then multiply this max distance and you basically avoid a square root operation here um and it's slightly more efficient so yeah the final thing to do is just reset the timer in here so we just want to reset that to max time now if i go back into play mode uh just check the values that come up on the ai locomotion max time one second and max distance one um so if i hop off here for a minute [Music] basically the player is only going to adjust its path every second and you can you can tune exactly how often you want this to be maybe you do want it to be like really frequent um but it will yeah it starts to look a little bit weird but again if you want if you want your your ai character to follow the player this closely you don't need to use path planning um because it's over such a small distance so when the player gets close to the character you could switch to a different mechanism rather than planning a path and i'm hoping to cover more of that stuff in upcoming videos but uh yeah for now just leave it at one second max distance one yeah i mean that's fine the the most important part is we're not calling that destination uh so frequently cool yeah so that's it for this video guys um if you have enjoyed it and you want to stay up to date with this ai series uh there'll be more videos on their way so hit the subscribe button hit the bell notification icon and you'll be informed as each new video comes out if you've enjoyed the video please hit like yeah it really helps the channel and also if you are interested in any of these assets or supporting this channel then my patreon is now live where you can find all of the assets uh all of these weapons the third person character and all of this ai series will be posted as soon as these videos come out cool so yeah thanks for watching and we'll see you in the next video kite
Info
Channel: TheKiwiCoder
Views: 15,008
Rating: undefined out of 5
Keywords: unity ai, unity ai locomotion, unity ai animation, animating ai, ai, animation, unity, ai nav mesh, unity nav mesh agent
Id: TpQbqRNCgM0
Channel Id: undefined
Length: 22min 11sec (1331 seconds)
Published: Fri Dec 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.