3D Survival Game Tutorial | Unity | Part 3: Selecting Items with Raycast & Creating Simple AI

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there and welcome back in the previous episode we created this terrain and i had some time so i made these mountains look a little bit more realistic with these spikes here so i used all the different brushes we have to give them this special look and i hope you also played a bit with your terrain in this episode we're going to do two things so the first thing we're going to do is actually be able to interact with objects for now we only have these trees as objects but later on we're going to have more and more objects and we want to interact with them right so usually in first person games there is some kind of little dot in the middle of the screen that helps us keep track where is the middle right because right now if we play we don't have anything to indicate what is the middle so if for example we want to point on this tree or select this tree it's not that easy because we don't know where this middle is and it's more important when we actually want to point on on something like some kind of enemy or we want to any thing that requires us to actually interact with something using our camera right so we're going to create this option and we're going to be able to interact with these objects so right now we're only going to be able to get their name but later on we're going to be able to actually do things and with these trees for example we're going to be able to chop them down and with other things we're going to be able to pick up so this is just the beginning but we're going to do that in this episode the next thing we're going to do is actually create some very basic a.i so we're going to have some creatures that will move around in our scene just wander around to give some life into the game right now we're going to just make them walk around we're not going to fight them or something but we just want to create this basic ai and we're going to be able to actually interact with them the same way like we interact with the trees we're going to be able to point at them and see their name for now so first of all let's create this little pointer on the screen to do that we simply click here on the screen and we create ui and canvas and inside the canvas we are going to create ui image and if we go i created a few folders here so i have my scripts folder i have my prefabs and i have art all the rest are the packages that i downloaded and i also have scenes so if i go into the art i also created another folder for the ui art i have this sphere that i created so that's a very simple image you can do in any photo editor it's just a small white circle so i'm going to use this sphere to create this small little dot so all i need to do is click on this image and drag this sphere inside the source image of course don't forget that when you bring this image inside you need to first import it so you need to make sure that it's a sprite and then you need to apply some kind of compression on it and then you can actually use it so now that we have it here inside the source image we can zoom back and see that it actually appears on the ui but we don't want it to be that big so we're going to make it smaller we're going to change the scale here to something like 0.05 okay so now it's a very small dot and if we play you can actually see this little dot over here okay so you can do something else you can maybe put a small little reticle or a different color or whatever you want but that's the way i'm going to do that and of course when we place it on our canvas make sure that the positioning is 0 0 0 because we want it to be in the exact center also anchor it to the center okay so then when we actually point on something it will be in the center of the screen okay now we have this thing but we can't do anything with it right now so let's move on and before we actually go into the script we're going to click on the canvas and create another thing we're going to create a ui text and we're going to call it interaction info ui you can call it whatever you want that's the way i'm calling it so we're going to use this text to actually display the name of the item that we hover over with our mouse right with our little pointer so it's very common in survival games and when you walk around and you see a stone then you just hover over the stone and it tells you that it's a stone and whatever you need to do so right now it's in the middle we're going to align it to the center and we're going to move it a little bit below this little dot you can place it wherever you want on the screen i'm just going to place it here for now we're going to change the style to bold and the color to white and maybe the size a little bit to 18. okay later on we can also change the font but we need to import fonts so we're going to do that later and now if we play we can see that we have this little text on the screen but of course we don't need to see this text all of the time we only need to see it when we actually hover over something so we're going to just disable it so we don't see it okay and then when we actually hover over something we can enable it but in the beginning it will be disabled okay so in order to deal with all of this selection and interaction of items we are going to create a system a manager to do that so i already have the script and i'm going to provide it again in the paste bin i don't know if i'm going to do that all of the time some of the time i'm just going to show you and you're going to copy it but for now i'm going to share it with you in the link in the description so first of all i go here into the script folder and we have it here the selection manager okay so this selection manager is the script that we're going to use to deal with all of this selection but in order to use it we are going to create an empty object and we're going to call it selection manager as well and we're simply going to drag this script on this empty object okay so again like i showed you in the previous episode you go to pastebin you copy everything from within the class and you just paste it inside and make sure that the class name and the class file have the same name okay and the script file have the same name with the exact capital and lowercase letters so now that we created this empty object and we dragged our selection manager script on it we can see that we need a reference to an object which is interaction and for ui and it's exactly this one so we're going to do that now we're going to drag it inside because we need a reference to it in order to enable it and disable it when we hover over something or not right so we're going to do that from within the selection manager and now let's go into the selection manager let's open up the script let's just close all of these so in the selection manager we have this game object the interaction in for ui so it's public because we want to actually reference it in the inspector and then we also have here a reference to the interaction text so that's a reference for the actual component of the text because if we go here you can see that this entire thing is a game object but we want to reference this text component so in the start we take the interaction in for ui and we get the component text and we place it inside the interaction text okay so now we have the game object itself and we have the text component so we can change the text whenever we want then in the update method we're simply creating array okay because we want to cast array from the middle of the screen so that's exactly what we're doing here camera dot main dot screen point to ray and input point mouse position because we want to get the position of the mouse which is locked in the middle because you remember we locked it in the middle of the screen in the previous uh when we created the mouse movement script right here cursor lock state and we locked it in the middle of the screen so now it will send array from the middle of the screen and it will search if it actually hits something so in order to check if it hits something we're going to use this physics dot raycast and here it says that we need to provide array and we also need to provide array cast hit okay so we create a raycast head here we call it hit and then we check if physics dot raycast and we provide the array that we created and then we also provide the rake has hit so we want to put the information inside this hit so whatever this ray is pointing at it will put it inside this head and then we simply insert this information the information that is inside this hit we place it in this variable so the hit dot transform we place it inside here so now we have a selection transform and the selection transform is the actual transform of the object that we hit okay so if we go here and we hit this tree for example then the selection transform will simply become the transform of this tree okay and the same thing with everything else but now we have something that we need to check because if we point on the ground it will get the transform of the ground if we hit the mountain or the sky or something else it will also get the transform of these things but we are not interested in that we're only interested in specific items items or objects that we want to interact with them right so we don't care we don't care if we actually hit this ground because there's nothing to do with this ground we do want to get the information of this tree we do want to get information of maybe stones or sticks or things that we can pick up so that's why we need a way to check if the thing that we hit is actually something that we want to interact with so a way we can do it is we're going to create another component right another script for interactable objects and we're going to place that on every prefab that we want to interact with okay so then we're simply checking if this game object that we hit we're searching for a component and if we actually find an interactable object component okay so this is a class we created so we can see it here so i also provide it in the link below we have the script of an interactable object so this interactable logic simply has a public string item name and a method that returns an item name which is a string right so it returns a string and it gets the item name of this object so later on we're going to expand that and we're going to put other things inside but for now we're simply using this script to get the item name of this object and to also know that this object is interactable right because if it actually has this script then it will be true right because if selection transform get component and it actually finds this component then we can do something else so if we don't find this component on the object then we're not going to do something right so now that we have this interactable object script we're going to place it on all of our trees but we don't have to do it one by one we can simply go to the prefabs so in the prefabs i have the prefab of art tree and i'm simply going to select it and drag the interactable object on this tree okay so now it has this script and i'm going to give it a name let's just call it free okay so now that's the name of the object okay and now because that's a part of the prefab all of these will also have this script on them okay so we don't have to actually put it on every one of them because all of these are just copies of this prefab so now all of these have this interactable object so now what will happen when we actually point on one of these trees it will find this component on them so it will know that it's actually an interactable object so it will do this and what is this it will take the interaction text the one here that we the component that we referenced here and we're going to change its text to selection transform dot get component interactable object dot get item name so we're going inside this class and we're just calling this method we're getting the item name and the item name we set it here in the inspector right we set it here so each prefab will have its own name so it will get this name it will return the item name and it will send it and place it here inside the text of the interaction text and then it will take this entire ui the interaction in for ui and we'll just set it active so because it will become active we're going to be able to see it on the screen with the new text okay but if the selection will suddenly go to let's say the ground then we're going to turn off this ui okay so we won't be able to see it anymore okay so let's see how it actually works so we have these threes we have this script on them so let's play and now if we go here and we point on this tree we actually see three and then we point here it disappears point back three and we can do that with every tree good so now we can actually get the item name of all of our items right there's nothing much to do with the the name of these items it's good to know what there are sometimes we want to walk around and just get names of different objects but later on we're going to be able to use this script to actually interact and do things and pick up items and check all kinds of options we can perform for example i wrote down three but we can also do something like that we can type in need needs acts something like that or use axe okay so now when we go to a tree but it actually didn't save it because i only selected one again we need to go to the prefab birch 3 and here we want to change it x needed something like that so when we approach a tree then it says x needed so we know that there's actually something to do with a tree we need an axe or we need something to chop it down and then when we're going to have an axe we can simply approach this tree and then it will say click on the left mouse button to chop down or just press on something to interact and then we're going to start to chop the tree down so this thing is useful in different ways okay and here we're going to change it back let's just remove this and let's also remove it from the prefab okay so now we can see that we have the selection manager we linked it with the interaction and for ui so it becomes active each time we hover over something and it gets disabled when we hover on the ground okay so that's good and now we covered this part of selecting things and interacting with things right of course later on we're going to be able to pick up objects and we're going to be able to use objects and interact with objects but we just needed this very basic script to start with something the next thing we want to do as i told you is to add some life into our environment right it's not that important but it's just something i wanted to show you how to do right now and later on we're going to be able to actually interact with these creatures and also do all kinds of actions maybe hunt maybe gather so it's good that we have them right now so in order to actually make it look like something and not just use primitive objects you can use whatever you want you can use any kind of creature or animal that fits into your environment i'm going to use this free asset from the asset store the white rabbit okay so you simply download it and import it into your game it's for free and again i'm going to leave this link in the description here i downloaded this rabbit package and we have different animations and we have the model itself so all i did was create a prefab i took the this prefab of the rabbit i dragged it inside my scene and then i dragged it inside my prefabs folder so it became a prefab and now i only need to change the prefab and it will change all the other clones that i have in the scene okay so i have this prefab here and i created two versions okay i created a small one and a bigger one so i simply changed the scale of the big one okay and we can see that we have an animator on it so when we download this package we already have an animator but we need to attach the controller on it so if it's not attached you simply go here into the rabbits and you go into animations now into demo animator and you drag this demo animator onto your prefab okay you drag it over here to this slot then the second thing we need to add is this box collider i don't know why i have two of them i only need one of them okay because we don't want the rabbit to fall through the terrain so we want a box collider okay and we also want a rigid body because we want to use gravity we don't want him to just stay in one position on the in the air or something because if he moves from a certain elevation we don't want him to just keep on walking we want him to actually fall down okay so that's why we use this rigid body and besides this rigid body and besides this box collider i have this ai movement script okay but before i show you the script i just want to talk about the animator so you click on the rabbit let's just delete this one i'm going to use my prefab so i have a small rabbit here and you can do that with every creature so when you click on your prefab you can go to the animation panel the animator panel and you can see that we also have a depth animation we have a round animation we have an idol animation and we have a death animation right now we're not going to deal with the def animation i simply put the entry animation as the idle one so if it's not attached simply go here and then search for the idle one okay and then there's the run one so again you can go here and find the run animation and i simply created these transitions from idle to run so it uses this parameter is running okay so i created a new bull and i called it is running so when is running is true it starts running and when is running is false it becomes idle so that's a very simple animation okay so it's a very good package because these animations come with this rabbit okay so there's the idle animation and there's the run animation and i simply use this bull to control them if it's true it runs if it's false it becomes idle again so i just want to show you how to set it up if it doesn't work for you so now that we have our little rabbit and we have the animator let's talk about this ai movement so again you're going to find that in the description box let's go to our scripts and let's open the ai movement so this ai movement it's let's just make it bigger i think it's better for you to see it like this so this ai movement script is a very general script so it's not meant for this bunny right there's nothing here inside that tells us that it's just for this bunny we can use this ai script for every creature that just wanders around okay of course later on we're going to add more things and more functionalities but still it's not something meant for just one kind of creature we can use that and place it on every creature that we have simply drag it over here and then we control the different speed and these things we can always change them okay so now that we have our ai movement let's see what's going on inside we have an animator okay so it's simply a reference to the animator we have on our rabbit so in the start we just get a reference to the component the animator okay then we have a public float move speed so we can change it from the inspector although i placed it on 0.2 as default but we can always change that then we have a vector 3 which is the stop position so we use that in order to stop our body because we want our bunny our rabbit to walk around for some time and then stop for some time and then walk again so we need to determine what is the position that he stops in so that's why we need this vector three then we have a float that is the walk time then we have a public float which is the walk counter we have the wait time we have the weight counter then we have another end that is the walk direction then we have a public bull that is is walking we check if the ai is actually walking or not and then we have this start method so again as i told you we have this reference to the animator then we have these two variables the walk time and the wait time so the walk time is basically the time the ai will walk okay and then wait time is the time that the ai will just wait so if we're going to set the same values for all of our clones right all of these creatures then what we're going to see is that they all walk at the same time and they all stop at the same time and then they begin to walk together so it will be very weird and unnatural that's why inside this script i created this option to make it random okay so at the start each clone right each bunny each rabbit will get a different walk time and a different wait time so it will be random between every one of them you don't need to do that you need don't need to go and say okay this rabbit i want him to wait a certain amount and i want him to walk and you don't need to do that because if you're going to have a hundred different rabbits you don't need to do that one by one because we have these two lines of code here it will do that it will just create a random number between three and six and and here it will do it between five and seven again these have no meaning it's just numbers that i gave so i think that the walk time should be somewhere around three seconds and six seconds and the wait time should be between five and seven so again it really doesn't mean anything you can use whatever numbers you want if you want them to walk for a long time then you can just set higher numbers if you want them to wait for a different time you can just set different numbers but it will just give different values to different clones of these prefabs so each prefab so each bunny will just have different times and then we simply set the weight counter and the walk counter to the wait time and walk time that we get here because we want to reset the counter okay so we use these counters to actually check the time that have passed so we check how much time did he actually walk and we check how much time did he actually wait okay so at the beginning we actually set it to be equals to the initial time okay and then at the beginning after we do all of this we actually go to this method choose direction so it goes here and it chooses a direction to go to so here again we use this random range so it will give one of these numbers it will give either zero or one or two or three it won't give four right because here it says return a random integer number between minimum and maximum exclusive so it won't choose four it will choose zero or one or two or three okay so we have four options because there are four directions the bunny can move okay i just wanted to make very simple i want him to go either forward either this way either this way or either this way okay again he will walk in a certain direction he will stop and wait for a certain time and then he will choose another direction and then he will go to that direction and then he will stop and wait and choose another direction and go to another direction so he will always be in motion and he won't just go to one direction right so it will be random so that's why we use this random it will just choose a direction each time he starts walking so now when it chooses a direction we're setting is walking to true and we're resetting the walk counter again because here we just reset it at the start but then we need to reset it also each time we start walking and because is walking is true right now in the update method it's true so it will actually run this code okay so if is walking is true so if it actually shows the direction so it's ready to move because it shows the direction so is walking become true and then it will round this code so we're setting this is running pool to be true so we're using the animator component to set this bool to is running right if we go to the animator we can see that we need to use this is running we need to set it to true because when it's true it means that it says run but i actually mean just walk right so if it's true then the body will run and if it's false he will become idle so if you use it on other creatures just make sure you're using this name right is running even if it's not running even if it's just walking or i don't know crawling or something use is running or just change it here in the script otherwise it won't work you need this to be the same name so even if we're going to have different creatures we can still use the same name for their running okay so we don't have to change the script to fit it to different creatures we can still use the same script for many creatures so we're setting the animation to running so we will start running and then we're simply decreasing the walk counter by time delta time so what happens here is because it happens in the update and the update is basically called every frame so it actually will reduce one second every time okay so it will take the time delta time it will reduce one second at a time so if the walk counter for example here was set to five seconds then here it will go and be five four three two one and when this walk counter goes down it will run this code it will actually move the rabbit and then here it checks if the walk counter actually reached zero so it will go down it will be five four three two one and then it reached zero so it will stop the movement okay but we're going to reach that so while it's still counting right while the walk counter still goes down it will do this it will actually move the ai so here when we chose a walk direction right we chose an integer and we placed it inside walk direction we're simply creating a switch to check what will happen in case one of these numbers are selected right because we have four options as i said we want him to either go over here over here over here or over here so we have four options so we use this switch statement and we are checking what is the value of walk direction so if it's zero it will do something if it's one if it's two or if it's three so all it does here in the beginning it just changes the rotation right because we want him to move forward we don't want him to move this way like he's walking backwards and we don't want him to move this way on the side we want him to face the direction he's walking okay want him to walk with his head forward so that's why we need to first rotate him to the direction he wants to go so these lines are all the same when we actually move him to the direction but these lines are different because here in case 0 we're taking the transform the local rotation and we're not rotating it because you can see 0 0 0 it means that we're not doing any rotation and in this case he will actually just move forward because he is not rotating he will just keep moving forward again but here in this case in case 2 we're going to change the y rotation by 90 degrees so what will happen it will simply change the y rotation by 90 degrees and you can watch here we are going to rotate him by 90 degrees so he will rotate exactly to this position right and then he will walk forward and the same happens with the other cases minus 90 or 180 so he will either go this way he will either move to 180 right which is the opposite direction or he will move to minus 90. well it really is relative on where he starts but if he goes this way then it's minus 90 okay so it will choose one of these directions and it will just face the direction right so the difference is the rotation on the y-axis right we're rotating on the y-axis we can see it here so let's just move him the way he was okay so that's the beginning and then when it actually changes the rotation so either it's not rotating or it's rotating by 90 degrees so to that direction or to the left or backwards after it changed the rotation it will simply take the position of the transform which is our rabbit and it will add movement on the forward axis times move speed times delta time so the move speed it's something we set here and it's something we can change in the inspector but it will simply multiply the forward movement and we remember it from the previous scripts that forward is simply the blue axis right so the blue axis is this front axis right so it will move him forward on the blue axis and if he rotated here he will still move on the blue axis and if it rotated here he will still move on the blue axis so we're always moving him on the blue axis that that's why we always use this transform forward and then we simply multiply it by speed and by time tilde time so that it will be consistent with the different frames okay so we do all of this and then he will simply move forward okay he will not move a certain distance he will just keep moving forward but eventually he will stop because this timer the walk counter will eventually reach zero and then it will stop him so then we're taking the stop position we're taking the current position of the rabbit and we're placing it inside the stop position vector so we need to check where he is at now then we're also setting the is walking to false and we're just saying that transform position will become the stop position so now you're not moving anymore anywhere you're just going to stay at this position that you were when the timer hit zero because here it hits zero and he was at this point in space so he will become stationary on this point right so if the timer went and he walked walked walk and then the timer reached zero when it was here so it will save this position it will save the coordinates and place them inside stop position and then it will just change the transform to stop position so he will no longer keep walking he will just stay here and wait and he will wait here and we are also changing the animator back to false so he will stop moving so the animation will be the idle animation right and then we're also resetting the weight counter and then because his walking is false it will no longer run this code because it's false it will run this code and here it will simply do the same thing but now with the weight counter so now we're going to wait a certain amount of seconds and he will just stand idle until this timer runs out so when this counter will reach zero then he will again choose a direction and when he chose the direction he will walk again and because he is walking he will run this code again and it will do it all over again okay so that's the script it took me a few hours to actually write everything down i tried many things i also tried to do it with the nav agent right with the navigation so he will be able to walk and avoid obstacles but we don't really need him to follow us or chase us we just need him to walk around and i thought it would be just much simpler to do it with this regular transform and not use the nav agent this time later on we're going to use the nav agent for different things but for now we're going to do that with this very simple way okay so that's this script and then we are simply placing this script on our creature again this is the default value you can change it i just feel like it's very fast if it stands on something higher but you can play with that and here we're simply going to see the walk counter and the weight counter so i made them public just so we can see how much time is left and we can also see if it's working or not so now if we play we can actually see our little bunny running around so you can see he waits let me show you this in the just play and we can see that over here right so you can see we have the walk counter and the weight counter so he waits and then when the counter reaches zero he walks and then again he waits for a few seconds and then when it reaches zero he walks for a few seconds and then he waits for a few seconds so it always goes between these two and if we add more rabbits all we need to do is go to our prefabs and add more rabbit so we can put let's go here we can just add more small rabbits we can also put a big one like the king or the mother and we play and now each one of them will get a different value so they will not run and wait at the same time sometimes they will go at the same direction but that's just in the beginning afterwards they will all just be randomized so you can see some of them wait some of them run and it's actually cool because now we have i stepped on one and now they just walk around so later on we can do nice things with them we can maybe pet them or pick them up or even hunt them and take their meat i don't know it depends on you of course so you can see a very cute little rabbit and his rabbit babies and they can walk around inside the grass so it looks very very cool and now we can also do the same thing we did with the trees we're simply going into our scripts and we're going to take this interactable script and we're going to place it on our prefab so we have our white rabbit let's put this interactable on him so i'm simply dragging the interactable object script on our white rabbit let's call it white rabbit and let's do the same thing with the big white rabbit big white rabbit so i'm simply dragging and dropping this script on every item that i want to interact with for now we don't have any interaction we can just get their name but later on we're going to be able to interact with them so now if we play we can actually point on them it will say big white rabbit or simply white rabbit oh what happened to this one yeah there's some strange thing here with the rotation i'm going to figure that out you can see when they wait they actually rotate a bit but i think it has something to do with the animation and yeah also if the rabbit pushes him so there's a lot of little things we need to change of course later on and again the point here is not to get some kind of polished game but to just understand the way we can do all of this little by little we're going to polish as much as we can we're going to make them look better and the point that they're actually going to run in the grass so we're not going to really see any weird little things and for example we're going to go to a different place so there will be other creatures and other animals and we come back here we are still going to see these rabbits because eventually even though the movement is randomized they will always just walk in this area right not because i put any boundary but because they will always change the direction and just walk here i will be very surprised if one of them will reach far away maybe it will happen like if i play for an hour or two right but for now they all stay here of course if they reach the end of the world they will fall down but it's cool so this episode was a little bit longer because i want to show you these two things in the next episode we're going to finally pick up items so we're going to have maybe a rock or a stick and we're going to see how we can actually pick these items and of course for that we also need to create an inventory so we're going to create a ui for an inventory and pick up these items so we're going to approach them and just click and pick them up and they will appear in our inventory but that's all for this episode so you can do that with other creatures you can play around with these scripts and do whatever you want so thank you for watching please subscribe if you're still not subscribed you're going to see all the newest episodes this way it will just appear you can also click on the little bell and then it will actually notify you whenever there's a new episode and of course it will also support me so see you next time
Info
Channel: Mike's Code
Views: 44,837
Rating: undefined out of 5
Keywords: 3D, Survival, Game, Tutorial, Unity, c#, Inventory system, crafting, raycast, ai, pickup, items, unity engine, game development, how to create a survival game, mike schvedov, slot, backpack, selection, point, terrain, crafting system, fps, first person, survival game, unity tutorial, unity 3d tutorial, unity 3d, game dev, how to
Id: _GzSfnJNNI4
Channel Id: undefined
Length: 51min 24sec (3084 seconds)
Published: Mon Apr 04 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.