Learn AI in Unity3D Part 1 - Introduction To Steering Algorithms

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] all right uh today we're gonna do is we're gonna look into the world of artificial intelligence or gaming ai and we're going to investigate a few steering algorithms today so there are different aspects in ai and we're going to focus specifically on steering for this lesson and we're going to start off with a few techniques one is called seek and flee which we're going to start with now there's also pursuing evade and there's also wandering we're going to start with these five different techniques but we're going to start off now with seeking and i'm going and fleeing so what i have here is i have a pre-built project here uh if you want to follow along the idea is that have a character and also have a cube and and have charac have control set up for the character so you can move the character along the screen using just left right up and down or wasd keys on the keyboard but right now i have it set up so we're going to do some seeking all right so i'm going to hit run this is the end product here and i'm going to build it out together so just letting it load all right and you're seeing here the key cube is just running straight to the character now let's do some moving around here just so you can see i have here the scene view on on purpose because it gives you a better review of what's going on i also have cinema machines set up so that it follows the character but rather than give you a narrow zoom level i thought the scene would be a better view here so i'll be using this relying on the scene view a lot today and you can see that as i move my character around the cube is actually turning to face the character all right and so now uh let's try fleeing so i click on flea mode notice that it's actually running away from the cube now i'm going to get my character to come back i should probably we'll probably have to restart this and you'll see flea mode a bit better that way all right so let's hit the flea and now watching the cube run away oh flea in the check box there and it's running away so better keep up with it there it is now you'll notice that it's changing direction as i change direction right there and um i it's so so you can see there that the cube is actually running away from fembot and when i moved it fembot around a little bit it actually changed direction so this is seek and flee and one thing you'll notice is that seek and flee are actually the opposite of each other so the algorithm in interesting enough for seek and flee is just a matter of just multiplying one line of code by negative one and you have your fleet all right so we're gonna build out the seek we're gonna add the functionality at the end to do fleet so first thing we need to do in this project is add a new script called seek flee script so first i'll take away this end product here and you can see that we're going to work with that in a moment so so here's the project we're gonna work on we already we only have fembot here we have a uh we have a plane here that we're gonna that that's three thousand by three thousand and i'm gonna start off by actually adding a cube even before i add in the script i'm gonna add a cube so i'm gonna go to 3d object cube and we'll just place the cube maybe a little bit behind fembot there and the cube it actually if you look at the con if you look at the game view actually it blends in too much so so you can see that i want to change the color if you can change the color go for it if not it's no big deal so i'm going to just change the the color to be like a black so i actually have a black is one of the materials and there we go so it's a little more visible to us and i'm going to move it back a bit the important thing about this cube is that we want to maintain it to be a y of one okay we just wanted to maintain it that it's it's at this at this particular level and so that makes it easier for us to to work with here because uh you're gonna find that with forces and gravity and all that sort of stuff it's going to actually move below the plane it'll move high up in the air and then we'll it'll it'll look really odd in terms of how we're how we're chasing and evading or fleeing and seeking uh between the two objects here all right so we'll leave it as the name cube now like i said before we need to add a new script so i'm going to go back to project here i'm going to add a new script so i'm going to hit the plus sign there go c-sharp script and like i said before we're going to call it seek fully script all right so let's uh let's open up the script here we'll say seek these scripts so it should have automatically launched visual studio for you if not just double click on the script and get visual studio to open so that you can actually start editing this script all right now first thing we'll need is are some variables okay and so we'll focus purely on seeking right now so i'm going to say public game object and we'll call it character right and public game object target now the names will feel a bit misleading because of what i'm using uh to to do this like the character in question here is going to be the cube right you might be thinking intuitively character the actual fembot object a character there and then target you would have thought would be the cube but this is why we're setting it up because i added movement to a keyboard movement to fembot so uh that's why we're uh that's why we're using the reverse of these names so again character is the cube target is fembot so we'll be chasing or or seeking uh seeking fembot here now we're going to add a few variables here all right we're going to need a private vector3 for velocity we're also going to need some constants so i'm going to say public uh when we can make a private uh the you can make them public so that as you're tuning and testing so let's do let's make it public uh you can adjust these numbers in your editor as you're running and testing to see what a good set of numbers are for for your ai here so i'll say public float mass is equal to 15. and then we'll say public float max velocity we'll set it equal to three and we'll say public float max force equals 15. all right and so these are three constants mass velocity and force that you can fine-tune because you made them public you can fine-tune them in the editor as you're running so you can see what's the ideal set of numbers these i found to be ideal set of numbers as you saw from the demo was good enough but hey you can play around these numbers on your own time and and see what works for you now we're only going to work with start and update for today in this in this script so particularly we'll use start and we'll say velocity is equal to vector 3 dot 0 right just to initialize it to be a zero velocity now we'll work we'll do the rest of our stuff in update all right and what we're going to do is now in terms of what you saw in the demo we're using the bait we're using basic physics here position velocity time uh mass force to try and to try and calculate and figure out how we're going to get to our current position to where fembot is so that's what we're doing right so right now we need a desired velocity so we're going to say vector 3 desired velocity make sure i spell it right we'll set it equal to be the difference between the target and the character in terms of position so we'll say target dot transform dot position subtracted from character dot transform dot position all right so just the difference between the two all right now we'll say desired velocity is equal to desired velocity dot normalized times max velocity and this is where you can fine tune max velocity to see how it works so the idea is that we have here a position we want to convert it to velocity so what we're doing is we're we're multiplying it by a normalized value all right normalized numbers are between zero and one or like a a setting that that is easier to work with that's whole point of normalization and then we'll multiply it by max velocity so we have our scalar here to scale our speed uh in our in our editor and make to ensure that we can get the right number all right so we have here our desired velocity after being normalized now we need a steering vector so based on where we are and our desired velocity we're going to create a steering vector here by saying vector 3 cause steering is equal to uh desired velocity subtracting velocity so the actual speed at which we're we're proceeding and let's clamp it down based on on a on the applied force so we'll say steering is equal to vector 3 dot clamp magnitude of steering and length will be based on uh max velocity all right i mean max force we're clamping it between steering and max force all right now we're going to divide our clamped steering vector by mass so we'll say steering slash equals mass all right based on the mass that we're applying so you change the mass you fine-tune the mass uh in order to adjust your uh your your your seeking behavior so if you have like a big burly character let's say named big boy or something they're not gonna move as fast and and then they shouldn't be moving as fast and therefore mass should be much higher so you have a slower seeking ability and may not even catch up at all if your character is super fast but if you have like a tiny little road runner and and it's supposed to be super fast to seek you you out then mass should be much smaller so this is where you can fine-tune mass to be to be something that will work for you and accordingly force uh if you wanted to have maybe maybe you're in a different environment you're in space and maybe force should be should be higher because with in a vacuum environment you might be able to chase faster right no no drag no friction happening in space right so so things to consider as you're adjusting max force mass and and max velocity all right so now let's scale it by clamping it again we'll say velocity is equal to vector 3 dot clamp magnitude again all right we'll say velocity plus steering so we'll apply it to our steering comma max velocity all right so now we've now we actually have a a a steering vector that we want to go to all right now let's now we've been dealing with the velocity uh but we know that distance that we want to travel is just velocity times time so we're going to say now we'll apply this to our character by saying character dot transform dot position so up until now we've just been calculating where we want to go and now we're going to apply it to our characters we're going to change the character's position by saying plus equals velocity times time dot delta time all right so now that we've applied this to the character now we know that this is where we want to be all right so we're going to say character dot transform dot position equals new vector3 oops let's try it again is equal to new vector3 now this is to aid the character in being able to look at uh where we're going but more importantly actually we're doing this additional step uh because i want it to i want the character to be clamped at a height of one so we're doing this extra step here all right so we're gonna say new vector three character dot transform dot position for x the dot x comma one for y all right wanna keep it at one because there could be an instance as you're moving around it could get sort of pushed up in the air or pushed below the floor we don't want that to happen so i'm doing this extra step here it's not technically necessary right if you add in features like gravity and and and and and rigid bodies and so on you wouldn't have to do this but i didn't i didn't go that further that far into doing this with my little play area here so that's why we're clamping it to to one essentially so we're saying y is one and then we'll say character dot transform dot position dot set all right so this is going to hold it down at y of 1. so this extra line it's not technically necessary but it's it's needed for our particular application today and then finally let's get this thing to move forward so we'll say character dot transform dot forward is equal to velocity dot normalized so this is going to say make it so move in a manner towards the character all right so this is what we have here we have our uh this is all we need to do is seeking uh a seeking algorithm in order to get our cube to chase after fembot all right now the trick is to apply all of this to our game so what we're going to do is i'm just going to hit save right save that script we're going to switch over to our editor take a moment to refresh there all right and so we'll do now is we will add this new seek flea script to our game script object and click on game script so i already had a game script object already created all right just to be the manager of our of our scene so i'm going to hit add component all right and i'm going to add seek flea script all right so if you don't already have one you'll need to create an empty and add and add an empty gamescreen script object to it already this is this script i already have gamescenescript object this is this was meant for controlling the direction and movement of fembot all right so we have that seek flea script is here and now i have character target so character will be cube and target will be fembot idle all right and you can see you have mass velocity and max force that you can fine tune as you're playing with it so let's hit ctrl s to save my scene here all right and uh open up my game tab there so we can see there's fembot and just hit play make sure i'm clicking within game so that it's in focus and there you see the cube has moved towards fembot now let's try moving fembot around so you can see there the cube is chasing and you notice that the cube is actually turning to face fembot right so you can see that's what's happening it's chasing or it's seeking out fembot all right so that's that's seeking now a moment ago i said or earlier on i said that seeking and fleeing are opposite actions to each other so the beauty of of it is that we don't have to do we don't have to create a brand new script to get fleeing up and running we can actually take our existing script and modify it slightly so that we can do some fleeing action here so we're going to revisit our script here and we're going to add a variable all right a bool so we'll say public bool flea mode set it equal to false all right so the idea behind this is that this is false if we're if we're doing the seeking and it'll be true if we're doing fleeing all right now all we have to do is apply this boolean here and we'll we'll use the editor to specify whether this is true or false is to go down to this line of code here all right and we're going to put this inside an if statement so we're going to say if flea mode equals false then we'll just do this line as is right i'm going to copy and paste this line and make a duplicate of this line and put this inside an else so say else there and we're going to modify the else such that we're going to say negative 1 times velocity there all right so now if it's flea mode all you do is just negate the position and the character will run away that's all we have to do okay so i'm going to hit control s to save this let's switch back to our editor and what we'll do is uh we'll wait for it to refresh all right and you'll see that flea mode is just a check box and hit the check box there check it off let's hit run let's see this thing in action all right now you see the cube is running away and i'm going to actually move it around here now that you'll notice that the cube is changing direction i kind of lost fembot there and see if i can just double click on the cube there to bring it into focus and let's there maybe that zoomed out view will help you up there they're kind of i know they were seeing fembots trying to chase down the cube but you see the bottom line is that it is fleeing and i'll just hit pause let's bring q back into focus try again all right but the idea is that you can see that the cube the cube is running away and it changes direction it's actually changing direction based on where fembot is so you can see here that this is fleeing so we've just implemented seeking and fleeing and and now we're gonna attempt to do is uh pursue and evade so we're gonna do is i'm going to bring in the editor of the completed project just to show you pursue an evade but the idea behind pursue and evade is that they okay so they sound the same as seek and flee but they're different so let's start with pursue pursue versus seek the main difference between seek is that you're just ch you're just heading towards where the the target is pursue is trying to predict where you're going to go all right so pursue so if if i have my if i have my hand here and i want to head so seeking would just head right to there but if i'm moving say this way then my my character should be actually going to where i'm i'm trying to be that's the idea behind we're trying to meet somewhere there so so in pursuit you're trying to predict where you want to go all right so so the idea behind it is that we're uh we're trying to make the ai a bit smarter so pursue in a way is smarter than seek so i'm just going to hit my game script object i'm going to uncheck seek fleet to disable seek and flee i'm going to enable pursue evade and we're going to follow a very similar idea here so i'm just going to adjust the view here so you have something better to see an easier view there we go and let's give this a run all right now you see that the cube is heading right to fem box i haven't moved fembot so the cube is trying to be a bit smarter here it's trying to predict now i didn't go too heavy into into predicting uh i use the proper tuning variables to predict because it looks a little bit like seeking still but the idea is that it's trying to actually think ahead and the nice thing about pursue versus seek is that the code is very similar the algorithm is very similar there's just slight there's just a slight modification to getting to think a few steps ahead and actually that's really what you're doing is you're modifying the code to think a few steps ahead or a few seconds ahead all right so that's pursue now evade if i check off the check box you notice that it's running right so it's actually similar to to it but it's actually trying to think further ahead it's it's similar to flea so evade is similar to flea but it's actually trying to be a bit smarter by thinking further ahead it's actually trying to trying to do a fleet a seek sorry a flea move but a few seconds ahead in the future so it's actually trying to jump ahead in fleeing that's the idea behind pursuing evade is that they're very similar to seek and flee they're just trying to think a few steps ahead here so looking now what we need to do is we're going to add a new script so i'm going to actually work we're going to add an a separate script to do pursue evades we have a separate reference even though it's basically this is a very similar code almost identical with only the nuances you need to solve for pursuit but it's good to have a separate reference and you saw how we can separate uh our the movements that we want so let's add a new script it's gonna hit c sharp script and i'm gonna call this pursue evade script maybe you want to have separate scripts for pursue and for evade but that's totally up to you i'm going to i'm going to merge the two again like i did with seek and flee to to have the to have a very similar script that way and what we'll do is i'm not going to retype the entire block i'm actually going to copy and paste the code from seek and flee because it's a very similar script there so let's just double click on pursue evade let's get it open all right and let's start with the variables we're gonna use the same variables as before all right so i'm gonna take those variable definitions and put them into pursue evade all right and we're not using flea let's actually rename flea to evade evade mode all right and we know that velocity and the start will be velocity equals vector three dot zero all right so we're starting off with that now in terms of update maybe it would be better to have a good review of what we're doing again even though it's it's basically the same code we know that we want to have a vector 3 desired velocity equals target dot transform dot position subtracting character dot transform dot position all right but you can copy and paste the code i'll retype it and re-explain it now what we want to do though is this next line of code instead of copy pasting what we're going to do is we're going to rewrite it the idea is that uh our desired velocity should be normalized and times what our max by our max velocity that's that's what we have here in seek and flee right we're just going to say we're going to normalize it and times it by max velocity we're going to do instead though is uh pursue like i said earlier is trying to predict the next move so you want to think a few steps ahead so we're going to do is we're going to predict the next position rather as well as normalize it so first let's make our prediction we'll say float pred is equal to desired velocity dot magnitude divided by max velocity all right so doing a little prediction there now we want to work this prediction this number of steps ahead into our desired velocity so now we're going to continue on what we have before we'll say desired velocity equals desired velocity dot normalized times max velocity also times pred our prediction now we can continue on with our steering force all right so now you can continue the way we did with seek and flee we could say vector three steering is equal to desired velocity subtract velocity so just like before now we're going to clamp it to our max force so we'll say steering is equal to vector 3 dot clamp magnitude for steering and max force all right and then finally we'll we'll scale it by our mass so divide it by mass so i'll say steering slash equals mass and like i said earlier about my discussion on mass it still applies you have a big burly character you use a heavier mass you have a tiny little roadrunner use a lighter mass all right so you can fine tune it to be the as you need to for your game all right now let's uh let's clamp it down let's clamp down our velocity by saying velocity so let's apply the steering force to our velocity by saying velocity is equal to vector 3 dot clamp magnitude of velocity plus steering comma max velocity all right we'll apply a v evade after let's just focus on getting pursue up and running so we'll say character dot transform dot position actually we've already got the variable up and running let's just use the if statement get out of the way we'll say if evade mode equals false so evade is the opposite of pursue so we might as well just get it done so we'll say character.transform.position plus equals velocity times time dot delta time right else we'll just do the negative of that so we'll say negative one times velocity all right so we where we factored in evade so evade just like uh flea is the negation of pursue right so all you do is just negate pursue and you've got you you've got you've got evade all right it's that that's simple now let's apply this to our character but before we do that i still want to clamp the y to one so i'm going to do this extra line here so we'll say character dot transform dot position is equal to new vector 3 and we'll just we'll just repeat our characters x we'll say character dot transform dot position dot x we're gonna clamp y to one and z will just be character dot transform dot position dot set all right finally now we have our new position that we want to go for our character let's push our character forward so it doesn't just appear there it'll just push forward or move forward to the new position in a in a in a linear way so we'll say character dot transform dot forward equals velocity dot normalized all right and that's it for pursuing evade okay now the trick is to apply this to our uh to our to our game in the editor so hit ctrl s to save all right now let's switch back to editor all right and let it refresh there and we want to do is we want to click on our game script object here we added seek and fleet to it i'm going to add component again this time we'll say pursue and evade all right so now we got pursue evade script so character and target are same as before cube and fembot idle so i'm going to drag my cube all right and fembot idle and let's leave evade mode unchecked uh you can play with a mass max velocity and max force on your own to see what works what nicely for you i'm going to now you see i've lost focus i'm just going to double click on cube actually double click on fembot idle and there's both let's just adjust my view there to stand out a bit more there we go all right and now uh let's give this a run so let's open game view there we'll hit the plus sign actually make sure game script is highlighted because i may want to click on evade mode as well oh and don't forget to uncheck seek and flee make sure that when you add pursue evade uh uncheck seek and flee just so you don't have two things moving at the same time well i've got uh i've got uh fembot in the cube so let's uh let's play around with it so you can see that all right and so you can see that it's still following it's pursuing it now it's trying to predict where where it wants to go but unfortunately i'm not playing with it there we go so now so it's actually trying to keep up with fembot fembots moving much faster for it to do that but that's the idea is now it's doing a pursuit and you may want to play around with the tuning variables to see how just how fast you can get this thing to go all right so let's pursue let's get it to run away so now it's going to run away all right and it's trying to predict or there we go let's get it back into there get back into the into the view it's trying to evade as much as possible as quick as possible so there you go there is pursue and evade all right so the last ai feature that we want to do is called wandering now you notice that as the cube was moving to pursue or to evade or to seek or to flee it was doing it in a linear manner and we as people uh we don't walk perfectly straight we tend to sort of swagger side to side or you know like you know we don't or strut like side to side we don't we don't go in a perfectly linear motion and and most creatures don't do that either right even vehicles don't necessarily do that too like an airplane that that's that's flying across the screen doesn't just fly across the screen it might just you know sway right so sway a bit right because of external forces this is called wandering right in ai we call it wandering we want to apply a wandering motion to our character so now with with wandering uh what we're going to do is we're not going to use fembot for this third one we're going to actually add a sphere and we're gonna actually put the sphere get the spear to to spawn at different spots so that it actually is following and so that we can get the cube to follow and in a wandering manner randomly uh the the movement of our of our uh of our sphere so we're gonna do is we're gonna go to game object 3d object add a sphere and the sphere is good there i'm going to move it over a bit and let's change the color as well so i'm going to change the default if you can just change it to a different color all right so it's going to choose one of these other materials here and that's the idea behind it i just want it to be a different color all right so yeah so we have here our sphere and we're going to do is we're going to add a new script to do our wandering and our script for wandering will follow the seeking ability let's first get let's first get our quote-unquote seek up and running without the wandering and get it to follow or and get it to seek out the sphere in random positions so let's add a new script to our project i'm going to call it wanderscript and we'll open up in visual studio after it's done refreshing all right so like i said we're going to we're going to recreate the seek script but inside wander so i'm going to copy and paste from seek and flee this time again so we'll just take uh without the flea mode because we're not dealing with with fleeing at all just going to do wandering so i'm going to copy character and target just copy the whole thing and then delete flea mode all right let's get rid of that bool there we don't need it we'll need to add a few extra variables though which we'll do afterwards so in our variables here now from start we just needed that velocity is equal to vector 3 all right and in terms of update let's just take our exact same seek script all right paste it into wander but since we deleted the flea mode variable we have to get rid of this if block right and the else as well and let's just line this up proper okay all right so now we basically just have a seek script inside our wander all right we're going to follow c you can follow pursue if you wanted instead the idea is that we want to do some wandering all right and i don't want to call it target i want to call it wander target so i'm actually going to rename the variable to wander target that'll cause an error so let's fix that all right and yeah so basically we just have a seek here now let's modify this script so instead of us chasing fembot we're going to chase that sphere and we want that sphere to appear in random positions all along all right so we're going to create a method called next waypoint i'm going to put it right here just above update so we'll say private void next waypoint all right and what i want to do is i want to i want to change the position of wander target we'll do it every second or so or ever sorry every few seconds and we want to restrict it to be within a range within reason so i'm actually gonna if i switch back to uh the editor for a sec i want to keep my sphere to spawning within this radius here and i've already set it i've already done the numerical calculations figured out so i want to keep x between negative 15 and positive 15. i want to keep z between negative six and positive six y will always be one all right so i've already i've already looked at uh i've already looked at you know the the transform positioning of the sphere and i've already figured out that x and z will be within those ranges and that's good enough for where our sphere can be so we'll just focus that for our environment of course if your environment's different you want to try and figure out you really want to try and figure out you know where my mouse is along along this along this area and along this area right so you have like a little sphere and a square right so but that's based on what your environment is set up okay so that's why these numbers may differ for you okay so let's start by saying vector three call it one equals new vector three and we'll clamp the range we'll generate a random number first of all and restricted between negative 15 and positive 15. so we're going to use random say random capital r dot range and we'll say minimum value between will be negative 15 maximum value be positive 15. all right so that'll be for our x comma y is always one comma and z will be between negative six and positive six so we'll say random dot range for between negative six comma positive six all right so that's our that's our our this is supposed to be our new position for where our sphere is going to appear now if you just say wander target dot transform dot position is equal to one all right i could have just done that in one line of code but whatever all right so we have here this is we have here a method that will change the position of wander target we want this to run repeatedly all right nice thing about unity is it actually has a nice method to invoke a method repeatedly it's called invoke repeating so we're going to start and we're going to have this method next waypoint repeatedly running all automatically we could've done a co routine to do this ourselves but then we this actually saves a lot of work in doing that so we'll say invoke repeating the name of the method in quotes so next waypoint it's case sensitive so be careful with that comma the time so we'll start it after one second so 1.0 f and repeat rate so how often to do this we'll say 5f all right so every five seconds so we'll start this after one second all right and then we'll do it every five seconds we'll invoke this method called uh next waypoint all right now let's uh let's apply this without wandering okay so i'm gonna control s to save all right let's switch back to our editor and get everything up and running first of all without wandering all right so now let's add this new script wanderscript to our uh to our gamescript object here and what i'm going to do is i'm going to hit add component and we'll say wanderscript now we want to do is we want to uncheck seek and flee and i should have unchecked pursue evade last time or uncheck cknv last time but i did that inside the game so you didn't remember to do that so uncheck seek and flee uncheck pursue evade and just have wander script and we're going to do character will be uh will be our cube wander target is our sphere all right now let's run this and see let's see it in action on its own we don't really need the game but we'll see everything in action here under scene all right so now you can see that the sphere every five seconds will move to a new position all right and you can fine tune that looks like five seconds might be too uh too long so maybe we'll change that to two to three but let's see here so it gets there and there you go now it's changing position all right so it's seeking out the sphere as it goes what we need to do is we need to now apply wandering to this all right so it won't just walk in a linear manner it'll be kind of like strutting now the thing about walking towards or move or seeking out the the object is that uh we're gonna the way it's designed wandering is designed is that uh you try and you try and move like this right and this is kind of a circular fashion all right and when you're doing it this way while moving like you see me moving my hand here uh you end up having you end up trying to trying to adjust the facing of the of the cube in a circular manner in a way that you're applying circular uh circumference radius distance and so on so it actually is changing the angle of facing all right that's actually what we're doing here when we're walking right and so and so that's we're going to apply right now to our cube so we're going to modify our our wandering script to add in a a bit of wandering here so let's jump back to our wander script here and let's add a few extra constants here that we can work with they can fine tune on your own after you've done this we'll say public float circle distance we'll set it equal to 6 public float circle radius equals three and then public float so like i said we're dealing with a a circular motion here so we'll say circle distance circle radius we'll need something called wander angle set it equal to 15 so an angle of 15 degrees and we'll need a finally a public float angle change so how much do we want to change the angle by in each iteration let's say like you know five degrees just to start off with maybe a smaller number is better it'll be up to you to fine tune and see depending on your object and how you want to go all right let's create a method that we're going to use inside our update afterwards to adjust the wander angle we'll call it wander so we'll say private uh vector three and return back of a new vector three wander passing in a vector three called velocity all right now like i said we're dealing with a circular motion here so we'll start by creating a vector 3 called circle center uh equals velocity dot normalized all right now we'll say now we'll scale this this circle center by distance okay so we'll say circle center star equals uh circle distance right the circular distance scalar we we set up above now we'll need to figure out the displacement force is how much you want to displace our uh and we want to apply a force in displacing our where we're facing so you can see in my hand here where we're going to calculate the force for it so we'll say vector three just scroll up a bit here vector three displacement is equal to a new vector three we're going to clamp it to to one so we'll say between zero and one uh so we'll say new vector three x will be math f dot clamp zero one to clamp between zero and and the number so one all right we'll keep y as one always all right and z will do the same thing math f dot clamp01 for 1.0 f all right now let's apply this displacement force to our circle radius so sorry apply our circle radius displacement force by saying displacement star equals circle radius all right now let's up now we want to do is we want to change the angle at which we're doing this so we're going to before we do that we're going to create a new method to play with the angle at which we're facing so we'll come back to this method we'll add some more to it we'll say private vector3 set angle is equal to vector 3 vector comma float wander angle all right and inside of this this is where you actually can fine tune your wandering whether you want to go this way we're actually going to go we actually go like like this way but also like this way all right so kind of like a nodding motion as well so we're gonna walk we're gonna walk and not walk and nod like like it won't just be it won't just be uh this way it'll be like this way up and down right so so kind of like a real more realistic human movement where we're actually just kind of like up and down up and down but also like side to side that's really how we move as people right so we want to apply that to our cube here so that's why we're gonna we're gonna set our angle in two directions in x and z y will always stay the same though so we'll say vector three v equals vector past the one we've passed in uh now we'll say how much we want to scale by or adjust the angle by so we'll say float len equals vector dot magnitude all right now let's apply it to both our x and z of portions of components of our vector here so we'll say v dot x is equal to math f dot cos of wander angle all right scale it by len all right v dot z is equal to math f dot sine of a wander angle and scale up by a len as well all right finally return v okay this will be our method we're going to use to adjust the angle of of displacement of our character okay now let's apply this back to our wander method so now we're going to say displacement is equal to set angle of displacement comma wander angle all right and so now let's adjust the angle of our and and force so we'll say wander angle so wander angle will constantly be changing will be plus equals random dot range between 0.01 f and 1.0f all right times up by our change in angle so angle change right subtract it from so we're going to change our angle change as well so we'll say angle change times 0.5 all right so what we're doing here is uh we're going to also change the wander angle and we're also going to change it by our angle change so wander angle will be even though we hard coded to 15 to start off with it's going to actually be changing based on so it'll be like a plus or minus five all right so it's a maximum plus or minus five so between uh 10 degrees and 20 degrees is what we're doing here all right because we set angle we set a wander angle initially to 15. all right so all's be changing now let's apply this change to our force so we'll say and we'll return it back so actually we'll say finally vector three uh wander force is equal to circle center plus displacement finally return wander force okay so we've done all this calculation here we've done all this calculation here uh to adjust angles to adjust movements you know to sway up and down and side to side right so we've done all this adjustment here now let's actually apply this to our character so the question is how do we apply this to our character well it's actually not that big a deal here uh what we're gonna do is uh we're gonna scroll up to our update and let's look at steering okay so now we're saying vector 3 steering is equal to desired velocity minus velocity let's modify this we're going to actually say vector 3 wand is equal to desired velocity minus velocity and then we're going to say vector 3 steering is equal to wander of wand and that's it we're done so now we've basically just circumvented our steering by applying wandering to it right so you can see that it's just that simple like be able to apply wandering uh you just update up update accordingly the trick though is actually creating or formulating your wander algorithm so that you're doing the wandering that you want and in particular wander itself will stay the same it's the set angle method that we had at the bottom that may change depending on your application so this method may change depending on you are you applying to x to z to y are you doing a cosig sign right so so this this is really the bread and butter fine tuning where the fine tuning will happen all right so after we've adjusted these two lines here all right now let's apply this in our in our game in the editor okay so we've already connected everything up all right so if i switch over to uh my my editor here well first let's actually save our script we haven't saved the script yet so back here do control s to save all right now switch to editor let it think all right now i have to do is just hit run all right and so notice the cube is kind of jittering kind of shaking it's like left to right uh it's it's not going perfectly linear it's it's kind of like it's chugging chucking along right so that's what we're trying to do with wandering here right it's it's uh it's seeking out the sphere but in a in a not in a perfect manner there all right that's what we want we'll have add a little more realism to our game right so there you have it we've we've just investigated five ai algorithms for steering in the steering category so the idea was to steer to our destination right we looked at seeking then we looked at fleeing fleeing being the opposite of steering is of seeking so it's really easy to do we looked at pursue pursue is very similar to seek except the difference is that you're trying to predict where you want to go so like in hockey you don't you don't pass the puck to where the person is standing you pass the puck to where the guy's gonna be so that's what we do with with pursuing we try to predict we want to go and evading is the opposite of pursuing and finally wandering so we added a little bit of jittery movement a little bit of you know strutting to our to our character all right so that's it for for steering methods in in artificial intelligence in unity uh don't forget to like subscribe and i'll see you in the next tutorial
Info
Channel: Jawaad Sheikh
Views: 191
Rating: undefined out of 5
Keywords: unity3d, learn to code, app development, game development, artificial intelligence, ai, steering, seek, flee, pursue, evade, wandering
Id: mUgMtVw1FHw
Channel Id: undefined
Length: 59min 30sec (3570 seconds)
Published: Tue Nov 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.