1 00:00:00,383 --> 00:00:02,728 In this video we'll add code to the ship 2 00:00:02,729 --> 00:00:04,532 to control it with the keyboard. 3 00:00:04,533 --> 00:00:08,000 That way we'll learn about Godot's input mapping features 4 00:00:08,001 --> 00:00:10,833 and how to listen to input in our code. 5 00:00:11,283 --> 00:00:14,250 Head to the project menu in the top left, 6 00:00:14,251 --> 00:00:17,983 then Project Settings and to the Input Map tab. 7 00:00:18,167 --> 00:00:22,342 This tab lists the key mappings in your Godot project. 8 00:00:22,343 --> 00:00:25,041 It allows you to map keys on the keyboard 9 00:00:25,042 --> 00:00:27,092 mouse clicks, mouse buttons 10 00:00:27,093 --> 00:00:29,082 or buttons on a gamepad 11 00:00:29,083 --> 00:00:32,058 to a name you can use conveniently. 12 00:00:32,775 --> 00:00:36,156 Now you have a bunch of them that come precreated 13 00:00:36,157 --> 00:00:39,375 in every Godot project for user interfaces. 14 00:00:39,767 --> 00:00:46,067 We also created a couple more in this project for the practices 15 00:00:46,068 --> 00:00:48,958 and for a couple of features we have in this course. 16 00:00:49,108 --> 00:00:53,158 But we're going to be recreating a couple for practice in there. 17 00:00:53,467 --> 00:00:58,133 I invite you to scroll to the point you see the move_left input 18 00:00:58,134 --> 00:01:01,950 and on the right we're going to be clicking this dust bin 19 00:01:01,951 --> 00:01:04,125 to remove the move left input. 20 00:01:04,126 --> 00:01:08,458 So click the input next to that to move right, move up, down and boost 21 00:01:08,459 --> 00:01:10,200 because we'll be remaking them. 22 00:01:11,000 --> 00:01:12,742 I'll scroll back to the top 23 00:01:12,929 --> 00:01:16,067 and typically the way you would create a new input mapping 24 00:01:16,068 --> 00:01:20,617 is by going to this action bar and you start by typing in the name. 25 00:01:20,618 --> 00:01:25,050 For example, to move left we'll use something like Move and Scroll Left. 26 00:01:25,051 --> 00:01:29,875 You can press Enter to add the input action and it will be appended 27 00:01:29,876 --> 00:01:31,258 at the bottom of the list. 28 00:01:31,933 --> 00:01:35,483 Then, to the right you have a little "add event" button. 29 00:01:35,750 --> 00:01:38,117 If you click it you get a drop down menu 30 00:01:38,118 --> 00:01:41,975 where you can choose the kind of key or button 31 00:01:41,976 --> 00:01:44,192 that you want to map to this action. 32 00:01:44,425 --> 00:01:46,517 Typically you'll want to use something like 33 00:01:46,518 --> 00:01:49,742 Physical Key or Key for the keyboard. 34 00:01:49,743 --> 00:01:53,000 Joy Button and Access are for gamepads 35 00:01:53,001 --> 00:01:55,900 and Mouse Button for the mouse or touchscreens. 36 00:01:56,550 --> 00:01:58,899 The difference between Physical Key and Key 37 00:01:58,900 --> 00:02:03,008 is that people have different keyboard layouts around the world. 38 00:02:03,009 --> 00:02:09,192 For example, in North America they use the WASD keys to move a character. 39 00:02:09,717 --> 00:02:14,675 In France, we might use ZQSD because the layout is different. 40 00:02:14,917 --> 00:02:20,008 Physical Key is going to store the position of the key on the keyboard 41 00:02:20,009 --> 00:02:21,442 rather than the letter. 42 00:02:21,443 --> 00:02:23,375 and that's what we'll be using in here. 43 00:02:23,376 --> 00:02:27,458 Click Physical Key and then you can type A, for example, 44 00:02:27,583 --> 00:02:29,600 to make the character move left. 45 00:02:29,850 --> 00:02:32,970 Now we're going to create three more inputs like these 46 00:02:33,217 --> 00:02:36,583 for the move_right, up, and down actions. 47 00:02:36,783 --> 00:02:40,517 In the action bar type "move_right". 48 00:02:40,518 --> 00:02:42,307 We're going to create them all at once. 49 00:02:42,308 --> 00:02:46,725 So move_up and move_down. 50 00:02:47,608 --> 00:02:50,558 For move_right, you click the plus button next to it. 51 00:02:50,559 --> 00:02:53,475 Physical Key, press the D key in my case 52 00:02:53,476 --> 00:02:56,842 click OK, move up, same thing. Physical Key. 53 00:02:56,843 --> 00:03:02,683 I'm going to press W, click OK to create the action and then Move Down. 54 00:03:02,684 --> 00:03:04,558 I'm going to add Physical Key. 55 00:03:04,559 --> 00:03:08,008 Press S to move down and OK, 56 00:03:08,292 --> 00:03:13,350 and with that we have the input actions and we can start using them in our code. 57 00:03:13,442 --> 00:03:17,133 Can click the Close button to get back to the script editor. 58 00:03:17,134 --> 00:03:20,108 We will now add a couple of lines of code 59 00:03:20,109 --> 00:03:24,225 to calculate the desired move direction of the player 60 00:03:24,226 --> 00:03:25,792 based on the key presses. 61 00:03:26,017 --> 00:03:30,150 I invite you to put your cursor at the end of the process function 62 00:03:30,151 --> 00:03:32,983 and press Enter to insert a couple of lines 63 00:03:32,984 --> 00:03:35,933 because we need to add code at the top of the function. 64 00:03:36,500 --> 00:03:39,917 We'll start by defining a variable named "direction" 65 00:03:39,918 --> 00:03:42,838 that will be our ship's move direction 66 00:03:43,038 --> 00:03:47,075 and we will initialize it with a value of Vector2.ZERO. 67 00:03:47,317 --> 00:03:54,067 Now, typically a direction vector is a vector with a maximum length of one. 68 00:03:54,068 --> 00:03:59,675 This allows us to represent only the move direction of the player 69 00:03:59,676 --> 00:04:02,400 and we can multiply that by a speed 70 00:04:02,401 --> 00:04:05,133 to give our ship a velocity. 71 00:04:05,134 --> 00:04:09,383 That is to say, a direction and a speed together. 72 00:04:10,186 --> 00:04:13,433 Let's calculate the direction and I'll explain as we go. 73 00:04:13,434 --> 00:04:17,333 So we're going to start with the horizontal direction. 74 00:04:17,516 --> 00:04:22,383 On a new line we type, direction.x = 75 00:04:22,758 --> 00:04:26,400 and we're going to type, Input.get_axis 76 00:04:26,817 --> 00:04:30,892 and then this is a function that is going to give us a value 77 00:04:30,893 --> 00:04:34,217 between minus one and one depending 78 00:04:34,218 --> 00:04:38,233 on the two input actions that we give it as arguments. 79 00:04:38,483 --> 00:04:42,692 We first have to write the input 80 00:04:42,693 --> 00:04:46,342 that makes the value go towards minus one. 81 00:04:46,683 --> 00:04:51,350 This is going to be move_left because minus one represents 82 00:04:51,351 --> 00:04:55,824 the left horizontal direction in the engine 83 00:04:55,825 --> 00:05:00,000 and then we add a comma followed by move_right. 84 00:05:00,333 --> 00:05:04,300 Note how you have to put the input actions defined 85 00:05:04,301 --> 00:05:07,392 in the project settings in quotes there. 86 00:05:07,825 --> 00:05:12,874 This is going to give us minus one if we press the A key 87 00:05:12,875 --> 00:05:16,042 and one if we press the D key. 88 00:05:16,383 --> 00:05:20,225 Then we're going to do something similar for the y-axis. 89 00:05:20,226 --> 00:05:26,717 direction.y = and you can copy the Input.get_axis call 90 00:05:26,718 --> 00:05:31,041 from the line above and press Control V to paste it there. 91 00:05:31,042 --> 00:05:33,225 We just have to change the input action. 92 00:05:33,226 --> 00:05:36,417 So we're going to turn move_left into move_up 93 00:05:36,708 --> 00:05:42,625 because minus one on the vertical axis represents moving up. 94 00:05:42,925 --> 00:05:46,758 We're going to change the second argument to, move_down. 95 00:05:47,083 --> 00:05:51,175 Before we move on, a word on this input thing here. 96 00:05:51,350 --> 00:05:53,683 It's what we call a "global object". 97 00:05:53,684 --> 00:05:56,217 It's something you can access in any script 98 00:05:56,400 --> 00:05:58,583 and you can call functions on it, 99 00:05:58,584 --> 00:06:01,250 functions that are useful to make your game. 100 00:06:01,383 --> 00:06:03,782 You can use it to calculate the mouse position, 101 00:06:03,783 --> 00:06:05,642 to know if a key was pressed. 102 00:06:05,958 --> 00:06:08,983 We'll see that moving forward in the course. 103 00:06:09,867 --> 00:06:13,317 Now we're going to change how we move the ship. 104 00:06:13,417 --> 00:06:16,625 We want to calculate our velocity 105 00:06:16,626 --> 00:06:22,075 by multiplying the direction vector, we just got, by some speed value. 106 00:06:22,392 --> 00:06:23,967 At the top of our script, 107 00:06:23,968 --> 00:06:27,358 we're going to define a new variable named "max speed". 108 00:06:27,500 --> 00:06:30,683 This will be the ship's maximum movement speed 109 00:06:30,858 --> 00:06:34,450 and let's set it to 600 pixels per second. 110 00:06:35,170 --> 00:06:38,341 Then we can change the default value of the velocity 111 00:06:38,342 --> 00:06:43,069 because now the velocity will depend on our keyboard input. 112 00:06:43,070 --> 00:06:45,283 We'll set it to Vector2.ZERO. 113 00:06:45,435 --> 00:06:47,457 That is to say, if we don't press anything 114 00:06:47,458 --> 00:06:50,117 at the start of the game, the ship will not move. 115 00:06:51,783 --> 00:06:57,900 Down before we change the position, we're going to calculate the velocity. 116 00:06:57,901 --> 00:07:03,825 Like so: we're going to write velocity is equal to direction 117 00:07:04,042 --> 00:07:06,708 multiplied by maximum speed. 118 00:07:07,133 --> 00:07:10,608 This is a fairly typical way of coding movement in games. 119 00:07:11,158 --> 00:07:13,525 You will see why at the end of the series 120 00:07:13,526 --> 00:07:17,075 when we add smooth movement to the ship. 121 00:07:17,225 --> 00:07:21,442 You can try your game by pressing F6 on your keyboard. 122 00:07:21,443 --> 00:07:25,625 Now, if you press WASD, you will see your ship can move. 123 00:07:25,626 --> 00:07:27,533 It moves horizontally, diagonally 124 00:07:27,534 --> 00:07:31,142 and the thrusters update when it's not moving. 125 00:07:31,475 --> 00:07:34,042 There's one thing we need to fix though. 126 00:07:34,043 --> 00:07:38,458 If you try to move horizontally and then diagonally, you should see that 127 00:07:38,459 --> 00:07:42,308 when you go diagonally, the ship moves instantly faster. 128 00:07:42,692 --> 00:07:43,633 Why is that? 129 00:07:44,117 --> 00:07:47,783 Well, this is due to our direction calculation right now. 130 00:07:48,225 --> 00:07:52,000 When we only go horizontally or vertically, 131 00:07:52,001 --> 00:07:56,058 we might get a value of one on that axis. 132 00:07:56,059 --> 00:08:00,725 But when we go diagonally, we get an x direction, say, of one 133 00:08:00,726 --> 00:08:02,983 and a y direction of one, 134 00:08:02,984 --> 00:08:06,142 and our vector ends up being longer. 135 00:08:06,143 --> 00:08:08,975 So it's increasing our velocity. 136 00:08:09,308 --> 00:08:12,000 But we want the ship to move at the same speed 137 00:08:12,001 --> 00:08:13,942 regardless of its move direction. 138 00:08:14,425 --> 00:08:18,092 To do that, we have to add some condition, like so. 139 00:08:18,667 --> 00:08:21,933 After calculating the direction, we're going to add new lines. 140 00:08:22,108 --> 00:08:25,300 We're going to say, "if direction.length", 141 00:08:25,301 --> 00:08:29,867 length is a function that tells you the length of the vector. 142 00:08:30,167 --> 00:08:34,899 If it's greater than one, so if we're moving diagonally, basically, 143 00:08:35,533 --> 00:08:38,450 we're going to change the direction value. 144 00:08:38,451 --> 00:08:42,967 So we say direction is equal to direction.normalized. 145 00:08:43,575 --> 00:08:47,492 This is another function of the vector 2 type 146 00:08:47,493 --> 00:08:51,175 that's going to limit the length of the vector to one. 147 00:08:51,933 --> 00:08:57,000 With these two lines, the ship will not move faster when going diagonally. 148 00:08:57,783 --> 00:09:01,267 These two lines also work great with a gamepad, 149 00:09:01,268 --> 00:09:05,283 if you are working with a joystick and using analog input. 150 00:09:05,725 --> 00:09:08,975 There's one small little bug that we want to address, 151 00:09:08,976 --> 00:09:11,017 that if you move to the left and stop, 152 00:09:11,018 --> 00:09:13,825 the ship goes back to looking to the right. 153 00:09:14,108 --> 00:09:18,733 One thing we can do is only change the rotation in our code 154 00:09:18,734 --> 00:09:23,142 when the ship is actually moving or when there's an input direction. 155 00:09:23,143 --> 00:09:25,700 So we're going to add a line before it. 156 00:09:25,858 --> 00:09:33,608 We're going to say, "if direction.length is greater than zero" 157 00:09:33,817 --> 00:09:36,992 or we can use the shorthand, "if direction" 158 00:09:37,008 --> 00:09:39,925 the computer will understand that we want a vector 159 00:09:39,926 --> 00:09:42,008 with a length greater than zero. 160 00:09:42,950 --> 00:09:47,467 We can select the line below and press the tab key 161 00:09:47,468 --> 00:09:49,567 to increase its indent level 162 00:09:49,568 --> 00:09:53,417 to run it only if the direction vector 163 00:09:53,418 --> 00:09:55,400 has a length greater than zero. 164 00:09:55,600 --> 00:09:57,800 In that case, we rotate the ship. 165 00:09:58,033 --> 00:10:00,507 You can now try the game and move 166 00:10:00,508 --> 00:10:01,824 and when you stop moving, 167 00:10:01,825 --> 00:10:05,083 the ship preserves its current loop direction. 168 00:10:05,292 --> 00:10:06,941 With that, in the next video, 169 00:10:06,942 --> 00:10:09,592 we'll add a boosting mechanic to the ship. 170 00:10:09,593 --> 00:10:13,091 Which will be a great opportunity to look at signals, 171 00:10:13,092 --> 00:10:15,749 another very important feature in Godot. 172 00:10:15,750 --> 00:10:16,758 See you there.