1 00:00:04,310 --> 00:00:05,060 ‫Welcome back. 2 00:00:05,090 --> 00:00:07,910 ‫In this video, we are going to make our character move. 3 00:00:08,030 --> 00:00:13,160 ‫And it's going to be a very simple movement going towards the left and towards the right, left and 4 00:00:13,160 --> 00:00:13,490 ‫right. 5 00:00:13,490 --> 00:00:17,690 ‫So whenever we click or press a button, it should go and left, left and right. 6 00:00:17,840 --> 00:00:19,910 ‫So let's go ahead and build that. 7 00:00:19,910 --> 00:00:25,670 ‫So we start off by going to our character here. 8 00:00:25,670 --> 00:00:29,090 ‫Let's click on the character and let's add a component to it. 9 00:00:29,570 --> 00:00:36,710 ‫And I'm going to use scripts, I'm going to create a new script and I'm going to call that one char 10 00:00:36,710 --> 00:00:42,200 ‫controller, which is character controller, and let's open that one up. 11 00:00:43,940 --> 00:00:51,950 ‫So what I'm going to use is the awake method instead of the start method, and I will need the rigid 12 00:00:51,950 --> 00:00:58,730 ‫body because I want to access the movement components, pretty much so rigid body, I'm going to call 13 00:00:58,730 --> 00:01:02,090 ‫it RB and then I want to have. 14 00:01:02,860 --> 00:01:06,820 ‫A Boolean, which tells me whether he's walking right or left. 15 00:01:06,850 --> 00:01:11,920 ‫So if we have a look at the player again, currently he's walking, right? 16 00:01:11,920 --> 00:01:14,360 ‫And then if he goes there, he's walking left. 17 00:01:14,380 --> 00:01:20,990 ‫And what that means walking right means rotation of 45 and walking left means rotation of -45. 18 00:01:21,010 --> 00:01:23,560 ‫So these are the two values that are relevant for me. 19 00:01:24,160 --> 00:01:24,430 ‫All right. 20 00:01:24,430 --> 00:01:25,990 ‫So let's go back to the script. 21 00:01:26,710 --> 00:01:31,600 ‫Well, the Boolean is walking, right? 22 00:01:31,960 --> 00:01:34,600 ‫And I'm going to set that up as being true. 23 00:01:35,440 --> 00:01:40,480 ‫Then within the awake method, I'm just going to initialize my rigid body. 24 00:01:40,480 --> 00:01:48,850 ‫So I'm going to get the component rigid body from our player because the controller is assigned to our 25 00:01:48,850 --> 00:01:52,990 ‫player or our character, and that's why this is going to work. 26 00:01:53,050 --> 00:01:57,770 ‫Then I'm going to use the fixed update in order to move the player forward. 27 00:01:57,790 --> 00:01:59,650 ‫So let's do that. 28 00:01:59,740 --> 00:02:03,850 ‫Ah, let's create the fixed update method. 29 00:02:04,690 --> 00:02:15,340 ‫And in here I just say set the transform position of the player as the transform dot position plus transform 30 00:02:15,340 --> 00:02:23,680 ‫that forward times two times time delta time, dot delta time. 31 00:02:23,770 --> 00:02:31,810 ‫So that does it moves the player by a certain amount forward depending on how much time has passed and 32 00:02:31,810 --> 00:02:34,150 ‫in fixed update that should always be the same time. 33 00:02:34,420 --> 00:02:39,950 ‫So we are moving our player step by step and it's going to be moved forward. 34 00:02:39,970 --> 00:02:41,400 ‫What does forward mean? 35 00:02:41,410 --> 00:02:47,110 ‫Well, forward means based on the direction that he's currently looking at, he's going to go forward. 36 00:02:47,110 --> 00:02:54,520 ‫So if he is, let's say if he's at 45 degrees, then he's just going to go forward here to the right. 37 00:02:54,520 --> 00:02:59,220 ‫And if he if he is at -45 degrees, then he's going to go forward to the left. 38 00:02:59,230 --> 00:03:04,840 ‫So no matter which angle we choose for him, he's always going to go to the right direction. 39 00:03:06,220 --> 00:03:09,370 ‫Next, we can go to our update method. 40 00:03:09,370 --> 00:03:12,070 ‫And in here I'm just going to check for the input. 41 00:03:13,120 --> 00:03:27,880 ‫So if the input thought get key down or get key actually down, then key code dot, let's just use space. 42 00:03:27,880 --> 00:03:33,280 ‫So whenever we press base, we want to change the direction of the player. 43 00:03:34,180 --> 00:03:42,790 ‫So let's create a new method which handles that, and I'm going to call it a private void switch. 44 00:03:44,200 --> 00:03:51,960 ‫So what that does is it's going to change the variable walking right to not walking, right. 45 00:03:51,970 --> 00:03:55,930 ‫So that means if I'm walking right was true, it's going to be false now. 46 00:03:55,930 --> 00:03:58,380 ‫And if it was false, then it's going to be true now. 47 00:03:58,390 --> 00:04:00,700 ‫So that's just what we say here. 48 00:04:00,970 --> 00:04:10,570 ‫And now we check if walking right is true, then we transform the rotation. 49 00:04:14,340 --> 00:04:25,110 ‫Then we change the transform rotation value to criterion dot ula and here we can enter the degrees so 50 00:04:25,110 --> 00:04:28,530 ‫we can enter zero, 45 and zero. 51 00:04:30,360 --> 00:04:35,400 ‫That means that I'm just going to change the direction of to where he's walking by 45 degrees. 52 00:04:35,430 --> 00:04:42,420 ‫So pretty much I'm going to change the direct the transform of the player itself by 45 degrees. 53 00:04:42,780 --> 00:04:44,910 ‫So that happens if he walks right. 54 00:04:45,570 --> 00:04:59,400 ‫And if he walks left, we will need to change the transform rotation value to criterion EULAR zero -45 55 00:04:59,400 --> 00:05:00,060 ‫zero. 56 00:05:00,240 --> 00:05:04,260 ‫So as I've just shown you in unity, that's what we're going to do. 57 00:05:04,260 --> 00:05:11,250 ‫So we're going to change the Y to 45 if he's walking right, and -45 as he's walking left. 58 00:05:11,460 --> 00:05:14,130 ‫So now that we have created this script. 59 00:05:17,220 --> 00:05:18,750 ‫Let's go ahead and save it. 60 00:05:19,140 --> 00:05:20,610 ‫Let's go back to Unity. 61 00:05:21,150 --> 00:05:22,770 ‫Let's save our scene. 62 00:05:22,770 --> 00:05:25,880 ‫And by the way, we should maybe clean up as well here. 63 00:05:25,890 --> 00:05:27,330 ‫Let's create a new folder. 64 00:05:27,330 --> 00:05:33,030 ‫I'm going to call it script and let's drag our chart controller into the scripts folder. 65 00:05:33,480 --> 00:05:40,590 ‫And before we test it, of course, we need to call our switch method within the update method. 66 00:05:40,860 --> 00:05:45,780 ‫So whenever somebody press a space, we will go or we will change direction. 67 00:05:45,780 --> 00:05:47,040 ‫So let's check that out. 68 00:05:48,950 --> 00:05:54,140 ‫Our player is moving and I'm turning and turning and boom, he's falling off. 69 00:05:54,140 --> 00:05:55,220 ‫So let's do that again. 70 00:05:57,600 --> 00:06:01,210 ‫Let's move and then forget to turn and he's falling off. 71 00:06:01,230 --> 00:06:03,750 ‫All right, so now we have the basic movement of our game. 72 00:06:03,780 --> 00:06:06,810 ‫Of course, it doesn't look very well because he's not animating. 73 00:06:06,810 --> 00:06:10,890 ‫He is just holding his hands to the right and left, which looks a little weird. 74 00:06:11,040 --> 00:06:14,900 ‫But we're going to change that within the next videos. 75 00:06:15,240 --> 00:06:20,940 ‫So first of all, what is really important, what we will need to do is to change our camera because 76 00:06:20,940 --> 00:06:22,410 ‫now it's not following our player. 77 00:06:22,410 --> 00:06:26,470 ‫So our player is simply running out of our sight when we can't see him. 78 00:06:26,490 --> 00:06:28,350 ‫So let's do that in the next video.