Placeholder Image

字幕列表 影片播放

  • What is up, guys?

  • Fire here from awesome dudes that come.

  • Let us now import the assets for our new game that you are going to create.

  • As you can see, I have a brand new project.

  • This is a three D project.

  • So when you create a new project, check the check box or the radio button for three d Name your project.

  • Save it in any location where you want to save it and create it here.

  • I only have the scene's folder with my game placing, 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're gonna go in our assets folder that you can Donald in the link below and select all of these assets that we have bonds, 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 a unity to take mercy upon us and import all off these assets.

  • So let us wait one moment more.

  • None.

  • And and finally they are here.

  • So in this video, I'm going to set up our main or game plea scene.

  • And we're going to do that by going inside of the pre fabs folder.

  • And here we have our level.

  • And here it is.

  • I'm still going to drag and drop it here in the hierarchy panel.

  • And while our 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 dark, but now it's a bit wife again.

  • So I'm gonna go quickly inside off this a lightning tab, and I'm gonna unchecked 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 will probably be floating for you.

  • Don't you try?

  • If I can make it float?

  • No, Apparently not here.

  • Yeah.

  • Finally.

  • So it will.

  • Finally, it will probably not finally be floating for you like this.

  • I docket usually right next year 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 off this lightning tab and you're gonna unchecked this auto Generate.

  • Now, this altar generate notice here when I hover over it says automatically generates lightning data in the scene when any changes are made to the lining system.

  • This means if we take this directional light and if I even change until a bit So if I change the rotation from 50 to 71 it will re bake lightening 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 sun and so forth.

  • That's why I am going to turn it off.

  • Now we are going to duplicate this direction light.

  • But first of all, for dis light, I'm gonna set the rotation to 10 like this and it already looks pretty nice.

  • And the intensity I'm gonna set it at one.

  • We consent in 1.5, for example, but one will do, and I'm going to duplicate it.

  • Some gonna quickly duplicated here and this is directional light one.

  • And also for this one, I'm gonna set the rotation for the Y axis at zero.

  • So the position x zero position, Why is 30 Station zero Rotation X is 10.

  • Why?

  • Zero and Z is zero.

  • This is for our directional light.

  • One for our direction.

  • Like to sit this one right here.

  • I'm going to set the Y rotation at 43.

  • And do we want to set here 0.3 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 used 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 ah whole section with a brand project or Banyu project.

  • So ah, 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 direction of light.

  • I can actually remove this one so we can leave it at one.

  • You can play with intensity if you want to you.

  • But I will leave it at one.

  • And the rotation is perfect.

  • I have turned off the duplicate buying away and it looks nice.

  • It looks pretty nice by going 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 zero.

  • The why access is going to be one.

  • Something like this.

  • And also the far for clipping planes we descended.

  • It's 10 something like this.

  • This what I did for the clipping planes.

  • It will simply if I pump it up the attention to these parts right here.

  • Limoges demonstrate.

  • Which parts do I mean?

  • Come on.

  • Highlight tool where it is.

  • Here it is.

  • You see this from camera?

  • This right here.

  • This right here.

  • Like what the camera is seeing.

  • So pay attention to that.

  • If I take my clipping place and place that far, it means it will see as long as the camera is stretching.

  • You see as long as the cameras 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 in, create our snake, and I am going to right click here and create an empty game object like this.

  • 000 on 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 council 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 000 for our rotation.

  • I'm going to see, hear or said it.

  • Negative 19.

  • So negative 90 0 for the Y, 180 for the Z for our skill 11 and one.

  • And by the way, are snake here.

  • We need to set the Z axis.

  • Let me just 5.8.

  • Something like this for 5.8.

  • 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 0.12 point 12.12.

  • And I'm saying I'm going to set it to point all.

  • But I'm setting the scale.

  • I did not said what I'm going to set.

  • I'm going to set the scale, so select it and the skill is going to be 1.12 1.12 1.12.

  • 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, note and let me just move it right here and this note.

  • I'm going to say it's 0.1 point 1.1.

  • It needs to be smaller than our head.

  • And let me just duplicate it one more time.

  • And I'm gonna put what a note.

  • 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 couldn't move here.

  • We just moved the low.

  • So let me just put this one 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 us Fear 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 a rigid body on it now unchecked.

  • This used gravity because we don't want to use gravity and duplicated one more time.

  • I'm going to rename it to simply tail and put one of these as a child off our sneak.

  • Now Let me just zoom in and for our tail here.

  • I'm going to set it in 00 and position it here something like this.

  • So we're gonna position it to be as a tale 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 tale.

  • And now we can safely remove it so tale can be removed.

  • And now this is our snake.

  • If I go into game police and you see this is our snake.

  • So the tail is here.

  • It has a sphere collider and a rigid body.

  • The note can also have or it needs to have a sphere collider.

  • But we can on check it for this note.

  • So for the 2nd 1 the one after the head, we don't need this fear collector to be checked.

  • And the rigid body we are also going to add it and uncheck used gravity by the way, for the tail.

  • Also, make sure that you check this is trigger and do the same thing for this tale right here.

  • Check it to be a trigger.

  • The tail right here.

  • I meant the one that we drag and drop into the steak pre fabs folder and for the snakes head, which is this dude right here.

  • Now for this one.

  • We're also going to attach a sphere collider.

  • So Sphere collider, make sure it is a trigger.

  • And I am also going to attach a rigid body.

  • And for this rigid body, I'm going to check 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 if 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 0.1.

  • 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 ivory is.

  • So here it ISS Yeah, it is large, actually.

  • Need to re size a little bit more billing here.

  • Moving our Sirica later?

  • Yes, something like this.

  • I need to recite it more.

  • That's a 0.1 point.

  • Well, just so that I can see where it is.

  • You see this green line?

  • This is where our spirit collider is.

  • If I said it at zero, I think this is perfect.

  • Yeah, 00.1 or 0.11 is where we want this fear collected to be.

  • I also I am going to make it a trigger catting.

  • 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, 0.11 is for our radius of the sphere Collider.

  • I'm also gonna click on add component and attach a rigid body 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.

  • This is preparation for our game.

  • We have the snake.

  • We have the level.

  • I'm gonna drag and drop the snake in the snake brief at 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.

  • No, this one I just named Node because it's the second body part of the snake.

  • And we have the teal, 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 old Nokia phones.

  • When you use that snake and you ate more and more, the snake go grows larger and larger.

  • So all of those other objects that we're gonna add the larger and larger orbit 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 collectors on all of them.

  • So the tail and the sirah collider for the tail needs to be checked.

  • It needs to work because we can collide with the tail.

  • You know the snake can kill itself if it touches itself.

  • So this is for the tail for the note.

  • It does not matter because we're not gonna do that for the notes, which is the next part after the head head here, it's also going to have a rigid body.

  • All of these game objects have a rigid body.

  • Except for the head and the node.

  • This Fear 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 top most 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 1st 1 or the 2nd 1 off and probably pump up the intensity on the 1st 1 However you want to do it, it's up to you.

  • So I'm not gonna touch that.

  • I will turn the 2nd 1 on.

  • So both of them are turned on.

  • This is our snake in the middle off 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 script folder inside of this one right click and I'm going to create here folder for help her scripts.

  • Now for our helper script.

  • The first group that we need to create is our script that's going to hold our tax.

  • I'm going to name it Tax on.

  • First of all right, click, create a Sikh sharp script.

  • You are gonna double click in an open it in visual studio Monta develop whichever one you are using.

  • Now, since in our tax class, as you already know or assume, we will add tags and default variables that we are going to use throughout our games, I take the liberty to copy them simply here and now you can pause the video and simply pays them.

  • But before you do that, I am going to explain what they are now here.

  • It's player direction, direction without end.

  • Anyways, the tax here will represent the tax.

  • So we have the walls.

  • So when we collide with them, we will 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 tax for our game object.

  • And here we can see tail with small tea or a I L I wrote it all caps and for our metrics.

  • So these are the metrics that we're going to use to position and move our note inside of our game or to move the snake.

  • We will see that.

  • Do not worry about that.

  • And this is a public into Marais Shin to determine if we are moving left up right down.

  • And this is the Count E g, or it will show us how many movements do we have so want to three and four.

  • So we have four movements now, As you can see in our public in, um, we can say left is equal to zero up is equal to one right is equal to two, 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 012 and three to access indexes in AR Ari.

  • So don't worry about that.

  • You can pause the video now and copy all off these tax metrics and player direction.

  • And now I am assuming that you have copied all of those and I'm going to move forward and go immunity and in script, I'm going to right click in Crete a new C sharp or actual new folder.

  • And this one has been to be our player scripts.

  • And inside of this folder, I'm going to right click in Crete a C sharp squid.

  • 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 Dragon.

  • Drop our player controller just like this, I'm going to double click it and open.

  • And here in visual studio, let me just do my thing as always.

  • And this is going to be the scream that's going to move the player.

  • We're 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 will use a separate script.

  • This script is the one that is going to move the player.

  • And of course, we need here, couple off variables.

  • The 1st 1 is going to be our public player direction, which I'm going to simply call direction.

  • And since it's 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 hiding Inspector, Public float step linked, which is going to be equal to zero point you f.

  • Now this is the movement in Terrible.

  • So we are going to move our game objects or the snake by this movement in Terrible.

  • So 0.2.

  • Now, we don't want to move our snakes every frame that is 60 frames in a second.

  • So we don't want to move in every frame, so we need a movement frequencies.

  • 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 0.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 note prefab or actually our tail prefab.

  • So you can name a tail, you can name it.

  • Note however you wanted, it is private.

  • It will be only accessible here.

  • But we need to make it serialize herbal or serial Izabal 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 will see an empty field to drag and drop this game object for in it is located in the pre fabs and snake Select Here the snake and dragon 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 wanted 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're going to use a private list off 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 off rigid bodies, which I'm going to call notes.

  • And this is going to be every single separate node inside of the snakes body that is snakes, heads and sneaks tails so on and so forth.

  • We also need a private vector three.

  • And for this one fruit?

  • No, actually, don't need this one.

  • So we're not going to use this one.

  • But we are going to use a private, rigid body for main body.

  • Our private rigid body for the head body and a private transformed T R for our own transform sortie are not transform.

  • We also need a private load 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 nude 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 note or not?

  • Now, instead of me explaining all of these variables one by one, what they are, I will demonstrate that so in awakes I'm going to change start to awake in the away.

  • The first thing a turban to do is we're 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 Our transform is equal to t R.

  • Next thing, our main body is going to be equal to get component and get the rigid body 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're getting its rigid body by using, get component.

  • The next thing that we need to do is we need to call here, innit?

  • Cynic and notes.

  • Now, this is a function it was still did not create.

  • And right below that function, we're also going to call, innit player, And this is also function it was still did not create.

  • Now let us go and create that right below the update.

  • We're going to create a void in it.

  • Snake and nodes.

  • Now, in this one, we are going to initialize the notes.

  • That means every single separate part off the snake.

  • As you can see, if I go inside of the scene view, you see, we need to get all of these notes, so we need to get the head of the snake.

  • So this one, we also need to get this note the middle one and the tail one.

  • So we need to get all three off these.

  • So here in our unit sneak knows we're going to see nodes, which is our array of rigid bodies or a list you see here of Origen bodies and we are going to make it a new list off rigid bodies and below here were going to say nodes that add to add to that list.

  • And we're going to say T r, which is our transform Get child, and we're going to get the child That's at zero index that get component.

  • So it's get component rigid body component just like this closed parentheses and we're good to go.

  • Copy base, copy paste.

  • And we're going to say child one and the child to And what is going on here now, before I explain, the last thing that we need to do is our had body is equal to notes and element.

  • That's at index number zero.

  • What is going on here?

  • You see, here we are using t R, which is our transform.

  • You see, here are TR is the transform Get child and we're getting the child.

  • What does this mean?

  • You see our transform the top most transformed from our snake.

  • It has Children.

  • So we have the note or the head, the note in the tail and we know that or the Transform knows that we have Children assigned to it so we can use our access those Children's by using in Texas.

  • So you can say get child zero, which means it will get the first child or the child.

  • That's an element zero, which is in our Kays head.

  • Get child one.

  • In our case, it's the node get child to.

  • In our case, it is the tale.

  • And here noticed the head body.

  • It is the region body off the head son ache.

  • So the rigid body of the head snake and we know the head is set at index number zero, which I explain a moment ago Now, in order to initialize our player so right here below, we're going to create, innit 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 switch on, we're going to use direction and we are going to use cases in our search in 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.

  • Phased it.

  • So we're going to have direction left up and don't and here I'm going to simply say down.

  • So based on the direction, we're going to position all off our notes or shift them to that position.

  • So if we need to, or if our starting position is the right position, we're 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 nose in the element that index zero minus new Vector three and here for our ex were going to say Metrics.

  • That note and zero F zero f and four are for our nodes.

  • Number two or element.

  • That's a number two.

  • And by the way, here we need to say no.

  • Zero that position.

  • So the position of our No.

  • Zero and by the way, inside of our knows, we're adding Bridget bodies.

  • So when we say position, we are getting the position of the rigid body you see here when I have are over, it says the position off the rigid body.

  • So we're getting the position of the rigid body, and here we're going to say nose and the element that's an index to now we're going to say that position is equal to We're going to use notes and element that zero that position minus new Vector three and for our metrics.

  • So we're going to see metrics that node, and we're going to multiply that by two and zero F zero f What is going on?

  • Well, when we start our game to the right, we need to shift the positions off our game objects to the right side.

  • And in order to do that, we need to get the note zero, which is the head, What we need to do essentially, if we're starting to the right side, that means when we start our game, that means when we start, our game will start moving to decide what we need to do.

  • We need to shift the position of this node over here and this note over here because then we are going to move to the right or actually do the left side.

  • Excuse me are moving to the left, but assumed that I said left.

  • So if we are moving to the left side, we shift their positions to the right side by using Plus, 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 side, simply going to copy these lines of code and pays them here instead of typing here minus were going to say, Plus, 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 our snake, that means we're going to decide.

  • 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 note 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 element one and use the Element zero node, which is the head and adds to its position the metrics node, which is the displacement that we saw here.

  • This is the displacement off our notes.

  • So This is how far away they are from each other and do the same thing For our second note.

  • 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 note here, this code right here, it's going to move this note over here.

  • And this code right here is going to move this tale node, over here.

  • This is what we are doing.

  • Essentially now we're going to do or everything that we explained here for the left side applies to the right side except well, the right side is opposite to the left for are up side.

  • We're going to do the same thing except for the y axis.

  • So for the Y side, we're going to say minus but our metrics node is going to be on the y.

  • So zero for the zero effort X and we are going to move that one on the why and same thing here.

  • So we're just going to take this and zero f or the X and for the y metrics node multiplied by two.

  • Why multiplied by two because our note at second position is our tail note.

  • So we need to multiply that by two because our head is here.

  • Our node is here.

  • We need to multiply this by two.

  • So one had his one note is too.

  • And multiply that by two.

  • Moving that right over here.

  • This is why we are multiplying with two for our down position.

  • Simply copy this code from the opposition pays it here and for the down we're going to say plus like this and Walla, we are good to go now.

  • Of course, we need to create one more function.

  • So right here above our innit player, we're going to create a void set direction a random and here we're going to say in direction random or di d I are random is going to be equal to a random that range from zero up to and we need to say here in't 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 count, if you remember, has a value of four.

  • But this is an Inu Maru ation, and we can cast this in numerator in to an integer.

  • I've talked this already.

  • Once in one of my struggles can remember which one, but anyways, we can cast it into an integer because, as I said, the values for 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 the I R around them now.

  • Same way as we can cast these Denham's to an integer.

  • We can use a new Marais shins to cast integers to eat him so we can see here player direction now based here on the random value, let's say Iran, the valley's equal to one So this d I r int or the I R ran the value will have a value of one when we convert that into our into our enumeration player direction and we go back here.

  • The valley Juan is up.

  • It will be the same if the values to so value to is right.

  • If the value is, let's say three, 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, or uncommon.

  • Almost that, um, position we can uncommon these functions now.

  • Now pay attention.

  • If we go back here, I am going to clear the council and we go here in our game.

  • Pay attention.

  • How the snake is positioned sake.

  • It's been its position.

  • Wit right direction in mind.

  • So it looks like it's going to start moving this direction.

  • Noticed 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 in his position again to the right side, stop and rewrite the game, It will be positioned now, you see, Like it is going upwards.

  • So this is how we are moving these notes relative to the head.

  • No, this is how are we positioning these notes by using these lines of code right here.

  • Now, we're still still not done here with the awake function we need to create our delta position or which to go Our position in which where we are going, so are Delta.

  • Position is going to be equal to new list of vector threes like this.

  • Open closed.

  • These currently brackets.

  • And here I'm going to pass a new vector three and here to see comma.

  • Now for the ex Im going to say negative step length comma zero f Ford, the Y.

  • I'm going to copy this and basin below.

  • In here, I'm going to say zero f first.

  • So zero f and here I am going to say step linked comma here pays this one right here.

  • Now, here we are going to have a positive step length, and I'm going to copy this and face it here.

  • Now, another comma here, remove this comma.

  • And here we're going to have the negative linked now, right here.

  • I am going to add comments, and here we need to end this.

  • David Witt are semi colons.

  • So good to add comments here like this.

  • So this one is going to be our negative t x or going to the left side Here, 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 direction.

  • So you see here this step linked this one right here He is actually the value, or we're going to move by that value.

  • So this is the time interval that value.

  • When we move, we will move by 0.2 units.

  • This is what I'm trying to say.

  • So when we have this right here, this means negative D why we're going to the left side or negative XO.

  • Disi Here we have the positive.

  • Why meaning up here we have the positive X meaning, right?

  • And here we have the negative wide going down and you see here Negative.

  • Why?

  • Positive X positive.

  • Why?

  • Negative X.

  • So this position right here means we're 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 a void check, movement, frequency.

  • And for this function, we're going to stay here, our counter.

  • So counter plus equals time that doubt the time we know the time dot 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 stay here.

  • If our counter is greater or equal to movement frequency, What we are going to do, we're going to see our counter is equal to zero to start counting from zero again.

  • And here we're 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 stay here, void, move or create a function that is going to allow us to move and not on.

  • Animator Move.

  • No, simply avoid move.

  • And inside of this function, we are going to create here affected three D position.

  • This is the Delta position we're going to use here.

  • Delta position.

  • So it's actually Delta position, and we are going to use that by using here and int, and we're going to pass here the direction So the doubt the position or D position is our delta position.

  • We're casting the integer or direction to an integer to get it as an index.

  • So depending on the index, if we're 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 zero in the dump them in the Delta position.

  • And if we go here in the Delta position element, that's in the zero 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're going to create here a vector three parent Poso Parent position, head, body, that position.

  • And right below here, we're going to create the Vector three previous position or the previous position and below.

  • Here we are going to say Main body, that position is equal Thio main body position plus the position right below were going to say head body That position is equal to head body that position plus the position again.

  • Now we are also going to move all the snake note So we're going to stay for int I, which is equal to one as long as one or I is less than notes That count are I is going to be plus plus and we are simply going to see here.

  • Previous position is equal to notes element.

  • That's an index I that position storing the previous position notes an element.

  • That's an index I That position is equal to parents position and parents 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 thio Check if we need you, not treat era.

  • It's check if we need to create a new note because we ate a fruit.

  • We will do that later on.

  • And for that, we're simply going to say if create note a tail, then we're going to write the code to do that for us.

  • Now, practically.

  • We're 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.

  • Come on, go down.

  • This is check moving frequency in the update.

  • We're going to calculate our counter by adding to its 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 avoid fixed update.

  • And let me just remove this private because I like it to be void simply and in are fixed.

  • Update.

  • We're going to check if move, So if we can move, then move is equal to falls 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 that all the time.

  • If the counter is greater or equal to the movement frequency, then we're going to say counter is zero so that we can count from the beginning from zero and move is equal to truth 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 increments.

  • It is gone.

  • We cannot control the snake noticed.

  • Now if I keep the play button now Snake is going down where it's just a moment ago.

  • First time when we tested out, Snake was moving upwards.

  • I'm going to hit the play button again.

  • Hopefully it'll move.

  • Left a right to know again it is 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 contested 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, we'll go to the right side note.

  • Anyways, let's go.

  • Break it down.

  • Line by line.

  • So these here are now getting ah, clear picture or we are understanding for what we're using.

  • This direction is going to determine in which direction are snake is going to go.

  • Step length is the frequency or the unit's measurement unit.

  • By how much units we are going to move every note.

  • That is 0.2 units.

  • Moving frequency is the how many times we can move in a second.

  • So every 0.1 of a second we can move counter is used to test that out.

  • So we're 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 truth and moving forward.

  • This is our tail prefab, which 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?

  • Isn't up right or down.

  • And we're using that by while using the step length and these values right here.

  • You see here the negative step length is negative.

  • X meaning we're going to the left side.

  • Positive.

  • Why?

  • That means up and positive step linked.

  • That means going to the right side.

  • Negative.

  • Why means we're going down now.

  • Here are the notes that we're going to get when we initialize the snake.

  • Basically, these are the rigid bodies off every part off the snake.

  • So the head the node in every part of the tale so we can have 20 taels.

  • When we eat 20 fruits, every a single one of those tales is going to be added to this node array.

  • You see here are using the transform to get the child's.

  • So we are getting the child and the 01 and two child zero index or at zero index, is our head.

  • So we're doing or assigning that right here and moving back.

  • Here we have the main body head and the 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, RTR is the transform.

  • And here we are, initializing the snake knows by getting the rigid bodies from every child.

  • Next, we are initializing the player.

  • That means we are setting the direction, the initial direction off, the movement off the snake.

  • We're doing that with this function, which is this one right here.

  • So using a random range from zero and direction dot counts a player direction.

  • Count your casting that to an integer, because the value here of the count is equal to four, as you can see here, account is equal to four.

  • So when we cast it to an integer, it will display that value.

  • So it'll display for 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 the values 33 is down.

  • If the value is, for example to then to is right and you get the picture.

  • So we're 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 this place these notes 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 hear the play button you see here.

  • The initial direction was left side, which means both of these notes this one here and this one here shifted to the right side by using these lines of code.

  • So using the head position off 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 step length.

  • This is same as step length.

  • And for our second note, we need to multiply that by two because we need to also place it after our middle note.

  • Because we have three notes.

  • Same applies for right up and down now moving forward.

  • Here we have our check movement frequency.

  • We have explained this.

  • So we're adding to the counter doubt the time when the counter is greater or equal to the movement.

  • Frequency reset counter move is equal to truth.

  • 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 off the direction is to, that means we are moving to the right side.

  • Next we're getting the parents position, which is our head body position, and we're storing the previous position now.

  • First we need to start moving the main body, which is the main body off our snake, the top parent holding all notes.

  • And we're 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 had the body's position is equal to have bodies current position, plus the direction where we are going now.

  • Depending on if we're 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 zero for device.

  • It will move it to that direction.

  • Now we have to move all of our notes.

  • We moved our head already.

  • That's why in eye is equal to one.

  • Because here, if you paid attention, the element or the first note is that Note that element zero, which is our head note, 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 notes.

  • That's why I starts at one and not zero, and we're going to move how many nodes we have?

  • If we will have 100 notes, then we're going to use count, which will be 100.

  • So we will move 100 notes now the previous position.

  • And we're getting that by using the note at the current I index in its current position.

  • This is the previous position.

  • The new position for that note 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 dead 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.

  • Bly's 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 incremental values.

  • Or if we go hear the step link, that is, This is how many units it will use to move 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 off the snake.

  • So in order to fix that inside of our earlier scripts folder, we

What is up, guys?

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

Unity遊戲開發完整教程-3D蛇遊戲 (Unity Game Dev Full Tutorial - 3D Snake Game)

  • 0 1
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字