[Music] hey what is going on guys welcome to Flat tutorials and uh um what we're going to do today is a little bit different from what we have been doing earlier now what we're going to do is we are going to create a simple AI now this is a uh artificial intelligence basically uh this is what we are going to create is we are we going to have a lot of AI characters u in an enclosed area and they're not going to they're going to move around r rly but they're not going to collide with each other and that's that that's what we are going to do in this episode and now uh you need to keep in mind that there are thousands of ways to achieve this effect uh but I'm going to choose uh a way that I think is the best and is more mod modifiable but there are a lot of ways you can have like a bounding box and you uh once they are inside of that bound bounding box you push uh two AIS apart from each other and that works to when you are hand handling crowd but uh this is my way of doing things so you might like it you might not uh it is completely depends on your preference so let's get started and um the first thing that we are going to do is we are going to uh open Unity of course um and we are going to create a new project and we are going to give our project name as AI Crow right uh you can save it anywhere you want we are going to let it 3D and no packages uh we are going to import so I'm just going to click on create project and this will going to create a empty project for us and that's what we need all right so we are inside of unity right here um hold on something's wrong okay um we are here right okay now what we're going to do is we are going to turn down some settings because uh my computer can't handle recording and all this at the same time so I'm just going to uh set these Shadows to no shadows and I'm going to click on right click and create a plane this is where our AI is going to be um move is going to move around so I'm just going to go ahead and expand it it is very simple things that I'm doing over here so you don't really have to worry about all that so I'm just going to go ahead and create a folder to save my scripts I'm going to create another folder let's call it scenes I I'm going to create one last folder I'm going to call it artwork they're not going to be any artwork but I'm going to uh use this folder to save my materials uh that I'm going to use so I'm just going to go ahead and save the scene into the scenes and I'm going to call it test there you go now uh we're going to go to the artwork folder and right click create and we need a material and we're going to call it um plan and we're going to give it a a um color let's just say a darker one like this I'm going to go ahead and I said the unlet to um color okay because as I told you my computer can't handle all the shaders and things at the same time it's just going to go ahead and lag and I don't want that to happen Okay so we have our plane right here uh what we need is a cube 3d object cube right here okay now we are going to create boundaries this is just for our um level okay so I'm just going to go ahead and shrink it down like this expand it there you go um just do something like this I'm going to move it right over here I'm going to expand it you don't have to be perfect because we are just prototyping here so it don't really matter that much of course uh we don't want the player to fall off the edges so you you need to make sure that it is all covered and created an enclosed area it's going to work just fine okay now um I'm going to duplicate this okay and duplicate this going go over here and I'm going to rotate it 90 degrees on the x axis there you go I'm going to put it right over here right over here okay so now our our level is ready now all we need is our AI to be in there but first I'm going to go ahead and put the camera uh at the top so that we can see what is going on um we can do that by selecting the camera and we going to rotate it on uh xaxis by 90 degrees and we're going to go ahead and take it up right like this okay on the z-axis we're going to put it on zero so that we have right over here okay now you can see uh we can see our level okay now we are going to create our um AI all right so I'm just going to go ahead and create a another material I'm going to call it AI I'm going to set the Shader to unlet color of course and I'm going to give that color a red one okay now to create our AI I'm going to go ahead and right click um 3D objects Q this this is our going to be our Ai and I'm going to shrink it down I'm going to stretch it and looks pretty good okay I'm going to attach a color to it so that we can identify which is an AI and which is not and I guess that's pretty much it um now we can go ahead and start uh start programming the sayi because if you play the game right now nothing is going to happen this is going to stay there so right click create and we are going to create a c so let's call it AI controller and we are going to open this script up I'm using visual studio um for as my ID but you can use one up or any other ID that you prefer and it would work just fine okay now what we are going to do is first we are going to create sensors we need four sensors for this one but you can have as many sensor as you like is just going to work just fine um and yeah that's pretty much it okay so I'm going to go ahead and create a wide and this is going to be basically holding our um hold on guys I'll be back in a moment right right so we are going to create a um a void um on draw gismos this is going to help us draw um Graphics inside the editor but they are not going to be visible in the game so you can use this to prototype or create some special functions in your game which you want to visualize so I'm going to go ahead and go on drop giz Mouse and this is basically it all right so what we are going to do is basically we are going to prototype uh how our sensor are going to look like so I'm going to go ahead and go giz Mouse do drawr draw array and we're going to pass some things over here so if you see we we are going to pass array array has a position and from where it's going to be casted a direction and a length so that's what we're going to pass it now my position is going to be transform. position because I want to cast the aray uh from the player's position not anywhere else and also we are going to have a Direction so this is going to be the front um of the front uh sensor so I'm just going to go ahead and go give it the direction of transform that for forward and I'm going to give it a length which is going to be uh sensor uh length which we are haven't really told what that is so I'm just going to go ahead and go public float and we're going to give it a s and this is going to be our sent I'm going to give it a default value of five um which we will change it later on but it don't really matter so I'm just going to go ahead and go sensor length and because this is going to have variable um width and variable scale so basically I'm just going to go P transform. POS local scale um local scale. C no Lo scale local scale. C and that will be it so if I go back into the editor you can see what it's going on so if I just go back right over here okay we haven't really attached it so this is our queue I'm going to rename it to our Ai and I'm going to go ahead and add this AI controller to it and there you go you can see that there is um the uh Ray and if we resize it it's expand with it now uh there's one thing if you are if you want to visualize it in the editor you can go ahead and select this gizmos but toggle button and it will display the gizmos in your game scene but it won't be visible in your U main game so uh if you want it to be visible if if you want the sensors to be visible then create a graphics uh type of thing in order to make it visible in your game okay now we are going to create uh three more sensors which will basically going to be for each side and I'm going to have uh three more sensors so one two and three okay now uh I want to create a backward sensor so I'm just going to go ahead and give the direction to negative transform. forward this is a backward sensor okay um for uh the left and right sensor I'm going to go ahead and go transform. right and I'm going to go ahead and give this one as transform. right as well and I'm going to go ahead and give this one negative transform right so basically select um for this one I'm going to go ahead and add transform. loal scale. x and transform. loal scale. x now we have all the sensors that we need now I'm going to go ahead and strring down the length to a reasonable one so let's just say h 0.92 is going to work just fine for me um I'm going to shrink it down a little bit as well okay so this works um but it's still there's no movement there is nothing going on in here okay nothing's going on so what we are going to do is we are going to add a simple moment to it okay so we are going to go transform. position uh plus equals sorry CL equals transform. forward and we are going to give a a couple of things okay so we are going to give it a public sorry we're going to give it a public um float and we're going to give it a speed and I'm going to give it a value of 10 and we're going to go ahead and go transform the forward times it to a speed and we're going to times it to time time and that's pretty much it and if I go back uh if I play it my play is going to move forward okay that's good um but we can control it uh that we don't we can't control where it's going to move so what we're going to do is we're going to do this thing okay so we're going to go ahead and go public and we're going to give it a direction value sorry um float Direction value Direction value give it a default value of 1.0 and I'm going to go ahead and toggle it uh times a two speed uh Direction value now basically Direction value is going to control if it's going to move forward or backwards that's it it's not going to control in any other direction that's all it's going to do for um let me show you what it is okay so if I go back and if I go backward you see and the negative value is going to go backward so we can do it just like that or we can just modify the speed but I don't want to modify the speed um I'm going to go ahead when the sensor detects anything uh sensor detects a wall basically what what we're going to do is we're going to go ahead and set uh set the direction value toga1 or one accordingly so now what we're going to do is we are going to create Ray cast because these rays are just Graphics it is just showing but they are not detecting anything so what we are going to do is very simple uh we are going to create a rcast um so for for that for in order to record the data we need a rcast hit and I'm going to call it hit and that's it now we are going to create the sensors so first sensor is going to be for one so I'm just going to go ahead and go if um you know what I'm just going to go ahead and create all these sensors and it's going to work just fine so I'm just going to go ahead and go physics. rast and we're going to go ahead and provide it as transform. position and we are going to give this guy what direction we are moving so this is going to be the right sensor so I'm going to go ahead and go transform. right and we are going to save the data in hit so Al hit and we are going to give it a length as well so length is going to be this one right here there you go okay this is going to be our length and we are going to get this okay so when the array hits something is going to basically detect that uh is going to basically detect that we have hit something and it's going to run this if statement this is our right sensor so I'm just going to go ahead and say write sensor sensor okay now uh we are going to create the left sensor it is very easy I'm just going to go ahead and copy this I'm going to go down paste it and going call this one left sensor this is our left sensor and I'm going to give it a negative value on the direction so I'm just going to go ahead and go do this and now we are going to create our front sensor and our back sensor pretty easy all right so we are going to copy this whole thing and I'm going to go ahead and go paste it and I'm going to call this one front sensor oh there so front sensor and this is basically not a front sensor so I'm just going to go ahead and give the direction to transform. forward remove the negative value because then it will be backward uh and we are going to pass this value as the length so I'm going to copy this and I'm going to put it right over here there you go for back sensor I'm just going to copy all this I'm going to go ahead and paste it sorry uh I copied it one more time okay I'm going to go ahead and paste it and this is going to be our back sensor back sensor and and I'm going to give it a value on the negative side so this is a back sensor now all the sensors are ready but it's still they are not detecting anything as these if statements are going to run and they're not going to do anything because there's nothing in it so what we are going to do is we are going to do a simple thing basically we are going to say uh the H we can do a couple of things over here I'm just thinking how how I want to approach this okay so the first thing is that I need a collider as a private variable because this is going to hold our collider as my collider okay I'm going to go ahead and go my cider equals transform. git component um my collider my collider that's it oh sorry component sorry um okay um collider this is going to be our collider assigned onto the M Collider and I'm doing this so that we are seeing that our sensor is not hitting our own collider that's it this is this is for a small check that I'm going to do over here um so to uh to do that I'm just going to go if hit. collider do tag is not equals to obstacle obstacle uh and if it's not equals to obstacle basically we are going to return but we are going to do another check and we say hit do collider is equals to collider is equals to my collider okay so if this statement is true basically what what what we want to do is basically we are not going to run this statement we are just going to go back all right I'm just going to copy this if statement I'm going to paste it in all the sensors now if you want to detect that we have detected a wall or something we need to uh add this obstacle as tag in all those objects so I'm going to go back and I'm going to let it compile once it compiles I'm going to go ahead and go add tag and I'm going to go ahead and add the obstacle to it so I'm just going to copy this because I may mess up the spelling and it's going to create a lot of problems for us I'm going to save it and I'm going to go ahead and add the tag onto the AI as obstacle because this is an obstacle as well all the cubes are going to be set as obstacle and that's pretty much it all the cubes are just boundaries so that's pretty much it okay now we can say that we can detect we have collided with something so because our first sensor which is our front sensor is going to collide with something so I'm just going to go debug do log sorry log and we going to say wall and we have detected that wall so basically if it detects a wall is going to say wall that's pretty much it so it detects a wall there you go pretty simple pretty same um pretty easy to be honest um now it's going to work in All Those sensors is just going to work just fine so I'm just I'm not going to show you in each one of those I'm just test because all all of them are same so it's not going to make that big of a difference okay now we want another value I'm just going to go ahead and go um public float turn value turn value I'm going to give this value a value of zero this is also going to be uh moving between one and negative 1 uh or zero now I'm going to show you what this value is going to do what is going to do is this we are going to have a rotate function which is basically going to rotate this according to our sensor recognition so I'm just going to go ahead and go transform I'm going to create the rotation function for us um so transform. rotate simple and we are going to rotate on the direction of vector 3. up and we're going to times it to um you know what I need another variable and this is going to be our turn spr so I'm just going to go public float turn speed now turn speed is going to have a value of um let's just say 50 okay now turn speed have a value of 50 so I'm just going to go ahead and times it to uh turn speed times it turn value and we are going to have uh to multiply it time time that's pretty much it now what is what what it will going to do is this if I go back back and select the AI you're going to see that we have a couple of variables here which turn value and turn speed if I go back and set the turn value to increase it or decrease it you're going to see that our AI is turning in the direction of it now this is all going to be controlled through the script I'm not going to control it so I'm just going to go ahead and set them to a private variable so I'm just going to remove the public in front of them onto the uh turn value and Direction value because that's all we need um because we don't need as a public Val variable or visible in this Spector all right so we are here now we are going to implement this turn value first then we are going to toggle the uh Direction value but first what we are going to do if our sensor right sensor detects uh the uh that there is a wall then we are going to say turn value minus equals one we are going to turn left but if or let just say um play left sensor detects it so we are going to go turn Value Plus equals one all right I hope that you understand till now because if the right sensor detects that there's a wall then we are going to turn left and the left sensor detects there's a wall then we are going to turn right and if both these sensors detects the wall it's basically going to return a value turn value of zero and it's going to say don't turn anywhere because you can't both sides there as a wall okay now uh till now you understand what is going on here now if I go back and I'm going to uh know what it is better to demonstrate the whole thing first than doing over here so what we are going to do is we are going to program our front sensor so basically if the front sensor detects there's a wall we are going to go ahead and head backwards so we are basically going to do this if sorry if um Direction value is equals to one it means we are going forward then don't go forward and go backwards so Direction value minus equals um you know what equals TOA one this going to do all right so um and if there is a let's just say is this going to work fine okay so I'm just going to leave it like that uh Direction value equals netive 1 and same for the backward sensor I'm going to go back back and I'm going to go ahead and add this value uh to it um if Direction value equals netive 1 then set it to one okay so back sensor and front sensors are programmed now what we need to do uh is we need to check if uh if we haven't detected any of these um if we haven't detected any of these uh sensors hit anything then we want to just set the uh turn value to zero and in order to do that we're going to do a simple check and we are going to create a integer in flag I'm going to set this to let's just say um zero all right uh now we are going to go ahead and go flag incremented in every turn so we are going to go ahead and go over here we going to go ahead and go over here we going to go over here okay so this is basically to check that we have detected that there has been a uh sensor detection and if there to check that there haven't been as uh sensor detection we are going to set this check and see if the flag value is equals to false and if it is equals to false then we are going to say uh turn value equals zero equals zero okay now if I go back and run this you're going to see what's going to happen Okay so I'm just going to go ahead and click play and we're g to go backward okay so if I turn this guy let's just say return this guy over here you're GNA see ah you can't see okay uh let's just make a couple of these so that I can demonstrate to you how this works okay just going to go ahead do this okay I'm going to select all these I'm going to duplicate them move forward rotate them like say over here um I'm going to duplicate these as well and I'm going to rotate them right over here I'm going to click on play and let's see what's going to happen Okay so you can see uh that these all are detecting each other and you know what I'm going to go ahead and play around with these uh with their SES as well so I'm just going to go ahead and set this to let's just say um 1.2 or 1.3 1 five this would be fine okay so they are L pattern so the sensors are sticking out more if you see okay and they work pretty fine so uh what I see here is basically they're not colliding with each other because they know how to tackle situation when they are uh in front of any other type of AI or any type of wall any type of obstacle there is so this is a very simple uh intelligence but there are a lot of ways of doing this as well uh there are a lot of bugs that they sometimes keep on Road rotating in the same place uh but that won't last because they keep on moving uh there are thousands of things that that you can do you can have like invisible objects laid out all over the place and they basically choose based on timing they choose randomly those objects as targets and they are going to keep on avoiding each other until they reach that Target and they are going to switch and select any other Target that they is available and that will give you a great wandering type feel in these AIS a lot of things that you can do over here you can have pop uh path following things attached to them or uh you can have uh uh you can you can achieve these effects using those U nav mesh that is inside your 3D and it would work just fine but you know every this is perfect for like procedurally generated levels because in in these cases you don't uh in those cases you um you can't have pre-baked um meshes and because you can't have pre-big meses in those levels you can have real time Ai and it works pretty damn fine see they won't Collide they would just rotate and you can fix them very easily just uh add the turn value when you detect a um front sensor Collision or anything like that it would work just fine in a lot of ways but I hope sure that you understand what I did over here let me just explain to you one more time so what we are doing is we these are all all of our sensors and this if statement says if we are not detecting any obstacles or we are not uh we are detecting the obstacle as our own collider then basically return and don't do anything but if we are detecting s uh if we are detecting something on the right sensor then basically go ahead and turn left if we are detecting something on the left sensor go ahead and turn right and if we are detecting something on the front sensor go backwards and if we are something on the back sensor go forward that's all we are doing and yet we have this amazing effect uh not really that amazing but it looks pretty good so um I hope that you understand something from it and I'm going to go ahead and leave the project files of this project in the description down below they will be available in a day or two after the upload of this video uh keep an eye on my Facebook page because that's where I post the links first uh then update it on my uh YouTube video so that's pretty much it guys I hope you that you understand something from it and if did please leave a like and a comment saying that you did if you have any other idea for an AI go ahead and let me know and I'll go ahead and Implement that as well um that's pretty much it I hope to see you on the next one U and have a good one guys um I'm [Music] out