Unity Game Dev Full Tutorial - 3D Snake Game

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up guys fire here from awesome toots dot-com let us now import the assets for our new game that we are gonna create as you can see I have a brand new project this is a 3d project so when you create a new project check the check box or the radio button for 3d name your project save it in any location where you want to save it and create it here I only have the scenes folder with my gameplay scene which is this scene right here which is completely empty I did not import anything I was waiting for you to watch this video so now we are gonna go in our assets folder that you can download in the link below and select all of these assets so we have fonts materials models prefab sounds and textures so drag and drop them right here inside of the project tab and of course we will need to wait for unity to take mercy upon us and import all of these assets so let us wait one moment more no no no no no no and finally they are here so in this video I'm going to set up our main or gameplay scene and we're gonna do that by going inside of the prefabs folder and here we have our level and here it is I'm simply going to drag and drop it here in the hierarchy panel and voila this is our level I'm just gonna go here with my mouse and see it and this is our level now of course it looks like it's a little bit dark but now it's a bit white again so I'm gonna go quickly inside of this aligning tab and I'm gonna uncheck this out to generate and the Lightning tab is under window and then here you will see lightning and then you're gonna click on the settings which will open this tab right here it'll probably be floating for you don't you try if I can make it float No not here yeah finally so it will finally it will probably not finally be floating for you like this i dock it usually right next here to the inspector panel on my right side and when you select the lightning tab you're gonna go right here inside or the bottom of this lightning tab and you're gonna uncheck this auto-generate now this outer January notice here when I hover over it says automatically generates lightning data in the scene when any changes are made to the Lightning system this means if we take this directional light and if I even change it a little bit so if I change the rotation from fifty to seventy one it will rebake lightning it will try to simulate lightning and that takes time that's the reason why I have turned this off people try to rebuild the Lightning settings which will take a lot a lot of time for us and we will need to wait your computer will get hot so on and so forth that's why I am going to turn it off now we are going to duplicate this directional light but first of all for this light I'm gonna set the rotation to ten like this and it already looks pretty nice and the intensity I'm gonna set it at one we can set at 1.5 for example but one will do and I'm going to duplicate it so I'm going to quickly duplicate it here and this is directional light one and also for this one I'm going to set the rotation for the y-axis at zero so the position X is zero position Y is three Z rotation is zero rotation X is 10 Y is 0 and Z is zero this is for our directional light 1 for our direction light 2 so this one right here I'm going to set the Y rotation at 43 and do we want to set here point three for the intensity or something like this because now we're casting shadows here on the side and of course this all depends on the intensity it depends on the lightning I can also pump up the intensity which will be something like this which is something you use to kill vampires in your game anyways I'm not gonna go too much into lightning because it is a separate topic on its own I talk about this in the ultimate game development Academy in a whole section with a brand project or bad new project so a separate project for talking about lightning demonstrating how lightning works so it's a separate topic for itself but here I've just used this directional light I can actually remove this one so we can leave it at one you can play with the intensity if you want to but I will leave it at one and the rotation is perfect I have turned off the duplicate by the way and it looks nice it looks pretty nice if I go in the game this is how it looks like and by the way for the main camera here it is the z axis is going to be 0 the y axis is going to be 1 something like this and also the far for clipping planes I'm going to set it it's 10 something like this this what I did for the clipping planes it will simply if I pump it up pay attention to these parts right here let me just demonstrate which parts do I mean come on highlight tool where it is here it is you see this from camera this right here and this right here like what the camera is seeing so pay attention to that if I take my clipping planes and place it far it means it will see as long as the camera is stretching you see as long as the camera is stretching we don't need that much we only need 10 to stretch the camera right here where our plane is or our game field is and this looks pretty nice this looks pretty nice I'm pretty satisfied with this so we're not gonna touch this anymore but we are going to go and create our snake and I am going to right click here and create an empty game object like this zero zero and this game object is going to be our snakes I'm going to name it snake now for our snake we need to filter for our mesh which is our eyeball mesh and here it is eyeball I know I'm using an eyeball for our snake but hey nobody's perfect so don't judge me and I am simply going to take this eyeball and put it here inside of our game and we already see it is casting shadows let me just clear this console and here it is this is our eyeball I'm going to take this one the child one I'm going to remove this one which is the parent one and I'm going to put it as a child under our snake and let me just position so zero zero zero for our rotation I'm going to see here or set it negative 19 so negative 90 zero for the y 180 for the Z for our skill one one and one and by the way our snake here we need to set the z axis let me just five point eight something like this for five point eight so now it is visible and for our eyeball which is going to be the head of the snake so here I'm going to say head I'm going to set it to point 12.12.2012 scale I did not said what I'm going to set I'm going to set the scale so select it and the scale is going to be one point twelve 1.12 one point twelve now this is for the head of our snake we are also going to duplicate the head and I'm going to put it here and this one is going to be our node one I'm going to name node and let me just move it right here and this node I'm going to say it's point one point one point one it needs to be smaller than our head and let me just duplicate it one more time and I'm gonna put one a node as a child of our snake parent game object in this note I'm going to put it somewhere here something like this now that another the other note you didn't move it here you just move it below so let me just put this one a little bit closer to our snake yeah this works pretty nice and the last one is going to be our tail so this is why I have removed this one or this one is going to be our tail and for the tail I'm quickly going to add a sphere Collider and I'm going to attach a rigid body on it so attach a sphere Collider on this game object this one that we named tail and attach rigid body on it now uncheck this use gravity because we don't want to use gravity and duplicate it one more time I'm going to rename it to simply tail and put one of these as a child of our snake now let me just zoom in and for our tail here I'm going to set it at 0 0 and position it here something like this so we are gonna position it to be as a tail of our snake and for this one we're going to save it in prefab so I'm gonna go here and create a folder that I'm going to call snake and inside of the snake folder I'm going to drag and drop this tail and now we can safely remove it so tail can't be removed and now this is our snake if I go into gameplay scene you see this is our snake so the tail is here it has a sphere Collider and a rigidbody the node can also have or it needs to have a sphere Collider but we can uncheck it for this node so for the second one the one after the head we don't need the sphere Collider to be checked and the rigid body we are also going to add it and uncheck use gravity by the way for the tail also make sure that you check this is trigger and do the same thing for this tail right here check it to be a trigger the tail right here I meant the one that we dragged and drop into the snake prefabs folder for the snake's head which is this dude right here now for this one we are also going to attach a sphere Collider so sphere Collider make sure it is a trigger and I am also going to attach a rigidbody and for this rigid body I'm going to uncheck use gravity because we do not need to use gravity for the rigid body which we will see later on we will demonstrate that when we start moving because we are going to move from our code and if we let gravity do the thing it will pull us down which is something that we don't want now for this sphere Collider for the head we can actually also uncheck it you you can uncheck it because the top game object the top parent this one the snake it will have a sphere Collider and this sphere colliders radius is going to be point one or actually it is going to be one and the center is going to be let's see where it is just quickly let me see where my spherical area so here it is yeah it is large actually I need to resize a little bit more going here moving our sphere Collider yeah something like this I need to resize it more let's say Point 112 just so that I can see where it is you see this green line this is where our sphere Collider is if I set it at zero I think this is perfect yeah point one or point one one is where we want this sphere Collider to be I also am going to make it a trigger cutting this is okay let me just try to resize it a little bit soak it so that I can see where it is radius yeah point one one is for our radius of the sphere Collider I'm also going to click on add component and attach a rigidbody on this dude right here and I'm also going to uncheck this use gravity because we do not need it we don't need it and this is it preparation for our game we have the snake we have the level I'm gonna drag and drop the snake in the snake prefab folder so for the snake we have an empty parent top game object we also have the head which is this one right here or this one right here the top one or the largest part of the snake we have to know this one I just named that node because it's the second body part of the snake and we have the tail so every next game object that we add because you know when we eat our fruit the game object or the snake will grow larger it's the same as on all Nokia phones when you use that snake and you ate more and more the snake grows larger and larger so all of those other objects that we are going to add the larger and larger Arabic objects are our tails so the tail needs to be named like this so that we can differentiate it we have the head we attach sphere colliders on all of them so the tail and the sphere Collider fur the tail needs to be checked it needs to work because we can collide with the tail you know the snake you can kill itself if it touches itself so this is for the tail for the note it does not matter because we are not gonna do that for the notes which is the next part after the head head here it's also going to have a rigidbody all of these game objects have a rigidbody except for the head and the node the sphere Collider must be turned off or we can also remove it and for the snake we have the sphere Collider rigid body which is the topmost game object and this is preparation for our snake moving forward with our game we have prepared our level we added our snake we added two directional lights we turned one off now you can turn it back on or turn it off this all depends on you however you want to do it you can also turn the first one or the second one off and probably pump up the intensity on the first one however you want to do it it's up to you so I'm not going to touch that I will turn the second one on so both of them are turned on this is our snake in the middle of our screen and we need to start moving the snake in order to do that we need to go in our project tab and right click and create a new folder and this one is going to be our scripts folder and inside of this one right click and I'm going to create here folder for helper scripts now for our helper scripts the first script that we need to create is our script that's going to hold our tags so I'm going to name it tags and first of all right-click create a c-sharp script you are gonna double click it and open it in Visual Studio mana develop whichever one you are using now since in our tags class as you already know or assume we will add tags and default variables that we are going to use throughout our game so I took the liberty to copy them simply here and now you can pause the video and simply paste them but before you do that I am going to explain what they are now here it's player direction of direction without end anyways the tags here will represent the tag so we have the walls so when we collide with them and we'll check if the collision happened with a game object that has a tag wall when we pick up a fruit we will check that with the tag as well so these are the tags for our game object and here we can say tail with small T or a il I wrote it all caps and for our metrics so these are the metrics that we are going to use to position and move our nodes inside of our game or to move the snake we will see that do not worry about that and this is a public enumeration to determine if we are moving left up right down and this is the count eg or it will show us how many movements do we have so 1 2 3 and 4 so we have 4 movements now as you can see in our public enum we can say left is equal to 0 up is equal to 1 right is equal to 2 so we can do that as well this is something that we did not do before so I am showing you that we can do it and why are we doing it like this because later on we will use these values so 0 1 2 & 3 to access indexes in our array so don't worry about that you can pause the video now and copy all of these tags metrics and player direction and now I am assuming that you have copied all of those and I'm gonna move forward and go in unity and in script I'm going to right click and create a new C sharp or actual new folder and this one is going to be our player scripts and inside of this folder I'm going to right-click and create a c-sharp script now this one is going to be our player controller this is the dude who needs to be attached on our snake so select the snake drag and drop our player controller just like this I'm going to double click it and open it here in Visual Studio let me just do my thing as always and this is going to be the script that's going to move the player we are going to create another separate script for getting input that means we will move the snake or the player left right up and down and for that we'll use a separate script this script is the one that is going to move the player and of course we need here a couple of variables the first one is going to be our public player direction which I'm going to simply call direction and since its public it will be visible in the inspector panel I am going to hide it in the inspector we don't need it to be visible in the inspector panel but we need it to be public next we also need here and hide an inspector public float step length which is going to be equal to 0.2 F now this is the movement interval so we are going to move our game objects or the snake by this movement in terribles of 0.2 now we don't want to move our snakes every frame that 60 frames in a second so we don't want to move it every frame so we need a movement frequency so here I'm going to say public float movement frequency like this which is going to be equal to 0.1 and I'm also going to hide this bad boy in the inspector now you see we're going to move our snake every point 1 of a second instead of moving it every single time or 60 times in a second that's why we need this movement frequency now we also need a private game object which is going to be our node prefab or actually our tail prefab so you can name a tail you can name it note however you want it it is private it will be only accessible here but we need to make it serializable or serializable or serialized field because we want to drag and drop our tail if we go back here in unity and notice here in the inspector we will see an empty field to drag and drop this game object for again it is located in the prefabs and snake select here the snake and drag and drop the tail right here I'm also going to hit apply to make changes to this prefab now this is why we need to make it a serialized field because we want it to be private but still we need to drag and drop that game object in side now we also need to store the next position displacement based on player direction and for that we are going to use a private list of vector threes which we are going to call Delta position this is where we are going to store the previous movement of our player don't worry we'll see everything in action we also need a private list of rigid bodies which I'm going to call nodes and this is going to be every single separate node inside of the snake's body that is snakes heads and snakes tails so on and so forth we also need a private vector3 and for this one fruit node actually we don't need this one so we are not going to you this one but we are going to use a private rigidbody for main body a private rigidbody for the head body and a private transform TR for our own transform sword TR not transform we also need private float counter and private bull move now this counter we will use it with the movement frequency to allow the player to move and we can put these two right here so that they are put with the appropriate or right below the appropriate variable that will accompany them later on and the last variable that we need is a private bull create node at tail this will tell us do we need to create a node at our tail that is when we eat a fruit do we need to create a node or not now instead of me explaining all of these variables one by one what they are I will demonstrate that so in the awake so I'm going to change start to awake in the away the first thing that we're going to do is we are going to say our TR is equal to our transform TR is this one right here so we're going to get it with our or our transform is equal to TR next thing our main body is going to be equal to get component and get the rigidbody component from our player and that is the top head or the top main body of the snake so the top parent which is the empty game object so this one right here that we currently have selected in the hierarchy we have that one selected that is the or the one that we are getting it's rigid body by using getcomponent the next thing that we need to do is we need to call here in it cynic and nodes now this is a function that we still did not create and right below that function we are also going to call init player and this is also a function that we still did not now let us go and create that right below the update we're going to create void in it snake and nodes now in this one we are going to initialize the nodes that means every single separate part of the snake as you can see if I go inside of the scene view you see we need to get all of these nodes so we need to get the head of the snake so this one we also need to get this node the middle one and the tail one so we need to get all three of these so here in our in it snake nodes we're going to see nodes which is our array of rigid bodies or a list you see here of rigid bodies and we are going to make it a new list of rigid bodies and below here we're going to say nodes that add to add to that list and we're going to say TR which is our transform get child and we're going to get the child that's at the zero index dot get component so it's component rigidbody component just like this close parentheses and we're good to go copy paste copy paste and we're going to say child 1 and the child 2 and what is going on here now before I explain the last thing that we need to do is our head body is equal to nodes and the element that at index number 0 what is going on here you see here we are using TR which is our transform you see here our TR is the transform get child and we are getting the child what does this mean you see our transform the topmost transform from our snake it has children so we have the node or the head the node in the tail and we know that or the transform knows that we have children assigned to it so we can use or access those children's by using indexes so you can say get child 0 which means it will get the first child or the child that's at element 0 which is in our case head get child 1 in our case it's the node get child 2 in our case is the tail and here notice the head body it is the rigid body of the head snake so the rigid body of the head snake and we know the head is set at index number zero which I explained a moment ago now in order to initialize our player so right here below we are going to create in it player simply here we are going to make sure where our player is moving left right or up or down so switch so we're going to use a switch and we're going to use direction and we are going to use cases in our switching case so we're going to have case player direction a right and here we're going to use break I'm going to copy this paste it so we are going to have direction left up and down and here I'm going to simply say down so based on the direction we are going to position all of our nodes or shift them to that position so if we need to or if our starting position is the right position we are going to do this so we're going to say nodes and the element that's an index one that position is going to be equal to nodes in the element that's at index 0 - new vector3 and here for our X we are going to say metrics that note and 0 F 0 F and for our for our nodes number 2 or the element that's a number 2 and by the way here we need to say note that position so the position of our node 0 and by the way inside of our nodes we are adding rigid bodies so when we say position we are getting the position of the rigid body you see here when I hover over it says the position of the rigid body so we are getting the position of the rigid body and here we are going to say nodes and the element that's at index 2 now we are going to say that position is equal to and we are going to use notes an element that's at zero dot position - new vector3 and for our metrics so we are going to say metrics that note and we're going to multiply that by 2 and 0 F 0 F what is going on well when we start our game to the right we need to shift the positions of our game objects to the right side and in order to do that we need to get the node 0 which is the head what we need to do essentially if we are starting to the right side that means when we start our game that means when we start our game we'll start moving to this side what we need to do we need to shift the position of this node over here and this node over here because then we are going to move to the right or actually to the left side excuse me I'm moving to the left but assume that I said left so if we are moving to that left side we shift their positions to the right side by using + now let me demonstrate that so I'm going to move them back here and notice here if we are going to move to the left so I'm simply going to copy these lines of code and paste them here instead of typing here - we're going to say + you see here we are doing the same thing except here we are doing that with the plus sign so let me go back here and demonstrate that with the left position if we need to start moving to the left position with arsenic that means we are going to this side and we know that the head so this bad boy over here he needs to be at the top so he needs to be the leading node in our snake in order to do that we need to shift this node over here and we need to shift this node over here and this is exactly what we're doing right here so if we need to move to the left side use the node that's at element 1 and use the element 0 node which is the head and add to its position the meth Rick's node which is the displacement that we saw here this is the displacement of our nodes so this is how far away they are from each other and do the same thing for our second node essentially this code right here is going to position these nodes relative to the node zero meaning and let me just go back here meaning our head is the relative position so our node here this code right here is going to move this node over here and this code right here is going to move this tail node over here this is what we are doing essentially now we are going to do or everything that we explained here for the left side applies to the right side except while the right side is opposite to the left for our up side we are going to do the same thing except for the y-axis so for the Y side we are going to say - but our metrics node is going to be on the Y so 0 for the 0 F for X and we are going to move that one on the y and same thing here so we are just going to take this and 0 F for the X and for the Y metrics node x 2y x 2 because our node at second position is our tail node so we need to multiply that by 2 because our head is here our node is here we need to multiply this by 2 so 1 head is 1 node is 2 and multiply that by 2 moving that right over here this is why we are multiplying with 2 for our down position simply copy this code from the up position paste it here and for the down we are going to say plus like this and voila we are good to go now of course we need to create one more function so right here above our in it player we're going to create void set Direction a random and here we are going to say int Direction random or die dir random is going to be equal to random that range from row up to you we need to say here int and we're going to say player direction that count to convert the count to an integer because if we use it like this without casting it to an integer and we go back in unity we will see here best of overloaded method match for random rage float float we need to put this into a float or an integer so I'm going to use it like this because our account if you remember has a value of four but this is an enumeration and we can cast this enumeration to an integer I've talked this already once in one of my tutorials can remember which one but anyways we can cast it into an integer because as I said the value is four so the direction random will be based on random range and notice here we are casting this value to an integer and simply here we're going to say Direction is going to be equal to di are random now same way as we can cast these enums to an integer we can use enumerations to cast integers to enum so we can see here player Direction now based here on the random value let's say random value is equal to 1 so this di are int or di are ran the value will have a value of 1 when we convert that into her into our enumeration player direction and we go back here the value 1 is up it will be the same if the value is 2 so value 2 is right if the value is let's say 3 then the direction will be down now we do need to call this right here so set Direction random we need to call it right above our switch in case statement so we need to call it right there and we need to go back here and we can unfit or uncomment almost set on position we can uncomment these functions now now pay attention if we go back here I am going to clear the console and we go here in our game pay attention how the snake is positioned say keys but it's positioned with right direction in mind so it looks like it's going to start moving this direction notice now when we run the game so I'm going to hit the play button you see now it is positioned like it is going to go downwards you see and if I remove or stop the game and play it again you see now it is positioned again to the right side stop and rerun the game it will be positioned now you see like it is going upwards so this is how we are moving these nodes relative to the head node this is how are we positioning these nodes by using these lines of code right here now we are still still not done here with the awake function we need to create our delta position or which to go or position in which where we are going so our delta position is going to be equal to new list of vector threes like this open close these curly brackets and here I'm going to pass a new vector3 and here I'm going to see comma now for the X I'm going to say negative step length comma 0 F for the Y I'm going to copy this and paste it below and here I'm going to say 0 F first so 0 F and here I am going to say step length comma here paste this one right here now here we are going to have positive step length and I'm going to copy this and paste it here now another comma here remove this comma and here we're going to have the negative length now right here I am going to add comments and here we need to end this statement with our semicolon so I'm going to add comments here like this so this one is going to be our negative D X or going to the left side here I'm going to put it like this this one is going to be our D Y or going to the up direction right here I am going to say this one is going to be D X or going to the right side and this one and let me just move all of these by one and this one is going to be our negative D Y or this means going to the down dier so you see here this step linked this one right here is actually the value or we are going to move by that value so this is the time interval that value when we move we will move by zero point two units this is what I'm trying to say so when we have this right here this means negative dy we are going to the left side or negative x so to say here we have the positive Y meaning up here we have the positive X meaning right and here we have the negative Y going down and you see here negative Y positive x positive Y negative x so this position right here means we are going to the left up right and this one is going down now in order for us to actually move we need to create a function that is going to move us but before that we also need to create a function here we need to create a function that is void check movement frequency and for this function we are going to say here our counter so counter plus equals time Delta time we know the time that Delta time is a difference between every frame so it's the time it takes from one frame to go to another frame and we're going to say here if our counter is greater or equal to movement frequency what we are going to do we are going to see our counter is equal to zero to start counting from zero again and here we are going to say move is equal to true to allow our game object to move now in order to make it move we are going to see here void move or create a function that is going to allow us to move and not on animator move no simply void move and inside of this function we are going to create here a vector 3d position this is the delta position we are going to use here Delta position so it's actually Delta position and we are going to use that by using here an int and we are going to pass here the direction so the Delta position or D position is our Delta position we are casting the integer or direction to an integer to get it as an index so depending on the index if we are going to the left side for example if you're going to the left side index will be zero meaning we will access element that's an index 0 in the Delta in the Delta position and if we go here in the Delta position element that's at index 0 is left as you can see right here so now that we have our Delta position what is the next thing to do first of all we are going to create here a vector3 parent POS of parent position head body that position and right below here we're going to create the vector 3 preview or the previous position and below here we are going to say main body that position is equal to main body position plus D position right below we are going to say head body that position is equal to head body dot position plus the position again now we are also going to move all the snake notes so we're going to say for int I which is equal to 1 as long as 1 or I is less than notes that count our I is going to be plus plus and we are simply going to see here previous position is equal to nodes element that's an index I that position storing the previous position nodes and element that's an index I that position is equal to parent position and parent position is now equal to previous position now don't worry if I'm not explaining everything in detail right now because we will test it out soon do not worry after we do all of this we need to check if we need to not Trevor its check if we need to create a new node because we a fruit we will do that later on and for that we're simply going to say if create note at tail then we are going to write the code to do that for us now practically we are done for now and we can test it out by going here in the update function we are going to call check movement frequency which is this one right here comma go down this is check moving frequency in the update we are going to calculate our counter by adding to it Delta time and if the counter is greater or equal to movement frequency then we will allow the movement and in the fixed update right below here so void fix update and let me just remove this private because I like it to be void simply and in our fixed update we're going to check if move so if we can move then move is equal to false and we're going to call the function move not fog mode move like this thank you like this so in the update function we are checking for the frequency so counter plus equals time Delta time if the counter is greater or equal to the movement frequency then we are going to see counter is zero so that we can count from the beginning from zero and move is equal to true in the fixed update if move is equal to true move will be false and move the snake by one increment and in the move here this is where we are going to move the snake now in order to test that and see it in action I am going to go back here in unity and run the game and we're going to see the movement of the snake and then we will go and break it down a line by line so when I hit the play button pay attention to you the snake you see snake is moving by one increment it is gone we cannot control the snake notice now if I hit the play button now a snake is going downwards just a moment ago first time when we test it out snake was moving upwards I'm going to hit the play button again hopefully it will move left or right to know again moving down or up come on try to move left or right just for once okay now it is moving to the left side and you can test it out as long as you don't see it moving left right up and down but this definitely works we see it now let me try one more time hopefully it will go to the right side no anyways let's go break it down line by line so these here are now getting a clear picture or we are understanding for what we are using this direction is going to determine in which direction our snake is going to go step length is the frequency or the units measurement unit by how much units we are going to move every node that is point two units movement frequency is the how many times we can move in a second so every point one of a second we can move counter is used to test that out so we are adding to our counter the Delta time we know that is the it's a very small number so the difference or the time it takes from one frame to go to another frame so that's a very small number and when that counter value is greater or equal to the movement frequency which is this value right here then we will allow our snake to move so counter is reset back to zero and move is equal to true and moving forward this is our tale prefab which we will use later on when we eat a fruit or when the snake eats the fruit we will add to the snake so it's get it gets larger and larger this is the Delta position and you see here Delta position is the direction where we are going is it left is it up right or down and we're using that by well using the step length and these values right here you see here the negative step length is negative X meaning we are going to the left side positive Y that means up and positive step length that means going to the right side negative Y means we're going down now here are the nodes that we are going to get when we initialize the snake basically these are the rigid bodies of every part of the so the head the note in every part of the tail so we can have 20 tails when we eat 20 fruits every single one of those tails is going to be added to this node array you see here we're using the transform to get the child's so we are getting the child at index zero one and two child at zero index or at 0th index is our head so we are doing or assigning that right here and moving back here we have the main body head and transform so the main body is our main body or the top parent or the parent of all game objects so this one right here the snake that's the main body our TR is the transform and here we are initializing the snake nodes by getting the rigid bodies from every child next we are initializing the player that means we are setting the direction the initial direction of the movement of the snake we're doing that with this function which is this one right here so you're using a random range from zero and direction that counts so player Direction count we are casting that to an integer because the value here of the count is equal to four as you can see here count is equal to four so when we cast it to an integer it will display that value so it will display four which means a random range from zero or between zero and four and when we get that random range let's say that number is three we will cast it back to player direction so if the value is three three is down if the value is for example two then two is right and you get the picture so we are assigning that to the direction now when we do that in this function based on which direction we have is it right left up or down we will displace these nodes and we saw that we have previewed that so let's say for example if the initial position is moving to the left side if I hit here pause and I'm going to zoom in on our snake I'm going to hit the play button you see here the initial direction was left side which means both of these this one here and this one here shifted to the right side by using these lines of code so using the head position of our snake as orientation and we're adding to that position this value here now metrics node is this one right here this is the increment or the stepped length this the same as step length and for our second node we need to multiply that by two because we need to also place it after our middle node because we have three nodes same applies for right up and down now moving forward here we have our check movement frequency we have explained this so we are adding to the counter Delta time when the counter is greater or equal to the movement frequency reset counter move is equal to true this happens in the update function in the move function we are getting the Delta position meaning the direction where we are moving so we are moving by using Delta position which is our array that we create right here and we are casting to an integer the direction if the direction is for example or the value of the direction is 2 that means we are moving to the right side next we are getting the parent position which is our head body position and we are storing the previous position now first we need to start moving the main body which is the main body of our snake the top parent holding all nodes and we are going to move it by using or setting its position to be equal to its current position plus the delta position or the direction where we are going so the direction position we're doing the same thing with the head body so head body's position is equal to head bodies current position plus the direction where we are doing now depending on if we are going to the left we are going to plus and the value for the left let me just see here the value for the left is the negative step length and 0 for device so it will move it to that direction now we have to move all of our nodes we moved our head already that's why int I is equal to one because here if you paid attention the element or the first node is the node at element zero which is our head node and we already set the head body or the head node we moved it so now what we need to do is move all other nodes that's why I start at one and not at zero and we are going to move how many nodes we have if we will have 100 nodes then we are going to use count which will be 100 so we will move 100 nodes now the previous position and we are getting that by using the node at the current eye index in its current position this is the previous position the new position for that node is the parent position so the parent position that we had here for our head and now the parent position is equal to the previous position because the next node will be repositioned by using that same position and the outcome for that is if we go in our unity and if we hit the play button you will see how our snake is moving come on unity please have mercy upon us so if I hit the play button again you will see that the snake will move in the increment or using incremented values or if we go here the step length that is this is how many units it will use two moves moving forward with our game we have the snake moving and it is moving in a certain direction the problem is that we cannot control that we cannot control the movement of the snake when the game starts so when we press any button we will not change the direction of the snake so in order to fix that inside of our player scripts folder we need to right click and create a new c-sharp script which we are going to call player player input in this script we need to attach on our snake right here so drag and drop our player input hit apply so that this change applies to the prefab and double-click it and open it in Visual Studio man I'm in saying open it in monodevelop but hey people change using Visual Studio I have changed anyways moving forward what do we need for our player input the first thing is our player controller so we need a private player controller that we are going to call player controller of course this is because we need to inform the player controller that we change the direction where the snake needs to move now we are also going to create a private int horizontal which by default is equal to zero and vertical which by default is equal to zero as well we also need to create a public enumerations of public enum that I'm going to call axis and you can assume which axis do we have so we have horizontal and we have vertical because well we have two axis no inside of our start function we can get our player controllers so player controller is equal to you get component get component player controller component like this and this can be done in the awake function as well so you can choose to put this in the awake if you want to we can leave it at awake because we are only getting here our player controller because I like to initialize my variables in the awake function more than I like to initialize them in the start function now inside of the update the first thing that we need to do is that we need to get our player input but before that we need to say horizontal is equal to zero and vertical is equal to zero to reset their values and now in order to get that input we need to call a function that we did not create that I'm going to call it get keyboard input like this and right below here we are going to created by typing void in the name of the function which is get keyboard input and now inside of our keyboard input we're simply going to say horizontal is equal to and we need to say input that get access or raw and here we are going to see horizontal and for our vertical we are going to see vertical is equal to input that get access or raw and it is going to be vertical but we have an error what is this error if I hover over it says cannot implicitly convert the type float to an integer the problem is that this returns a float you see this right here this input get access raw returns a float so we need to cast it to an integer by typing in parentheses int in front of it and the same thing we need to do it right here so we are casting or converting this float into an integer so we have here our horizontal and vertical we do need to test if our horizontal is not equal to null then we need to say our vertical so vertical is going to be equal to zero like this and why do we why did we type this here well if we move horizontal we need to set vertical at zero and this is to prevent double movement so just to prevent double movement this is why we are doing it like this now in order to test this out we need to create a function that we will still did not create and this function let me just type this out here I remove this curly bracket now right below our get keyboard input we need to create void set movement and inside of our set movement we are going to move our game object but you are going to do it or call it right here right below our gift keyboard input now inside of our set movement here we are going to say if our vertical is not equal to zero then we are going to call our playercontroller to change the movement but in order to change that movement we need to go here in our player controller right below our check movement frequency we need to create a public void set in put direction which takes a player direction dir as a parameter now first of all in this function we need to prevent movement in the opposite direction what does that mean it means that if we are moving up we cannot start moving down we cannot start moving down or if we are moving left we cannot start to move to the right side and vice versa so we need to say here if our dir is equal to player Direction up and our direction is equal to your player direction down or if our dir is equal to player direction dot down and our direction is equal to player Direction up you see my point because we cannot move in the opposite side because if the let me quickly go here in unity and if I take my snake in the scene let's assume snake is moving in the right side like this we cannot press left and it will start moving left now we cannot do that we need to start moving up and then move left or down and then move left this is my point that's why we are testing this and we need to say here as well if di r is equal to player Direction that right and Direction is equal to player direction that left or if di r is equal to player direction that left and direction is equal to player direction right so if this is true if some of these is true then we are going to hit return and not rect transform or whatever that was if one of these is true and notice we are using or so only one of these needs to be true so if Direction is equal to or dir is equal to up and dir is the parameter so if d IR is equal to up and Direction is equal to down and that is true we cannot move to the opposite side same thing goes for down and up right and left left and right but if this right here is not true then we are simply going to say Direction is equal to D I are now direction is our own variable that we are using here let me just find it here it is our direction we talked about it in the previous video so Direction is equal to the parameter that we put here now we also need to force move so we need to see here force move our game object or our snake now our force move we're going to create it so force move and simply in our force move we are going to say counter is equal to zero move is equal to false and move call our function to you move the snake immediately without waiting for the next movement frequency threshold well I will because when we press the button we want to move the snake right away we don't want to wait for the movement frequency threshold to kick in so that we can actually start moving the snake so right away when we press the button and this is what we are doing here so we are resetting the counter move is equal to false and we will call move to right to move the snake right away now going back to our set movement now if our vertical is not equal to zero we are going to say player controller that set input direction and here we are going to test if our vertical is equal to one question mark then we are going to say player direction that up call on player direction that down what are we doing here what is going on well you see here we are testing if vertical is equal to no this essentially this code right here and let me just take this code so this code right here that we did and let me just put it right here so that can actually see it this is what we are doing pay attention here so if our vertical is equal to one and we are using or questioning that or asking that with a question mark so if it's equal to one and that is true then the value will be up otherwise the value will be down this is essentially if as if we are doing this let me just put like this and this is essentially same as if we typed if vertical is equal to 1 then our player Direction is equal to player Direction up so player Direction up else if vertical is not equal to 1 then player Direction that down this is what we are asking with this right here so if the verticals value is equal to 1 then we will set Direction up which is logical because vertical that means that we will go upwards on the y-axis if and just go back Wow why is it taking me over there anyways otherwise if our vertical is not equal to 1 this is for this we are using this : so if it's not equal to 1 and let me just go back here so if it's not equal to 1 that means that we will use Direction down for the value meaning we will go down now here we are going to say else if else if our horizontal is not equal to 0 we're going to do the same thing so we're going to say player controller that set input direction and here we're going to say horizontal is it equal to 1 question mark and right here we're going to say player Direction that right call on player direction that left it's the same thing except for our horizontal so it's exactly the same thing now we can test this out but we will have one problem and we will see what that problem is so if I go back here I am going to clear the console and I'm going to hit the play button and notice now we can change the direct you see we can change the direction but you see the snake is moving like crazy as long as if we are holding our mouse are actually our keyboard I'm pressing W key ASD or left arrow right arrow up you see the snake is moving like crazy like some crazy parasite that came from out of Earth you know alien parasite and it's trying to get into you now we don't want this we don't want this kind of movement we have this movement because we are using input get access raw now this is not something that we want so I'm going to comment this out we need to create another function that will get us the movement of the snake now here we're going to create a function that will return an integer and we're going to call it get access or raw which takes an axis and I'm going to call it axis as a parameter and it takes this axis that we created this enumeration right here now right at the bottom of this function we are going to return zero if nothing happens because we need to return an integer but what we are going to do here is that we are going to test if our axis is equal to access that horizontal so we need to check for the horizontal axis then we are going to say bull left is equal to input that get key down did we press a key down and that is going to be key code key code that left arrow and bull right is going to be input that get heat down and this is going to be key code that all right arrow like this so we are going to get here the boolean for the left and the right now you can do the same thing for our W a s d I'm using here arrows so pay attention because we will use arrows left arrow right up and down arrow on your keyboard to move our snake now you can do the same thing or W key ASD and to move that that snake with those keys so here I'm going to say if left meaning if we pressed the left arrow key then here we are going to say return return negative one because we're moving to the left side if right then we press the right arrow key we're going to say return one like this otherwise we are going to say return no like this else if so else if our axis is equal to ax is that vertical so we need to check for the vertical axis then we're going to say bowl up is equal to input that get key down and we are testing for key code so get key down and it's key code that up arrow our ball down is equal to input that get he down and he killed that down arrow and same thing so we need to test it like this so we need to say here if up so if up we pressed the up arrow key we are going to return 1 if down then we are going to return a negative 1 so return -1 otherwise return 0 or return null I'm saying null for 0 but actually it's return 0 so this is what we are doing and let's go inside our keyboard input and right here our horizontal is going to be equal to get access a raw passing here access that horizontal our vertical is going to be equal to get access raw passing our axis that vertical so it's vertical like this voila so if we go back now in our unity and if I press the play button and notice now if I try to move you see the snake I'm holding the UP button so I'm holding these buttons and I'm pressing for the snake to move but the snake is not moving like crazy like you did a moment ago and notice here pay attention now you see the snake is moving and let me just go and demonstrate this so in our playercontroller if i uncomment or actually comment these lines of code out you will see how this helps us preventing the snakes movements so pay attention now if the snake is moving you see now we cannot move it like this you see it's moving to the left and now we're changing it to move to the right or now down and now it's moving up you see we cannot move it like this we cannot move the snake like this because the snake then we'll hit itself and it will die because remember if the snake or in the snake game on old Nokia phones or any snake game when the snake hits its own tail it is that long to put back Wow how are you want to call it anyways it is dead the game is over so that's why we are preventing the movement in the opposite direction because now if I am moving for example to the left side I cannot press right to move so I'm praying I'm moving to the right and I'm pressing left it's not working I'm moving to the left pressing right from moving up pressing down it is not working we need to turn left or right and then move to that side where we want to move and also let's go and briefly break down all of these codes now this right here it's pretty clear this is clear what it is this right here as well here we are resetting horizontal and vertical every time after the input and here we're getting that input now pay attention here we are using horizontal and our function get access raw and here get access raw here horizontal here vertical this is clear as well so here when it's horizontal we are checking for get key down if we press our key code down for the left error or the right error we will store that in these boolean if we press the left or if we pressed right we will return negative 1 or 1 depending on which these we have pressed otherwise we will simply return zero same thing for the vertical axis if we press keycode up or down this will trigger or it will set the appropriate boolean out of these two to be true if we press up will return one if we press down we will return negative one because down on the y axis is negative up is positive on the left side for the horizontal left is negative right is positive we did this probably thousands of times in my tutorials on youtube channel in the ultimate game development Kadim ii and i talked about this thousands of times and this is basic math if you go to the left side on axis it is the negative side right side is the positive up is positive down is negative so moving back inside of our get keyboard input we are storing that value in the horizontal so it will be either 1 or negative 1 or 0 and here we are checking if our value is not equal to 0 so we need to check if it's not equal to 0 because if it's equal to 0 we are not moving so we are not moving if it's equal to 0 so if it's not equal to 0 it's either 1 or negative one if vertical is equal to 1 then we will set it up which means positive if it's not equal to 1 meaning it's negative 1 then it's down same thing for the horizontal and in our playercontroller we created set input direction we are preventing here moving to the opposite side and we tested this out a moment ago and when that happens we're setting the direction to our own or dir this parameter to our own movement direction and then force move the snake just so that it moves right away instead of waiting for the threshold of the movement frequency moving forward with our snake we can move it and we can control it so let us now detect collision on the walls and also add our collectibles now first of all we're going to start with our walls and if we go here in the level we have these walls right here so all of these walls and let me select all of them so we have top laws we have bottom walls left and right walls select all of these walls except don't select their parents so bottom walls this one right here left walls and right walls we don't need to select these so select all of the vols except for their parents as you can see I did not select top walls game object bottom walls left walls and right walls because we are using that as a parent to store all of these children so now when we have all of these we are simply going to click on add component and we are going to add a mesh Collider so mesh Collider component on all of these which we can detect collision on them and I am going to tag them so click here on add tag and I'm going to create a wall tag I'm also going to create a fruit tag and a bomb tag so select again all of these walls except for their parents so select them except the parents and tag them with the wall tag and voila we are good to go and also for these obstacles here which are these as you can see so these right here if I turn them off so these also set their tag to be wall and I'm going to select 11 and level and hit apply so that this change applies to the prefab and if we go inside of our playercontroller and right here inside of our void on trigger enter in our ontriggerenter we can test if we collided with the wall except here for other I'm going to say target because I like to name this parameter target and I'm going to say if our target dot tag is equal to tags that wall we are simply going to print here so print touched wall like this so we touch the wall and that is that this is just for testing purposes we will fix this later on do not worry about that so if I go back here in unity and if I try to collide with some of these wall obstacles pay attention in the console so now when I hit one of these BAM we should see inside of our console that we have collided with the wall now of course we collided with the top walls but we are not colliding with the obstacle walls that is because we need to select them as well and attach a mesh Collider on them we forgot to do that so if I hit the play button again and if I go through these walls that our obstacle walls band you see touch the wall touched wall touched wall when we go through all of these so this is working we are detecting collision between our walls and now moving back in our project we are going to import the bomb and the fruit now here I'm going to filter for the eyeball in our project and drag and drop the eyeball right here and I'm going to filter for the bomb and I'm going to drag and drop the bomb also in our hierarchy panel and inside of our prefabs I'm going to right click and create a folder and this is going to be our pickups so pickups where we are going to store the eyeball and the bomb and here actually I'm going to name the eyeball to fruit and bomb balls simply to bomb I'm going to tag the fruit with the fruit tag and bomb with the bomb tag I am also going to remove the animation from the bomb and for the fruit remove the animation as well I am going to change the scale of the fruit to point one point one point one the z axis is going to be five point eight and we are going to attach a sphere Collider on it and actually let me just take this fruit so eyeball this fruit right here and I'm going to name this game object here as fruit and this one I'm going to save point one point one point one rotation I'm going to say zero this rotation negative 90 and on this one I'm going to attach a sphere Collider make it trigger and let me just see where it is here it is this is our eyeball or our fruit that we are going to use as a fruit that is and I'm going to tag it here with the fruit tag come on selected where it is fruit not bomb what is wrong with me so tag it with the fruit tag and for the bomb I'm also going to change that so I'm going to save point to point to point to the Z position five point eight for the bomb and here is our bomb we are also going to attach a sphere Collider on the bomb and voila this is it so if we go inside of our prefabs and pickups I'm going to drag and drop the fruit and I'm going to drag and drop the bomb prefab if we go back in unity here if we touch the wall I'm also going to see or if our target tag is equal two tags so tags come on tags that bomb we are also going to print now touched bomb because we're going to check if we touch the bomb now for our fruit we are going to do the same thing so we're going to say if our target dot tag is equal two tags that fruit tag and here we are simply going to deactivate it so we're going to say target dot game objects at active is false and we need to see create a node at the tail is now equal to true because remember here we are going to create a tail because we picked up a fruit so we need to create a tail now first of all we are going to test if this actually works and then we will see how can we create that tail so first of all we are going to pick up the bomb and see if we are printing in the console that we are picking or picked up the bomb so BAM we touched the bomb that is good we will end the game when we touch the bomb now we are going to pick up the fruit and when we pick it up BAM it is gone but we need to create a node at tail or make the snake larger because we are we picked up a fruit so if I go back here in Visual Studio this now is true create node a tail is now true so when we go back here this is where we need to create that so first of all when this is true we are going to say create node a tail is now equal to false because we only want to create node a tail once we don't want to create it multiple times and if we continue what is the next thing to do is that we need to create a game object so we need to say game object and we need to say new node is equal to instantiate and we are going to instantiate the node prefab or tail prefab and we can see here new tail or new tail or new no the actually new node field prefab and we are going to instantiate it where where we are going to instantiate it using nodes and we are going to say notes that count - 1 dot position position dot quaternion identity like this and let me just move quite on your identity here what are we doing here we know that in our instantiate we need to pass a game object that we want to instantiate or create a copy out of that game object we need to pass a position and we need to pass a rotation rotation is not important quaternion identity 0 0 0 what's important is the position notice here I'm using nodes and nodes count minus 1 dot position this is the last node in the nodes array that we are storing here and remember in the beginning we stored 3 nodes our head our middle node and our tail and I said every new tail node will be stored inside of this nodes array because we need to keep track how many do we have and if I go back here what is the next thing to do we need to say new node that transform that set parent and we need to set our own transform and here we're going to say you see here world position if world position stays true the parent relative position scale and rotation are modified such that the object keeps the same world space position rotation and scale as before so when we see here true it will simply make this node keep the same position same skill and everything relative to its parent and here are simply going to see nodes add and we are going to append to it new node that get component and we're going to append the rigidbody component like this because I said we are going to add every new node inside of this array now we can see this the best way to see this is if I copy and paste a little bit or multiple of these fruits so we can have one fruit here I'm going to duplicate in one fruit here one fruit here for example one fruit here and one fruit here just so that we can see that our snake is growing larger so now we have three of our snakes or three parts of our snakes now we just have four you see now we have four and five you see the snake is bigger and notice now again picking up these you see the snake gets bigger and bigger again picking one one more one more the snake is bigger and the bigger we pick up all of these you see we picked up all of these nodes and you can see that every single one of those nodes are actually those fruits are being appended at the end of the snake because we are using nodes and nodes count we are getting the element that's at the count minus one which is the last element inside of that array so the last element inside of that array we are getting that and we are positioning we are positioning that node at that position so if the last node let's say if I highlight or use my highlight tool so this is the head of the snake this is the node this is the tail and this is another tail so let's say we eat a new node or new fruit here so we eat it nom nom nom and now we want to append it to the snake so the snakes position or the snake grows larger so this node needs to go here that's why we're using this right here nodes count so this right here position which is the last position count minus one it's the last element in the array which is this one and its position so we are using its position to set it so append that right here so appended right after that position this is what we are doing and basically that is that what we can also add here is that now we can say if so here we can say or if our target dot tag is equal two tags that tail tag so if we touch our tail so you're also going to say print snake touched tail and if I go back here we need to make sure that our snakes last part has a tail you see here it needs to have a tail tag so add tag and here we are going to say tail tag so select it and this one will have a tail tag hit apply and if we go back here for our snake if I select it and the tail that we add here this tail also needs to have the tail tag because the snake will die if it hits itself and notice if I pick up some of these fruits and when we pick it up the snake will get larger and larger so then we can touch our own tail notice now we touch our own tail you see here snake touch and I spell touch with douche so snake touch the tail you see we touched tail and bam we should die when we touch our tail but we will do that later on don't worry about that the point is that this all is working we can remove all of these fruits so let me select all of these fruits we can remove them we are only left to create our gameplay controller who is going to spawn all of these fruits in our level son and so forth and also create audio effects but before we end this video I'm also going to create our UI quickly because our UI is not going to be that large so if I go here in our game our canvas is going to be set on screen split screen space overlay I'm going to scale it with screen size 180 by 720 and I'm going to match the width and height equally I'm going to take the image this is going to be our icon so snake icon and I'm going to set the anchor at the top middle I am going to set here for the source image this snake portrait or however we are going to call it the width and height is 8080 for this snake the position X is negative 99 and the position Y is negative 5/8 like this and the next thing we are going to add our separator so I'm going to take here I'm going to actually rename this one as well UI canvas I'm going to hit here right-click and you I text now this text is not going to be a child of our snake icon it is going to be a regular text I'm going to call it separate or separator and I'm going to simply add this line to separate and we will see what we will separate I'm going to set the font at Arial and let me just resize that I'm going to set the anchor at the top middle the width is 29 and the height will be 135 the position X will be negative 32 and the position Y is negative 49 the skill or the font size is 103 and I am going to set the color to white and voila this is it and actually we can move it just a little bit down something like this yeah this can do so negative 62 negative 62 can do and voila this is our separator now I named it separate tone it's separator and we are also going to create a score so here instead of separator I'm going to call it score now for the school our top anchor the width is going to be 303 the height is going to be 83 the position X is going to be 1/5 1 and the position Y is going to be negative 60 and here I'm simply going to say score like this zero and let me just find it where it is where is my score where did I put it yeah here it is here is my score and let me just resize the font something like this so the font can't be 50 here in this case I'm going to set the font at 50 and maybe just move the score a little bit down negative 72 so negative 72 for our score now the font for our score is going to be our open son's irregular open son's regular here it is and voila that is practically that this is going to be our score or maybe just move a little bit so negative 6 9 this works perfectly so this is our UI here is where we are going to keep track of the score this is our snake image this is the separator between the snake image and the score not that fancy because the point is not to create fancy you I depend the point is to create our snake game also make sure that you attach or drag and drop the bomb inside of our pickups so drag and drop the bomb and the fruit to make them prefabs a lot of wrapped things up starting with our game play controller so in the scripts folder and helper scripts I am going to right click and create our game play controller like this and I am going to right click here in the hierarchy and create an empty game object which I'm going to name game play so it's game play controller so game play controller and I'm going to attach the script on the game play controller what do we need well as always we need a couple of variables to start with let me do my thing so let me tag the class give a little bit of space the first thing we are going to make our gameplay controller and instance so public static gameplay controller instance like this and for our instance right here instead of our update I'm going to create create create make instance and inside of this one I'm simply going to say if instance is equal to null meaning it's not pointing anywhere I'm going to say instance is equal to this and voila we are good to go I'm also going to call this function in the awake function as always so this is nothing new we did this probably thousands of times so far before moving forward I am also going to type here using unity engine dot u I because we need to get ourselves some reference to that text displaying the score to our user and we are going to display the score every time our snake eats a fruit so here we also need a public game object for our fruit pick pop now this is not a pickup truck it's simply a fruit and we also have a bomb pick up like this now also we are going to have a private float this is going to be our minimum underscore X which is equal to negative 4.25 F we are also going to have maximum X which is equal to 4.25 F now the minimum is negative and a positive or the maximum is positive minimum Y which is equal to negative two point twenty six F and positive or maximum Y which is equal to two point twenty six F and also our private float Z underscore P OS which is equal to five point eight F what is all this here what did i just type am I going crazy or not well first of all we need to make sure that we are going to spawn our fruits and the bomb between our game objects here let me just find the level here it is so between when I said between our game objects I meant between here and here so as you can see this point let me just take it again so this point here is for point or let me just find it it's negative for point 25 so negative for point 25 which is somewhere around here and this one is positive for point 25 which is somewhere around here how did I find these values well I just did as you just saw I took this fruit or this eye and I positioned it here and saw which position that was then position it here and saw which position that was did the same thing for y-axis and voila this is how I got them now our fruit and bomb in the scene we can remove them because we're going to drag and drop if I suck the gameplay controller we will go in prefabs and pickups and we will drag and drop these from our prefab so fruit pickup will go here and bomb pick up will go right here so moving forward or going back here and z position is same as for all of our game objects you can see snake has Z position five point eight so does every other game object that we want it to be visible so moving forward we also need a private text which is going to be our score text and we need a private int which is our score count now inside of the start function we are going to say so void start and here we are going to see our score text is equal to game object find the game object with the name score get the component text from that game object like this and we are good to go now you need to make sure that the name of that game object is indeed score so in our UI here so UI canvas here it is score so make sure the names match up or otherwise this will not work now we are going to create functions that are going to start spawning our pickups be that a bomb or our fruit so first of all we are going to create an eye enumerator a co-routine as you might assumed and we're going to call it spawn pick ops like this so start spawning pickups and here we're going to say yield return new wait for seconds and we are going to wait from a random dot range between 1 F and 1.5 F so between 1 second in 1.5 of a second we're going to wait and then we will spawn either a fruit or a bomb and we're going to do that using a random value so if random range from zero to ten is greater or equal to two then we are going to instantiate a fruit so here we're going to say instantiate and we're going to pass here our fruit pickup at the position new vector3 like this and for our X we're going to say random that range from minimum X to maximum X this is for the x position for the Y position random that range from maximum or minimum Y to maximum Y and our Z position so zpos comma quaternion identity and we are good to go so let me just put a quaternion identity right below here and voila we are good to go this is for spawning R and actually I'm also gonna put new vector3 right here and put this right here so it works perfectly like a charm so instantiate the fruit pickup on the random range between minimum X and the maximum X minimum Y and the maximum Y and I've explained this it will instantiate our game object so I'm going to take it like this and zoom in on our level just a little bit something like yes and I'm going to take the highlight tool just so that I can demonstrate that this is the minimum X and this is the maximum X value minimum or maximum Y and this is the minimum Y so between random range between minimum and maximum that's this range so between here so between these two values here it will spawn it on the x axis and on the y axis it will spawn them between these values as I explained a moment ago how are we in stanching or how did I get those random values so this is if random range is greater or equal to two because there is only 20% chance that we will spawn a bomb if that happens then here we are going to copy this line of code and here in the else statement instead of fruit pickup we're simply going to say bomb pickup and voila that is that but we need two more functions because here above we are going to create void sock start spawning like this so start spawning and here we're simply going to say start kill routine and we're going to start our spawn pick obscure routine which will start while this coyote but here inside of this Co routine we are also going to call invoke and we are going to invoke this function like this so you see here we are invoking this function this one right here that will call our Co routine and again over and over and over and over until we create here a public void cancel spawning and in our canceled spawning we are simply going to cancel so cancel invoke passing here the start spawning invoke Co routine that well we are going to cancel and when we start our game right here in the start function we are going to spawn it after 0.5 seconds so 0.5 of a second and we will start spawning and spawning and spawning now before we test this out and see if this actually works and it will work we are going to go here and we're going to create a public void increase of increase score and here simply we are going to say score count plus plus in our score text dot text is equal to score column plus score count and this right here will be called inside of our snake so when we go in our playercontroller when we pick up a fruit you see here we are going to call our game play controller so game play controller that instance that in Currys score just like that so simply call it to increase the score and by the way notice here if I go and run our game I'm going to hit the play button so now after 0.5 of a second you see here we have a bomb we have a fruit so here I'm going to pick up a fruit another bomb fruit right here let me try to pick it up so fruit here now one thing that you are probably noticing is that these game objects are staying in the scene they are not being deactivated until we pick them up or until we touch the bomb and eventually we will run out of places where we can walk or move because of the bomb now because of that we need to go in our scripts and here I'm going to create a new folder for our pick up scripts and simply here I'm going to create the activate pickup so c-sharp script deactivate pick up inside of this script we are simply going to create two functions are actually one function called void deactivate which is going to do what game object set active so that set not game play controller game object and this is what I hate so Visual Studio crashed on me for some reason so we need to wait for it to reload again when this happens I simply close Visual Studio this happens also with monodevelop I hate it when this happens it simply freezes will not take into consideration monobehaviour monodevelop whatever but now it works because now you see I can call our game object set active is false and in the start I am going to invoke our deactivate after random range so random that range between 3 seconds and 6 seconds so between 3 seconds and 6 seconds we are going to deactivate our game objects now as we did or did we go here in our gameplay controller and we called here increased score actually I did not even tested that out I didn't did not see we were looking at our fruits but pay attention now so pay attention to the score when you start picking out fruit you see score is not 1 and let us try to pick this up and now score is 2 and again our fruits and our bombs are not being deactivated because silly me select them right here in the prefabs and attach the deactivate pick up script so if I hit the play button now and pay attention to the score again so when we start picking out fruits we will start also counting the score so now score is 1 score is 2 and you see the fruits are disappearing I almost caught that one on did I cut this one I think I did ok this one I missed 4 okay this one I missed 5 and you see it is working so we are counting the score everything is going perfectly well what we need to do now is actually stop a game when we hit one of these so either wall bomb or tail so when we hit one of these we simply whatever what we are going to do is we are going to say time not to tome but the time that time skill is zero we are going to stop our game then you can restart the game by using your code or either press the button restarted because I'm not gonna go we did this already in the Mary's of times restarting the game menus I do this extensively in the ultimate game development Academy so you have all of the resources you need to create your own mechanism for restarting the game so on and so forth because what I'm going to do now is go in unity and I'm going to hit here or double click and create an audio manager script and going back in our scripts folder and helper scripts right click I am going to create a c-sharp script which I'm going to call Audio Manager and attach it on the Audio Manager game object so simply drag and drop it on the Audio Manager and what do we have in our Audio Manager well first of all let me just tag the class gave a little bit of space we are also going to make him an instance so we are going to see here public static audio manager instance instance like this and here we are simply going to create void make instance so if instance is equal to null we're simply going to say instance is equal to this in our awake function we are going to call make instance and that is that this is for our instance and we are also going to have our public audio clip for our pick up sound and for our dead sound and for these two we are simply going to have a public void play why are you correcting me so public void play underscore pick up sound like this and we are going to call here audio source play clip at point the clip is pick up sound at Point transform that position and I'm going to duplicate it paste it here this is going to be play dead sound and simply here inside of the pick up sound we are going to our dead sound and it's simple like that and for our sounds go in the Audio Manager and for our pickup so pickup sound is going to be our eat fruit sound and for our dead sound we are going to have game over sound too so game over sound number two this is what we have for our sounds moving back in our script for the player controller here when we increase the score we are also going to call our audio manager that instance to play the pickup sound and here we're going to call our audio manager that instance to play the dead sound practically we are done with our game if I go back here and I'm going to pump up the volume so that we can hear the sounds so now when we pick up our fruit pay attention let me just try to pick one of these up it's like an eating eating sound you see I had to stop there because I was about to pick up a fruit and again and again and again and we can pick them up and notice when I hit the wall the game is stopped we don't spawn anything anymore you can we did create here a function in our gameplay controller so we created this to cancel our spawning we did not call it because we're using here time scale in our gameplay controller when the time scale is set to zero instantiate will not work but if you want to implement something else you can call cancel invoke you can stop the game with the boolean whatever you choose and that is that basically so in our game play controller we did nothing new we just created here our instance we attached our pickup prefabs I've explained how I got these values this is the text for the score and the score count here we simply have a cool routine that based on the random value will either spawn a fruit or spawn a bomb invoke spawning which will call this function again to start the same cool routine increasing the score here for our audio manager simply playing the pickup sounds and the dead sound by using play clip at point we created also script to deactivate our pickups after three or six or between three and six seconds here for our playercontroller simply we called those functions to play the sounds and to display the score and we explained everything else in the previous videos so again I'm going to hit the play button so we can see this in action one more time so here is one of our pickups I'm going to try to die this time by eating or touching my own tail so I ate this come on come on come on this and this and this this also oh I missed that one yeah I died now you see here you see how he died we should have passed this here the reason for this is because my Collider for my eyeball or this one to do the first the snake's head is too large you can resize it so the radius is set at 1 or actually for this snake this dude right here excuse me so the radius is set at point 11 you can resize it let me just go in the scene and search for the snake you can resize it to something like point 0 9 which is smaller so if I hit the play button so now our collider is smaller we can still pick things off so we can eat here the fruit as you just saw so we can eat the fruit but when we try to pass our own tail then it will work because let me just try to make the snake a little bit bigger to demonstrate that so we ate this we ate this and let me just ate this or eat this as well and let me try it ok now it worked you see now we are not dead by going right near our tail but we will be that if I do this and bam we have died again this is our small 3d snake game this is how simple it is if you had this idea when the Nokia phone came out probably we will be millionaires but hey we did not came with that idea first so just learned something from this tutorial in this game that we have created for your hair from awesome dudes calm I will see you guys in the next video
Info
Channel: freeCodeCamp.org
Views: 80,476
Rating: undefined out of 5
Keywords: unity 3d game tutorial, unity game tutorial, unity tutorial for beginners, create a snake game in unity, 3d snake game unity tutorial, 3d snake game unity, unity snake game, unity3d snake game, how to create a snake game unity, create a 3d snake game unity, create a 2d snake game unity, snake game unity, create a 3d snake game in unity, snake 3d game unity
Id: 8ztq9fQT6Kc
Channel Id: undefined
Length: 109min 7sec (6547 seconds)
Published: Fri Nov 23 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.