1 00:00:04,010 --> 00:00:04,730 ‫Welcome back. 2 00:00:04,760 --> 00:00:10,970 ‫In this video, you will learn the basics of moving a object within our game, and that one will be 3 00:00:10,970 --> 00:00:12,530 ‫moved by you, the player. 4 00:00:12,530 --> 00:00:17,030 ‫So you will see how to use user input and how to use basic physics. 5 00:00:17,030 --> 00:00:20,820 ‫But of course, we'll have a closer look at physics within the next videos. 6 00:00:21,020 --> 00:00:22,970 ‫So let's go ahead in our game. 7 00:00:22,970 --> 00:00:24,710 ‫Let's delete our cube. 8 00:00:25,610 --> 00:00:27,040 ‫So let's delete that. 9 00:00:27,050 --> 00:00:34,190 ‫What I want to use is I want to use a 3D object, which is a sphere, and I want to have a 3D object 10 00:00:34,190 --> 00:00:35,240 ‫which is a plane. 11 00:00:35,240 --> 00:00:42,530 ‫So I do right click within my hierarchy view and then I go to 3D object and use plane. 12 00:00:43,040 --> 00:00:49,340 ‫As you can see, my plane has the transform position of 000 for the X, Y and z value. 13 00:00:49,340 --> 00:00:55,040 ‫This transform component, as I told you in one of the earlier videos, is always there for each and 14 00:00:55,040 --> 00:00:59,810 ‫every single object that you have within your game and you can change it around here. 15 00:00:59,810 --> 00:01:03,620 ‫For example, you could change the X position to move it back and forth. 16 00:01:04,460 --> 00:01:07,880 ‫You could change the Y position to move it up and down. 17 00:01:07,880 --> 00:01:10,490 ‫As you can see, that could be very relevant for you. 18 00:01:11,090 --> 00:01:16,820 ‫And as you also can see, we have the sun here which has a specific direction. 19 00:01:16,820 --> 00:01:20,090 ‫As you can see, the sunlight comes from the top left to the bottom right. 20 00:01:20,090 --> 00:01:22,520 ‫And that's why you get this shadow here. 21 00:01:22,520 --> 00:01:24,710 ‫So all of that is already taken care of. 22 00:01:24,710 --> 00:01:26,090 ‫You don't need to program all of that. 23 00:01:26,090 --> 00:01:28,010 ‫It's already there, which is great. 24 00:01:28,040 --> 00:01:33,170 ‫Our play now is at a position that I don't like to it to be, so I can always reset it. 25 00:01:33,170 --> 00:01:39,650 ‫So you can click on to that little button here on the top right side and choose reset. 26 00:01:40,190 --> 00:01:47,240 ‫By that you will reset it to the standard values of 000 for the position and for the rotation, and 27 00:01:47,240 --> 00:01:50,030 ‫at the same time, the scale will go to one, one, one. 28 00:01:50,780 --> 00:01:51,200 ‫All right. 29 00:01:51,200 --> 00:01:57,920 ‫So now let's move our sphere, however, so that it's not inside of our ground. 30 00:01:57,950 --> 00:02:01,790 ‫We can easily change that by moving the Y value up. 31 00:02:01,790 --> 00:02:08,390 ‫And you can either do that by manually entering a value here or you could drag from the y. 32 00:02:08,630 --> 00:02:15,320 ‫So hover over y with your mouse and then you can move it to the left and right and it will change it. 33 00:02:15,320 --> 00:02:23,570 ‫So now you could try to find the right position for your ball to be at and that one looks good. 34 00:02:23,570 --> 00:02:30,890 ‫So 0.37 But what you also can do, you can choose the transform tools here at the top left, for example, 35 00:02:30,890 --> 00:02:36,410 ‫the cross in order to move your object right in here. 36 00:02:38,010 --> 00:02:38,610 ‫All right. 37 00:02:38,610 --> 00:02:44,640 ‫So now what I don't like is the fact that all of them are white or all of my two objects are white, 38 00:02:44,640 --> 00:02:46,680 ‫which means our sphere and our plane. 39 00:02:46,680 --> 00:02:49,830 ‫And we can easily change that by creating an. 40 00:02:51,480 --> 00:02:52,700 ‫A new material. 41 00:02:52,700 --> 00:02:55,100 ‫So let's create a material and call that. 42 00:02:56,980 --> 00:03:01,090 ‫Ground and now we give the ground a different color. 43 00:03:01,090 --> 00:03:08,290 ‫So let's choose, for example, a green and now we can drag that material into our game. 44 00:03:08,290 --> 00:03:16,000 ‫And as you can see, our plane now is completely green and we can duplicate our ground, for example. 45 00:03:16,000 --> 00:03:19,720 ‫So let's do that and we can call it player. 46 00:03:19,720 --> 00:03:23,380 ‫And there we can, for example, use a red color. 47 00:03:24,950 --> 00:03:32,090 ‫So let's take this reddish color here and drag that into our player. 48 00:03:32,450 --> 00:03:39,140 ‫As you can see, if I drag it or hover it over while pressing the left button on the mouse, I can hover 49 00:03:39,140 --> 00:03:43,820 ‫it over my different game objects and for example, over my plane. 50 00:03:43,820 --> 00:03:48,240 ‫And then you can see the plane turns red or over my player, and then my player turns red. 51 00:03:48,260 --> 00:03:53,210 ‫If that doesn't work for you, you can also do that over here on the left hand side, on your hierarchy 52 00:03:53,210 --> 00:03:53,750 ‫view. 53 00:03:53,780 --> 00:03:58,670 ‫You could drag that onto your sphere and then the sphere will turn red as well. 54 00:03:58,970 --> 00:04:01,010 ‫Now, let's have a quick look at our sphere. 55 00:04:01,040 --> 00:04:08,540 ‫As you can see now, it has gotten a red color here, which means the component has gotten a color of 56 00:04:08,540 --> 00:04:10,520 ‫red, which is our red material. 57 00:04:11,210 --> 00:04:12,740 ‫Now we have our playing field. 58 00:04:12,740 --> 00:04:18,080 ‫We have our player or our sphere, and I'm going to call that player. 59 00:04:19,430 --> 00:04:24,590 ‫So that one will be my player and the plane will be my on the ground. 60 00:04:26,750 --> 00:04:32,210 ‫Now, in order to make our player move, we need to create a script for that. 61 00:04:32,600 --> 00:04:34,760 ‫And we have created a script already. 62 00:04:34,790 --> 00:04:38,420 ‫I call it test, but I don't like that script, so I'm going to delete it. 63 00:04:38,660 --> 00:04:42,860 ‫And as you can see, if you delete a script, this action cannot be undone. 64 00:04:42,860 --> 00:04:46,040 ‫So be very careful when deleting assets from your project. 65 00:04:46,340 --> 00:04:48,080 ‫So I'm going to delete that one now. 66 00:04:48,860 --> 00:04:56,840 ‫All right, so now let's create a new script, which I'm going to call player movement. 67 00:04:58,340 --> 00:04:59,660 ‫Now let's open that up. 68 00:04:59,900 --> 00:05:06,560 ‫And usually Visual Studio should come up or whichever editor you have set as your standard editor. 69 00:05:07,460 --> 00:05:13,010 ‫So next, what we'll need to do is to create two variables and use methods from here. 70 00:05:13,700 --> 00:05:16,910 ‫It's no problem if you don't understand everything that's going on here. 71 00:05:16,910 --> 00:05:19,640 ‫We will cover that in one of the later videos. 72 00:05:19,640 --> 00:05:24,890 ‫In the next chapter, I will go into detail what this using means, what this public means, this class 73 00:05:24,890 --> 00:05:26,050 ‫means, and so on. 74 00:05:26,060 --> 00:05:31,760 ‫What you just need to do now is follow along because we want to make our character move. 75 00:05:31,760 --> 00:05:36,110 ‫And if we want to make our character move, we will need a speed. 76 00:05:36,110 --> 00:05:42,160 ‫So let's go ahead and create a public float speed with a speed of five, for example. 77 00:05:42,170 --> 00:05:43,760 ‫So as I said, no worries. 78 00:05:43,760 --> 00:05:49,040 ‫And if you don't understand what's going on precisely, then we need another variable which is of type 79 00:05:49,040 --> 00:05:56,240 ‫rigid body and I'm going to call it my rigid body and within the start method, I'm going to initialize 80 00:05:56,240 --> 00:05:56,570 ‫it. 81 00:05:57,380 --> 00:06:03,980 ‫What that means is that I will set a value to my rigid body because otherwise it's empty and it doesn't 82 00:06:03,980 --> 00:06:04,910 ‫know what to do. 83 00:06:04,910 --> 00:06:09,890 ‫And within the start method is a perfect position to do that because that one is called right at the 84 00:06:09,890 --> 00:06:18,620 ‫beginning when our game object, which is our player, will be created within the game and here I choose 85 00:06:18,620 --> 00:06:20,960 ‫would get component rigid body. 86 00:06:21,830 --> 00:06:22,130 ‫All right. 87 00:06:22,130 --> 00:06:23,240 ‫So as I said, no worries. 88 00:06:23,240 --> 00:06:25,550 ‫If you don't understand what's going on, just follow along. 89 00:06:25,850 --> 00:06:31,070 ‫Then within the update method, I want to move my character vertically. 90 00:06:32,450 --> 00:06:41,840 ‫So I'm going to get the input of the user, get access, and I take the vertical axis and I do the same 91 00:06:41,840 --> 00:06:43,970 ‫thing for the horizontal axis. 92 00:06:43,970 --> 00:06:47,420 ‫So float move horizontal 93 00:06:49,760 --> 00:06:56,990 ‫is input dot get axis and this one is Hori zone tool. 94 00:06:59,780 --> 00:07:09,050 ‫And finally, I want to move my character vector three movement new vector three. 95 00:07:11,130 --> 00:07:19,080 ‫Move horizontal, then don't move it upwards and move vertical. 96 00:07:21,240 --> 00:07:28,320 ‫So what that means is in the update method, which is called once per frame, as it said here, my player 97 00:07:28,320 --> 00:07:30,570 ‫will check every time. 98 00:07:30,570 --> 00:07:31,920 ‫Is there a user input? 99 00:07:31,920 --> 00:07:33,990 ‫So does the player press. 100 00:07:33,990 --> 00:07:41,490 ‫For example, the vertical keys, which means does he press forward and backward or W and SW? 101 00:07:42,060 --> 00:07:45,060 ‫That will give us a vertical value. 102 00:07:45,060 --> 00:07:50,520 ‫So it will tell us, okay, he moves forward, that's one or he moves backward, that's minus one. 103 00:07:51,030 --> 00:07:53,580 ‫And the same thing is for the horizontal one. 104 00:07:53,580 --> 00:07:57,210 ‫So is he moving left or right that will be saved in here? 105 00:07:57,390 --> 00:08:02,130 ‫And now we create a 3D vector, which means it's a direction. 106 00:08:02,130 --> 00:08:09,630 ‫We say, okay, there is a direction which is a forward direction, which is a upward direction and 107 00:08:09,630 --> 00:08:12,330 ‫which is a sideward direction. 108 00:08:14,010 --> 00:08:15,980 ‫So this one is x move. 109 00:08:15,990 --> 00:08:19,710 ‫Horizontal is x, this one is our Y and this one is our Z. 110 00:08:19,710 --> 00:08:24,630 ‫And horizontal and vertical is from a specific perspective, we will see what that means. 111 00:08:24,930 --> 00:08:28,350 ‫And now finally, we can move our rigid body. 112 00:08:28,350 --> 00:08:32,970 ‫I will show you in a second what the rigid body is, and we will cover the rigid body in more detail 113 00:08:32,970 --> 00:08:34,440 ‫in one of the next videos. 114 00:08:34,440 --> 00:08:40,740 ‫So what we do now is we take the movement, which is the direction, and we multiply speed. 115 00:08:40,830 --> 00:08:47,250 ‫Whatever speed we have entered here, we will now move our player whenever we press buttons. 116 00:08:47,370 --> 00:08:52,230 ‫So let's just save that script and let's add that script to our player. 117 00:08:52,230 --> 00:08:58,050 ‫And there are multiple ways you can drag the script right from here, from your assets to the player 118 00:08:58,530 --> 00:09:02,730 ‫and the player movement script will be added in the inspector. 119 00:09:03,000 --> 00:09:09,450 ‫Otherwise you can go to add component and then search for scripts, and then you can find the player 120 00:09:09,450 --> 00:09:10,020 ‫movement. 121 00:09:10,020 --> 00:09:11,970 ‫You can press on it and you will add it. 122 00:09:11,970 --> 00:09:17,040 ‫If you accidentally have the same script twice, you can get rid of it by pressing on this top right 123 00:09:17,040 --> 00:09:19,770 ‫button and remove the component. 124 00:09:20,490 --> 00:09:27,450 ‫Now, before we run the game, let's position our player a little bit upwards and so that it's just 125 00:09:27,450 --> 00:09:29,250 ‫on top of the plane. 126 00:09:29,790 --> 00:09:35,370 ‫As you can see, it's it's just a well, we can even put it a bit higher. 127 00:09:35,490 --> 00:09:36,180 ‫All right. 128 00:09:37,320 --> 00:09:44,970 ‫And then next, we need to give our sphere a rigid body, and that one you can find under physics, 129 00:09:45,330 --> 00:09:46,440 ‫rigid body. 130 00:09:47,070 --> 00:09:54,480 ‫What that does is it gives a physical body to our object, which is in this case, our player. 131 00:09:54,480 --> 00:09:56,970 ‫And it has, for example, a mass. 132 00:09:56,970 --> 00:09:59,940 ‫It has a drag, an angular drag. 133 00:09:59,970 --> 00:10:02,820 ‫It can tell us whether it uses gravity or not. 134 00:10:03,210 --> 00:10:08,880 ‫And there are some further information we will look into Rigid Body in one of the next videos in detail. 135 00:10:08,880 --> 00:10:10,980 ‫But for now, let's just run the code. 136 00:10:13,370 --> 00:10:19,880 ‫And you can see if I move left, right and forward, I can move my ball. 137 00:10:20,480 --> 00:10:24,460 ‫Now, I don't like the angle of our camera, so we can easily change that. 138 00:10:24,470 --> 00:10:28,280 ‫Let's go to our main camera and now let's move it a little bit around. 139 00:10:28,280 --> 00:10:35,720 ‫As you can see here, we can move it towards this little this angle here, and we can change the rotation, 140 00:10:35,720 --> 00:10:39,710 ‫for example, so we could change the rotation. 141 00:10:43,310 --> 00:10:48,440 ‫And now we would have a different, very different look to our game from our. 142 00:10:49,750 --> 00:10:50,460 ‫Camera. 143 00:10:51,700 --> 00:10:53,620 ‫Now, that makes it a bit more difficult. 144 00:10:53,620 --> 00:10:56,710 ‫So let's set the rotation back to zero. 145 00:10:57,640 --> 00:11:00,700 ‫Let's move our camera a little bit to the side. 146 00:11:00,700 --> 00:11:02,320 ‫So here you have the camera preview. 147 00:11:02,350 --> 00:11:05,200 ‫Now let's rotate it over here. 148 00:11:05,200 --> 00:11:14,720 ‫And well, maybe a good position should be minus one five Z position can stay where it is and the rotation. 149 00:11:14,740 --> 00:11:16,690 ‫Now let's go with 45 degrees. 150 00:11:16,690 --> 00:11:19,030 ‫So now we have a different angle at our game. 151 00:11:19,720 --> 00:11:28,000 ‫And as you can see, there is my ball and whenever I'm off my playing field, it will fall off. 152 00:11:29,070 --> 00:11:31,980 ‫So as soon as it's off the underground, it will drop. 153 00:11:31,980 --> 00:11:34,680 ‫And that has to do with it having a gravity. 154 00:11:35,160 --> 00:11:37,410 ‫So our player has a gravity. 155 00:11:38,460 --> 00:11:40,860 ‫And that has to do with use gravity here. 156 00:11:40,860 --> 00:11:46,680 ‫So if I deactivate that, let's go ahead and look at it and let's see. 157 00:11:46,980 --> 00:11:52,050 ‫As you can see, our ball doesn't need the underground anymore. 158 00:11:52,050 --> 00:11:54,000 ‫And now it's somewhere we can't see it. 159 00:11:54,210 --> 00:11:55,350 ‫Actually, there it's back. 160 00:11:55,770 --> 00:12:00,990 ‫What you can see is if I push to the left and now I can't see it anymore. 161 00:12:01,020 --> 00:12:01,700 ‫There it is. 162 00:12:01,710 --> 00:12:03,090 ‫And I push back to the right. 163 00:12:03,090 --> 00:12:06,870 ‫It needs a little while until it goes back to the direction that I want to go. 164 00:12:06,870 --> 00:12:14,370 ‫And that has to do with it being a physical body or having a physical body, which makes it pretty darn 165 00:12:14,370 --> 00:12:15,000 ‫heavy. 166 00:12:15,000 --> 00:12:20,430 ‫So it's not that easy to change the direction with just a little force that we are applying to it. 167 00:12:21,330 --> 00:12:21,750 ‫All right. 168 00:12:21,750 --> 00:12:23,880 ‫So that's the basics of player movement. 169 00:12:23,880 --> 00:12:28,760 ‫We will have a look at more detailed pieces of the game later on. 170 00:12:28,770 --> 00:12:34,830 ‫Now, let's first have a look at the physics, because what we will do is we will have a look at physics 171 00:12:34,830 --> 00:12:36,120 ‫in the next videos. 172 00:12:36,120 --> 00:12:39,420 ‫We will have a look at scripting and the following chapter. 173 00:12:39,420 --> 00:12:44,220 ‫And afterwards you will have a much better feeling for what's going on and you will be able to create 174 00:12:44,220 --> 00:12:45,630 ‫your first little games. 175 00:12:45,780 --> 00:12:47,010 ‫So for now. 176 00:12:48,230 --> 00:12:51,410 ‫The player movement is a script that we have created. 177 00:12:51,410 --> 00:12:53,460 ‫We have written some lines of code. 178 00:12:53,480 --> 00:12:55,040 ‫You might not understand all of it. 179 00:12:55,040 --> 00:12:55,760 ‫No worries. 180 00:12:55,760 --> 00:12:56,760 ‫We'll get there. 181 00:12:56,780 --> 00:13:02,690 ‫The only thing that you need to understand is that you need to have a script in order to change the 182 00:13:02,690 --> 00:13:10,640 ‫behaviour of your game objects, which is our player, for example, or our underground and so forth.