1 00:00:23.510 --> 00:00:29.660 Hello, my name is Hyunjin Kim, and I'll be teaching a basic Unreal Engine course for real-time content implementation 2 00:00:29.660 --> 00:00:34.350 In this session, we'll learn about vector subtraction 3 00:00:34.350 --> 00:00:41.080 and how to use it to make a specific actor follow another actor 4 00:00:41.187 --> 00:00:45.337 Vector subtraction 5 00:00:45.680 --> 00:00:49.050 Let's explore vector subtraction 6 00:00:50.000 --> 00:00:55.200 Now, let's create a Blueprint class in the content browser 7 00:00:55.200 --> 00:01:00.040 Right-click in the content browser, then select "blue print class" 8 00:01:00.040 --> 00:01:06.239 and choose "actor" as the base parent class 9 00:01:06.599 --> 00:01:12.800 Let's name it "bp_aiactor" 10 00:01:14.080 --> 00:01:17.760 Double-click to open it 11 00:01:18.400 --> 00:01:21.519 Let's design its appearance 12 00:01:21.839 --> 00:01:26.400 Now, in the components panel on the left, click the add button 13 00:01:26.400 --> 00:01:32.519 search for "static," and select "static mesh" 14 00:01:32.519 --> 00:01:37.080 to add a static mesh component 15 00:01:37.879 --> 00:01:41.599 Next, select the newly created static mesh component 16 00:01:41.600 --> 00:01:46.600 In the details panel, choose the appearance for the static mesh option 17 00:01:46.639 --> 00:01:49.919 Now, click on the added part to expand it 18 00:01:49.919 --> 00:01:53.839 You can choose any shape you prefer 19 00:01:53.839 --> 00:02:04.000 I'll search for "ball" in the search bar and select "sm_ball_01" 20 00:02:04.599 --> 00:02:11.199 When you press the compile button, the material for this mesh is already set up 21 00:02:11.199 --> 00:02:20.639 You can change the material to any one you prefer; feel free to select the material that suits your preference 22 00:02:21.320 --> 00:02:26.679 Let's first make this actor behave 23 00:02:26.679 --> 00:02:33.199 by moving around after it's placed in the level 24 00:02:34.559 --> 00:02:38.479 Now, in the Event Graph 25 00:02:39.440 --> 00:02:47.679 let's use the formula p=p0 vt to make it move 26 00:02:47.679 --> 00:02:54.240 Last time, we used a similar formula p=p0 vt to move the player 27 00:02:54.240 --> 00:03:03.080 So, I'd like you to pause the video for a moment and try creating a backward movement 28 00:03:03.080 --> 00:03:10.479 similar to what we did last time, recalling the steps we covered 29 00:03:12.000 --> 00:03:19.039 Alright, first let's create p0 30 00:03:19.039 --> 00:03:26.520 Right-click in an empty space, then search for "Get Actor Location" and add that node 31 00:03:26.759 --> 00:03:31.039 Click to add it 32 00:03:31.039 --> 00:03:34.720 This node represents p0 33 00:03:36.160 --> 00:03:39.240 It represents the current location value 34 00:03:40.559 --> 00:03:48.559 Next, let's create v. This vector represents direction and magnitude, essentially the velocity 35 00:03:48.559 --> 00:03:57.039 To move backward, we'll need a vector in the opposite direction. We'll multiply this direction by a speed value to determine how quickly it moves 36 00:03:57.039 --> 00:04:02.440 So let's create a variable to represent the speed or velocity 37 00:04:02.440 --> 00:04:09.639 Go to the variables section, click the plus button to create a new variable 38 00:04:09.639 --> 00:04:15.399 and name it "Speed" 39 00:04:15.399 --> 00:04:21.119 Next, change the variable type to float 40 00:04:21.119 --> 00:04:29.679 Then, click the compile button, and in the details panel on the right, set the default value to 200 41 00:04:29.679 --> 00:04:32.189 Let's get the direction 42 00:04:32.209 --> 00:04:37.959 When we get the right direction, we use 'Get Actor Right Vector' by right-clicking 43 00:04:37.959 --> 00:04:44.000 Now, how do we get the forward direction? 44 00:04:44.000 --> 00:04:47.959 You do "get actor forward vector" 45 00:04:48.000 --> 00:04:56.559 Then there should be a 'Get Actor Forward Vector' that you can use to get the forward direction 46 00:04:56.559 --> 00:05:05.359 So for getting the actor's direction, you have options for the forward vector, right vector, and up vector 47 00:05:05.359 --> 00:05:09.909 How do we get the left, backward, and downward directions? 48 00:05:10.359 --> 00:05:15.339 If you multiply the right vector by minus one, it will give you the left direction 49 00:05:15.839 --> 00:05:22.729 Then, if you multiply the forward vector by -1, it will give you the backward direction 50 00:05:22.829 --> 00:05:28.040 If you multiply the up vector by -1, it will give you the downward direction 51 00:05:28.040 --> 00:05:35.720 So, when you want to get the backward direction, you can multiply the forward vector by -1 and use that 52 00:05:35.720 --> 00:05:38.220 You'll right-click again 53 00:05:38.220 --> 00:05:45.429 and search for "get actor forward vector" to add it 54 00:05:46.079 --> 00:05:49.069 And here, you'll multiply it by -1 55 00:05:49.069 --> 00:05:56.150 Drag the result of the Get Actor Forward Vector node and then multiply it by -1 56 00:05:56.150 --> 00:05:59.480 Add a Multiply node 57 00:05:59.480 --> 00:06:09.409 If you right-click in an empty space and search for "make literal float" 58 00:06:09.409 --> 00:06:11.890 you'll get a node like this 59 00:06:11.890 --> 00:06:17.000 This node simply creates a float value directly 60 00:06:17.000 --> 00:06:19.500 and you add that also 61 00:06:19.500 --> 00:06:23.970 Then, set the value here to -1 62 00:06:24.870 --> 00:06:30.970 Then, multiply the result with the forward vector 63 00:06:30.970 --> 00:06:37.600 Then, this itself becomes the backward vector 64 00:06:38.559 --> 00:06:43.320 Now, you need to multiply this by the speed value 65 00:06:43.320 --> 00:06:48.840 Take the result of multiplying the forward vector by -1, drag it into the graph 66 00:06:48.840 --> 00:06:54.700 and then search for "multiply" to add a Multiply node 67 00:06:54.740 --> 00:06:59.970 Drag the Speed variable from the left to get its value 68 00:07:00.620 --> 00:07:02.920 Let's remove that again 69 00:07:02.920 --> 00:07:05.470 When you hold down the Alt key while clicking and dragging the speed variable 70 00:07:05.470 --> 00:07:12.160 you can bring it in as a node 71 00:07:12.160 --> 00:07:15.460 that allows you to set its value 72 00:07:15.460 --> 00:07:20.040 If you hold down the Ctrl key while clicking and dragging the speed variable 73 00:07:20.040 --> 00:07:24.390 it will bring it in as a node that retrieves its value 74 00:07:24.390 --> 00:07:28.720 So, when we retrieve this speed value 75 00:07:28.720 --> 00:07:33.720 we can use a shortcut to streamline this process 76 00:07:33.720 --> 00:07:36.480 Let's remove that again 77 00:07:36.480 --> 00:07:42.799 Hold down the Ctrl key and click to bring the speed value in as a get node 78 00:07:43.239 --> 00:07:49.720 Then, connect this to the second pin here and multiply it 79 00:07:49.720 --> 00:07:53.799 Alright, now this becomes v 80 00:07:54.119 --> 00:07:57.419 Next, you need to multiply v by time 81 00:07:57.419 --> 00:08:02.399 You can multiply v by 'delta seconds' 82 00:08:02.399 --> 00:08:09.720 So, after multiplying by 'delta seconds,' drag the result 83 00:08:09.720 --> 00:08:13.640 and add another multiply node to multiply it by the speed value 84 00:08:14.200 --> 00:08:19.799 Then drag 'delta seconds' and multiply it with the result 85 00:08:19.839 --> 00:08:27.519 Double-click on the connection line to reroute it neatly without overlapping 86 00:08:27.519 --> 00:08:30.000 This then becomes 87 00:08:30.440 --> 00:08:33.640 the vt value 88 00:08:34.039 --> 00:08:37.239 Now, after adding p0 and vt together 89 00:08:37.239 --> 00:08:43.320 you can set this resulting value as the new location for this actor 90 00:08:43.760 --> 00:08:49.039 Drag the final result of vt 91 00:08:49.239 --> 00:08:53.520 then search for "add"to add an add node 92 00:08:54.039 --> 00:09:00.080 Then, connect the result from p0 to the other input of the add node to add these two values together 93 00:09:00.479 --> 00:09:06.320 So, the final result will be p 94 00:09:06.320 --> 00:09:14.080 Drag it from the add node and search for "set actor location" in the search bar to add the node 95 00:09:14.840 --> 00:09:17.640 Alright, then you just need to connect the execution pin from the event tick 96 00:09:17.640 --> 00:09:22.679 to the execution pin of the set actor location mode 97 00:09:22.760 --> 00:09:29.400 Drag the execution pin from event tick and connect it to the execution pin of set actor location 98 00:09:29.400 --> 00:09:33.719 Let's tidy up the connected lines a bit 99 00:09:34.320 --> 00:09:37.640 And then press the compile button 100 00:09:37.640 --> 00:09:44.840 Now, go to the actual level, drag bp_aiactor onto the screen, and place it 101 00:09:44.840 --> 00:09:51.719 Now, when you press the play button, you should see it moving downward as expected 102 00:09:51.760 --> 00:09:54.850 Alright, let's place one more 103 00:09:54.890 --> 00:10:01.239 Then, when you press play, you should see both of them moving in the same way 104 00:10:02.119 --> 00:10:05.629 When you create content like this 105 00:10:05.629 --> 00:10:13.840 the planning team can request to change the speed value of this moving actor 106 00:10:13.840 --> 00:10:20.840 Then we just need to change the speed value in the bpai actor 107 00:10:20.840 --> 00:10:25.670 But if we keep receiving requests to change these values 108 00:10:25.670 --> 00:10:29.719 it'll end up being extremely inefficient and time-consuming 109 00:10:29.719 --> 00:10:38.960 Therefore, we can set it up so that the planning team can continuously change these values themselves 110 00:10:38.960 --> 00:10:44.160 Next to the data tile of the speed variable we created 111 00:10:44.160 --> 00:10:47.280 there's an icon that looks like a closed eyes 112 00:10:47.280 --> 00:10:55.320 When you select that icon, it changes to an open eye icon 113 00:10:55.400 --> 00:11:02.400 In this state, click the compile button, then move the actual level 114 00:11:02.400 --> 00:11:08.320 After selecting one of the AI actors placed here 115 00:11:08.320 --> 00:11:11.810 and coming to the details window, you'll see 116 00:11:11.810 --> 00:11:16.160 that the value for speed is exposed 117 00:11:16.160 --> 00:11:21.200 within the default category like this 118 00:11:21.440 --> 00:11:25.140 After making it closed eye again 119 00:11:25.140 --> 00:11:28.540 compile it, and return to the actual level 120 00:11:28.540 --> 00:11:31.239 That item will have disappeared 121 00:11:31.239 --> 00:11:37.729 So, for variables like these 122 00:11:37.729 --> 00:11:41.520 where the planning team needs to continually test and find the appropriate values 123 00:11:41.520 --> 00:11:47.020 we expose them by opening the eye icon like this 124 00:11:47.020 --> 00:11:50.039 Then the planning team can simply change these values as needed 125 00:11:50.039 --> 00:11:53.939 I changed it to 50 126 00:11:53.939 --> 00:11:59.159 I changed the speed value of the actor placed on the right to 50 127 00:11:59.159 --> 00:12:04.479 The actor on the other side still has a speed value of 200 128 00:12:04.479 --> 00:12:07.389 So, when you press the play button 129 00:12:07.389 --> 00:12:14.000 one moves at a speed of 200, and the other moves at a speed of 50 130 00:12:14.559 --> 00:12:19.000 So, once you find the appropriate value 131 00:12:19.000 --> 00:12:27.200 you can change the default value in the bp actor to 50 132 00:12:27.200 --> 00:12:31.080 I'll change them all back to 200 for you 133 00:12:32.200 --> 00:12:36.719 I'll leave just one AI actor 134 00:12:37.919 --> 00:12:42.880 Now, when you play 135 00:12:43.119 --> 00:12:46.119 this actor moves backward 136 00:12:46.119 --> 00:12:48.919 To change this 137 00:12:48.919 --> 00:12:56.799 you'll want to make it so that the actor follows the player when the player moves 138 00:12:56.799 --> 00:13:02.640 In this case, if the player is stationary 139 00:13:02.640 --> 00:13:07.080 what direction should this actor move in? 140 00:13:07.080 --> 00:13:10.599 It should move in this direction, right? 141 00:13:11.119 --> 00:13:20.280 We want to determine this direction using vector subtraction 142 00:13:21.239 --> 00:13:26.479 Let me first illustrate vector subtraction with a diagram 143 00:13:26.640 --> 00:13:32.960 In this plane, the player is currently positioned here 144 00:13:32.960 --> 00:13:39.599 Then, our created actor is positioned here 145 00:13:39.599 --> 00:13:47.719 So, if we draw the position vectors of these two actors, it would look like this 146 00:13:52.679 --> 00:14:01.320 Let's call the red vector "a vector" and the green vector "b vector" 147 00:14:01.320 --> 00:14:08.320 Last time, we learned that adding vector a and vector b 148 00:14:09.919 --> 00:14:15.840 results in the vector connecting to this meeting point 149 00:14:15.840 --> 00:14:18.520 But this time 150 00:14:19.119 --> 00:14:25.440 let's try subtracting vector b from vector a 151 00:14:25.440 --> 00:14:30.280 Subtraction can be expressed as addition with negative values 152 00:14:30.280 --> 00:14:34.780 We can express 10 minus 3 153 00:14:34.780 --> 00:14:41.359 as adding -3 to 10 154 00:14:41.359 --> 00:14:47.440 Using the same method 155 00:14:47.440 --> 00:14:56.599 expressing a minus b as a (-b) is equivalent 156 00:14:56.760 --> 00:15:02.960 Subtracting a vector means moving in the opposite direction of that vector 157 00:15:02.960 --> 00:15:09.840 So, -b represents a vector that has the same magnitude as the green b vector 158 00:15:09.840 --> 00:15:15.359 but points in the opposite direction 159 00:15:15.359 --> 00:15:19.659 This is the vector -b 160 00:15:19.659 --> 00:15:24.640 Let's add vector a and vector -b 161 00:15:24.640 --> 00:15:28.590 Let me connect this vector from vector a 162 00:15:28.590 --> 00:15:33.590 which has the same magnitude and opposite direction as b 163 00:15:33.590 --> 00:15:37.119 Next, if we draw a vector from the end of -b that has the same direction and magnitude as a 164 00:15:37.119 --> 00:15:47.200 they will meet at a point 165 00:15:47.479 --> 00:15:53.520 Drawing a vector that connects to this point 166 00:15:54.280 --> 00:16:01.280 will result in the sum of a and -b 167 00:16:01.640 --> 00:16:09.919 When describing vectors, if we say they have the same direction and magnitude, we mean they're the same vector 168 00:16:11.119 --> 00:16:15.440 So, if we position this vector that appears in blue here 169 00:16:16.760 --> 00:16:22.520 what result do we get? 170 00:16:22.520 --> 00:16:28.719 It shows the direction from position b towards a 171 00:16:29.919 --> 00:16:36.919 If we subtract vector a from vector b 172 00:16:37.000 --> 00:16:43.719 we'll get a vector pointing from a towards b 173 00:16:43.719 --> 00:16:52.479 Using vector subtraction like this allows us to determine the direction towards a specific location 174 00:16:53.520 --> 00:17:01.479 So, to determine the direction in which our created AI actor should face toward the player 175 00:17:02.159 --> 00:17:05.400 we'll position it like this 176 00:17:06.439 --> 00:17:10.880 The AI actor is positioned here 177 00:17:11.800 --> 00:17:15.599 and the player is positioned over there 178 00:17:16.800 --> 00:17:19.800 The AI vector 179 00:17:20.280 --> 00:17:23.479 the player position vector 180 00:17:23.479 --> 00:17:29.599 We need to determine a vector that points in this direction 181 00:17:30.280 --> 00:17:39.640 This is achieved by subtracting the AI position vector from the player position vector 182 00:17:41.090 --> 00:17:47.520 So, what I want you to understand is 183 00:17:47.520 --> 00:17:52.680 how vector addition and subtraction are represented visually 184 00:17:52.770 --> 00:17:56.820 Making a specific actor follow 185 00:17:56.959 --> 00:18:01.759 Let's make a specific actor follow 186 00:18:01.759 --> 00:18:04.760 Let's go to the BP AI actor 187 00:18:04.760 --> 00:18:10.239 and use subtraction to make it follow the player 188 00:18:10.560 --> 00:18:14.210 This part was about direction 189 00:18:14.210 --> 00:18:17.800 So, let's modify this part 190 00:18:18.760 --> 00:18:23.680 First, to determine the direction towards the player 191 00:18:23.680 --> 00:18:27.160 we need to know the player's position 192 00:18:31.210 --> 00:18:35.319 Yes, currently the AI actor does not know the player's position 193 00:18:35.319 --> 00:18:44.239 So, at the start, we will find the player to determine their position 194 00:18:45.280 --> 00:18:48.780 Alright, now go to the Event Begin Play section 195 00:18:48.780 --> 00:18:51.199 Right-click in the empty space 196 00:18:51.199 --> 00:18:58.520 and search for the "Get Actor of Class" node 197 00:18:58.680 --> 00:19:00.980 Add that 198 00:19:01.830 --> 00:19:05.660 In the added node, you'll see an item labeled "Actor Class" 199 00:19:05.660 --> 00:19:09.479 If you select and expand the "Select Class" item 200 00:19:09.479 --> 00:19:18.520 you will see a list of all the classes available in Unreal 201 00:19:18.520 --> 00:19:24.000 But what class is the actor we're looking for? 202 00:19:24.000 --> 00:19:30.400 The actor we're looking for is a blueprint class called bp_player that we created 203 00:19:30.400 --> 00:19:38.640 So, if you search for bp_player here, you'll find it listed like this 204 00:19:38.880 --> 00:19:44.680 So, when you select that item 205 00:19:44.680 --> 00:19:54.239 this node will find the instance of the bp_player class in the level when we execute it 206 00:19:54.839 --> 00:20:00.199 And right at the beginning, it will find the player 207 00:20:00.839 --> 00:20:08.160 So, it informs us about the found result through the return value 208 00:20:08.160 --> 00:20:12.760 So, you can drag this return value out and place it 209 00:20:12.760 --> 00:20:18.520 If you select "Promote to Variable" 210 00:20:18.520 --> 00:20:22.400 a variable will be created in the Variables pane on the left 211 00:20:22.400 --> 00:20:25.650 The data type is "bp_player" 212 00:20:25.650 --> 00:20:27.660 We've learned four data types 213 00:20:27.660 --> 00:20:33.199 but we can also use our created classes as data types like this 214 00:20:33.359 --> 00:20:41.439 That means this variable can only hold instances created from the bp_player class 215 00:20:41.439 --> 00:20:47.119 So, let's name it "player" 216 00:20:48.000 --> 00:20:55.880 It finds the player at the start and assigns it to this variable 217 00:20:56.479 --> 00:21:03.079 Now that we've found the player, we can retrieve the player's position 218 00:21:03.640 --> 00:21:11.079 Alright, drag the player variable from the Variables section on the left 219 00:21:13.439 --> 00:21:22.839 then search for "Get Actor Location" and connect it 220 00:21:23.560 --> 00:21:27.040 Let's bring it down here for a moment 221 00:21:31.319 --> 00:21:34.729 Both of these are "Get Actor Location" nodes 222 00:21:34.729 --> 00:21:40.599 The difference between these two is whether they are connected to the player or not 223 00:21:40.599 --> 00:21:44.539 The one above is labeled as "Target: Self" 224 00:21:44.539 --> 00:21:49.519 Here, it automatically refers to itself 225 00:21:49.519 --> 00:21:51.919 But the one below is not referring to itself 226 00:21:51.919 --> 00:21:56.479 it's saying, "Hey player, give me your location" 227 00:21:56.479 --> 00:21:59.179 So, we've retrieved the player's location 228 00:21:59.179 --> 00:22:01.520 Next, we need to retrieve our own location 229 00:22:01.520 --> 00:22:10.050 Let's add another "Get Actor Location" node for the AI actor's position by right-clicking and selecting it from the menu 230 00:22:10.800 --> 00:22:14.040 We need to subtract these values 231 00:22:14.800 --> 00:22:22.160 After dragging and dropping the result of retrieving the player's location, search for "Minus" to perform subtraction 232 00:22:22.160 --> 00:22:24.960 Then you'll see "Subtract" appear 233 00:22:24.960 --> 00:22:27.410 Select "Subtract" 234 00:22:27.410 --> 00:22:33.520 and then connect the current actor's location to the second node 235 00:22:33.520 --> 00:22:40.719 Then, this result will give us the direction in which the AI actor is facing towards the player 236 00:22:40.719 --> 00:22:44.619 Now, we can make the AI actor move in this direction 237 00:22:44.619 --> 00:22:48.040 Let's write this command 238 00:22:48.040 --> 00:22:56.839 This is the direction towards the player 239 00:22:56.839 --> 00:22:59.269 So, instead of using the direction multiplied by -1 of this forward vector 240 00:22:59.269 --> 00:23:05.520 we'll use the direction that comes out from here 241 00:23:05.520 --> 00:23:11.680 So, go ahead and select and delete these three nodes 242 00:23:14.280 --> 00:23:21.000 I'll position the direction towards the player and connect it for you 243 00:23:21.000 --> 00:23:23.920 Let's tidy up the nodes a bit 244 00:23:24.920 --> 00:23:28.880 You've organized the nodes like this 245 00:23:28.880 --> 00:23:35.400 I'll represent the direction towards the player with a slightly different color 246 00:23:35.400 --> 00:23:40.719 Let's compile and play to see how it works 247 00:23:40.719 --> 00:23:45.640 When you play, the AI actor disappears 248 00:23:45.640 --> 00:23:51.400 It's not disappearing; rather, if you select the AI actor in the Outline 249 00:23:51.400 --> 00:23:57.560 you'll notice the location values changing rapidly 250 00:23:57.560 --> 00:24:01.079 And eventually 251 00:24:01.079 --> 00:24:10.880 the blue z-value changes to "inf" or infinity 252 00:24:11.760 --> 00:24:14.110 To understand this situation 253 00:24:14.110 --> 00:24:19.719 a more precise understanding of vector operations is necessary 254 00:24:20.560 --> 00:24:23.800 Let's say we have 255 00:24:23.959 --> 00:24:27.579 vectors a and b on this plane 256 00:24:27.579 --> 00:24:33.719 Currently, we've calculated this direction 257 00:24:34.000 --> 00:24:37.100 But what we're doing here is 258 00:24:37.100 --> 00:24:41.599 multiplying it by the speed value 259 00:24:41.800 --> 00:24:46.760 We currently have it set to 200 260 00:24:47.680 --> 00:24:54.680 Multiplying a vector of this magnitude by 200 makes it 200 times larger 261 00:24:55.119 --> 00:25:02.680 So, it results in moving a very long distance along this vector 262 00:25:02.680 --> 00:25:09.160 Thus, momentarily, the AI actor ends up positioned in this direction 263 00:25:09.160 --> 00:25:12.110 Alright, what's the next step? 264 00:25:12.110 --> 00:25:17.160 We'll again determine the direction in which the AI actor faces towards the player 265 00:25:17.439 --> 00:25:21.359 So, we're calculating this direction 266 00:25:21.920 --> 00:25:26.400 Here, we're multiplying it by 200 267 00:25:26.400 --> 00:25:29.350 We're already moving with such a large vector 268 00:25:29.350 --> 00:25:31.479 and now we're multiplying it by an additional factor of 200 269 00:25:31.479 --> 00:25:34.079 Then it will become extremely large once again 270 00:25:34.079 --> 00:25:39.239 We're currently repeating this logic 271 00:25:39.239 --> 00:25:43.760 So, what we need to do to modify this is 272 00:25:48.800 --> 00:25:57.160 to normalize the direction vector after calculating it 273 00:25:57.160 --> 00:26:01.310 So, we're creating a vector like this 274 00:26:01.310 --> 00:26:05.199 Here, we're multiplying it by speed 275 00:26:05.199 --> 00:26:11.239 and then by time 276 00:26:11.319 --> 00:26:18.000 That way, we achieve the movement speed we desire 277 00:26:18.520 --> 00:26:26.680 Normalizing a vector to have a magnitude of 1 is called vector normalization 278 00:26:27.359 --> 00:26:30.560 It's called normalization, often referred to as "normalize" 279 00:26:34.640 --> 00:26:41.800 So, we used vector subtraction to find the direction towards the player 280 00:26:41.800 --> 00:26:48.920 Now, we'll normalize this vector to make its magnitude 1 281 00:26:48.959 --> 00:26:52.880 Let's go back to the BP AI actor 282 00:26:53.199 --> 00:26:59.400 and handle the direction towards the player 283 00:27:00.239 --> 00:27:02.760 without directly multiplying the calculated result 284 00:27:05.239 --> 00:27:11.920 I'll search for "normalize" by using this result value again 285 00:27:11.920 --> 00:27:15.770 Then, there should be one "normalize" at the top 286 00:27:15.770 --> 00:27:18.920 Add the corresponding node 287 00:27:18.920 --> 00:27:23.470 Then, connect that result value 288 00:27:23.470 --> 00:27:25.880 By doing this 289 00:27:25.880 --> 00:27:33.719 the magnitude of the direction towards the player will be changed to 1 and returned 290 00:27:34.680 --> 00:27:38.080 Now, after compiling and playing 291 00:27:38.080 --> 00:27:47.119 you'll see that the actor will follow the player like this 292 00:27:49.920 --> 00:27:58.479 Let's modify the current node a bit to make it move in a different way 293 00:27:58.479 --> 00:28:04.640 First, let's temporarily disconnect the part that finds the player in the Begin Play event 294 00:28:04.640 --> 00:28:08.280 Next, let's create a new variable 295 00:28:08.280 --> 00:28:11.130 Please click on the plus button in the Variables section 296 00:28:11.130 --> 00:28:14.920 Name it "target" 297 00:28:15.359 --> 00:28:21.880 Next, if you search for "Actor" under object types 298 00:28:21.880 --> 00:28:27.680 you'll find it like this 299 00:28:27.680 --> 00:28:29.419 Selecting that option 300 00:28:29.419 --> 00:28:35.760 will set the data type to "Actor" like this 301 00:28:36.160 --> 00:28:43.439 So, now the "target" variable is capable of storing an actor 302 00:28:44.280 --> 00:28:48.680 Let's temporarily disconnect the "Get Player" node 303 00:28:48.680 --> 00:28:52.789 in the part where we calculate the direction towards the player in the Event Tick 304 00:28:52.789 --> 00:29:00.640 Now, let's drag and use the "target" variable that we created earlier with a "Get" node 305 00:29:01.920 --> 00:29:05.720 So, you've changed it to calculate the direction 306 00:29:05.720 --> 00:29:11.800 towards the position of this "target" using its location 307 00:29:12.000 --> 00:29:17.450 Then, click on the eye icon next to the "target" variable 308 00:29:17.450 --> 00:29:19.680 to make it visible 309 00:29:19.680 --> 00:29:24.640 and expose it in the details pane 310 00:29:24.640 --> 00:29:29.839 Compile and move to the actual level 311 00:29:29.839 --> 00:29:35.839 After selecting the bpai actor, if you look in the details pane 312 00:29:35.839 --> 00:29:39.119 you'll see that the "target" is currently set to "None." 313 00:29:39.119 --> 00:29:42.169 It means that currently, the "target" variable does not have any value assigned to it 314 00:29:42.169 --> 00:29:46.040 If you play in this state, what will happen is that 315 00:29:46.040 --> 00:29:48.190 an error will occur 316 00:29:48.190 --> 00:29:54.000 because the target variable is not assigned a value 317 00:29:54.000 --> 00:30:00.719 so asking for the position of something that doesn't exist causes an error 318 00:30:00.839 --> 00:30:07.119 When we were making the actor chase the player earlier 319 00:30:07.119 --> 00:30:12.760 we assigned a value to the "player" variable at the start 320 00:30:13.160 --> 00:30:21.319 So, when we instructed it to move towards the direction of the player, there were no errors 321 00:30:21.319 --> 00:30:23.819 If you play in the current state 322 00:30:23.819 --> 00:30:29.119 you'll see that an error occurs like this 323 00:30:30.919 --> 00:30:33.769 So, if you click on "None" next to "target" 324 00:30:33.769 --> 00:30:39.039 you'll see a list of all the actors currently placed in this level 325 00:30:39.039 --> 00:30:43.719 This means that all the actors listed in the Outliner here will be shown 326 00:30:43.719 --> 00:30:50.520 Because everything placed in this level is an actor, that's why you see all the actors listed there 327 00:30:50.640 --> 00:30:59.079 Now, if you select "BP Player" here, it should behave the same way as before 328 00:30:59.439 --> 00:31:02.849 Because now the "target" variable contains something—it contains the player 329 00:31:02.849 --> 00:31:06.959 Now it should be functioning to chase after the player 330 00:31:06.959 --> 00:31:09.709 If you play now 331 00:31:09.709 --> 00:31:12.920 it should continue to chase after the player 332 00:31:13.680 --> 00:31:21.520 Go ahead and copy the BP AI actor while holding down the Alt key, then place it where you want 333 00:31:21.880 --> 00:31:25.830 Now with both of these BP AI actors placed 334 00:31:25.830 --> 00:31:31.280 they should both be chasing after the player 335 00:31:31.959 --> 00:31:34.809 Let's go ahead and change some of the values here 336 00:31:34.809 --> 00:31:43.350 Set the speed value for the BP AI actor on the left to 350 337 00:31:43.350 --> 00:31:46.959 and set the "target" to BP Player for that actor 338 00:31:46.959 --> 00:31:54.640 Then, set the speed for the BP AI actor on the right to 300 339 00:31:54.640 --> 00:32:01.439 and set its target to the BP AI actor on the left 340 00:32:01.439 --> 00:32:06.239 So, you're selecting this BP AI actor 341 00:32:06.239 --> 00:32:10.460 Let's make some predictions before pressing the play button 342 00:32:10.460 --> 00:32:17.310 The AI actor on the left has the player as its target, correct? 343 00:32:17.310 --> 00:32:21.640 So, this actor will be chasing after the player 344 00:32:21.640 --> 00:32:30.119 But the actor on the right has its target set to the BP AI actor on the left 345 00:32:30.119 --> 00:32:32.769 Therefore, this actor won't chase after the player 346 00:32:32.769 --> 00:32:38.680 instead, it will start chasing after the AI actor on the left 347 00:32:38.680 --> 00:32:47.560 If you play now, you should see them following each other in a chase-like manner 348 00:32:48.460 --> 00:32:50.520 Let's add one more here 349 00:32:50.520 --> 00:32:53.320 Copy it again by holding Alt 350 00:32:53.320 --> 00:32:57.319 and this time set the speed to 250 351 00:32:57.319 --> 00:33:05.119 Set the target of the newly added actor to BP AI Actor 2 352 00:33:05.560 --> 00:33:07.860 If you play now 353 00:33:07.860 --> 00:33:13.359 you should see them moving in a chain-like formation, chasing each other 354 00:33:14.359 --> 00:33:20.800 If you apply this in a slightly different way 355 00:33:20.800 --> 00:33:25.560 you can create various other forms of movement 356 00:33:26.319 --> 00:33:30.869 So, during your personal study time 357 00:33:30.889 --> 00:33:36.199 I hope you try applying it in various ways 358 00:33:36.359 --> 00:33:44.439 Alright, I'll modify this code to revert back to the blueprint nodes originally used to chase the player 359 00:33:44.439 --> 00:33:47.389 Remove this target 360 00:33:47.389 --> 00:33:51.359 and then you should connect it to the player like this 361 00:33:51.719 --> 00:33:58.319 Next, make sure to connect the node that was in Begin Play so that it runs when executed 362 00:33:58.680 --> 00:34:05.079 Select and delete the variable that was created for the target as well 363 00:34:07.119 --> 00:34:15.120 Then, after moving to the actual level, select and delete the two additional actors that were added 364 00:34:15.120 --> 00:34:22.040 Let's play again to verify that everything is functioning correctly 365 00:34:24.560 --> 00:34:31.529 This time, let's modify it so that it calculates the direction based on the initial position when it starts 366 00:34:31.529 --> 00:34:38.879 and then moves only in that initial direction regardless of where the player moves 367 00:34:38.879 --> 00:34:43.379 The reason the BP AI actor 368 00:34:43.379 --> 00:34:46.399 keeps following the player is because 369 00:34:46.399 --> 00:34:54.679 the Event Tick node updates its direction each time it runs 370 00:34:54.679 --> 00:34:59.369 If we set it up so that we calculate the direction only once at the start 371 00:34:59.369 --> 00:35:03.000 and then move in that direction continuously 372 00:35:03.000 --> 00:35:07.479 the actor won't follow the player even if the player moves 373 00:35:08.000 --> 00:35:15.520 So, let's disconnect these nodes that calculate the direction towards the player 374 00:35:15.520 --> 00:35:19.639 and bring this into the Begin Play event 375 00:35:20.639 --> 00:35:29.360 Then, we'll create a variable to store the result of this direction calculation 376 00:35:29.360 --> 00:35:34.120 So, you'll drag the normalized result 377 00:35:34.120 --> 00:35:39.770 and use "Promote to Variable" to create a variable from it 378 00:35:39.770 --> 00:35:43.280 Name it "dir" 379 00:35:43.280 --> 00:35:50.399 For the variable type, it should automatically be selected as "Vector" for data purposes 380 00:35:50.399 --> 00:35:59.320 Then, connect it so that it runs after finding the player 381 00:35:59.320 --> 00:36:06.719 Then, upon starting, the dir variable should contain the direction towards the player 382 00:36:06.719 --> 00:36:10.600 Now, let's come back down to the Event Tick 383 00:36:10.800 --> 00:36:16.250 Then, you get this dir variable using get 384 00:36:16.250 --> 00:36:21.719 connect it like this, and you're done 385 00:36:23.840 --> 00:36:30.959 And then, after compiling, go to the actual level and play 386 00:36:31.399 --> 00:36:34.009 and it will move towards the player like this 387 00:36:34.009 --> 00:36:37.919 Then, play again and try moving the player 388 00:36:37.919 --> 00:36:39.469 it won't keep following 389 00:36:39.469 --> 00:36:45.520 you can see that it only moves in the direction that was initially set 390 00:36:45.520 --> 00:36:49.220 I'll try moving the player further to the right 391 00:36:50.020 --> 00:36:53.550 Then, although this AI actor previously moved in this direction 392 00:36:53.550 --> 00:36:59.199 this time, it will come towards the new position 393 00:36:59.199 --> 00:37:01.460 Let's try playing it 394 00:37:01.460 --> 00:37:07.879 Even if you move the position, it will still move in the direction calculated initially, right? 395 00:37:07.959 --> 00:37:13.760 So, you can confirm that the actor's movement varies 396 00:37:13.760 --> 00:37:20.400 depending on whether you calculated the direction towards the player 397 00:37:20.400 --> 00:37:26.199 in the Begin Play or in the Event Tick node 398 00:37:26.199 --> 00:37:29.109 I'll tell you about a new node 399 00:37:29.109 --> 00:37:33.879 So, in Begin Play, we're currently locating the player 400 00:37:39.179 --> 00:37:43.439 And then we proceed to perform some other action, right? 401 00:37:43.439 --> 00:37:45.389 First, we find the player 402 00:37:45.389 --> 00:37:49.399 and then we calculate the direction towards the player 403 00:37:49.399 --> 00:37:51.649 Then we need to do something else afterward 404 00:37:51.649 --> 00:37:54.879 So, we need to connect another execution pin at the end, right? 405 00:37:54.879 --> 00:37:57.579 If this increases, what will happen? 406 00:37:57.579 --> 00:38:03.159 It will continue with nodes being connected one after another like a chain 407 00:38:03.159 --> 00:38:07.439 As it continues like that, it might become less readable or inconvenient to navigate 408 00:38:07.439 --> 00:38:12.439 So, there's a blueprint node called "Sequence" 409 00:38:12.439 --> 00:38:18.850 Alright, if you right-click and search for "Sequence" 410 00:38:18.850 --> 00:38:20.900 you'll find a node for it 411 00:38:20.900 --> 00:38:25.159 When you add a Sequence node, it looks like this 412 00:38:25.159 --> 00:38:28.739 It determines the order in which actions are executed 413 00:38:28.739 --> 00:38:31.789 So it executes the first action as 0 414 00:38:31.789 --> 00:38:35.930 and once that's finished, it moves on to execute the second action as 1 415 00:38:36.230 --> 00:38:41.640 So, instead of directly finding the player in Begin Play 416 00:38:41.640 --> 00:38:47.980 we'll disconnect and use this Sequence node to execute actions in order 417 00:38:47.980 --> 00:38:51.630 In Begin Play, after connecting to the Sequence node 418 00:38:51.630 --> 00:38:55.159 we link the first task that needs to be performed 419 00:38:55.159 --> 00:38:59.000 Please also delete the connections from this node 420 00:39:02.420 --> 00:39:08.360 Then, connect the second task that needs to be done to "Then 1" on this side 421 00:39:12.719 --> 00:39:15.569 Using the Sequence node allows you to sequentially proceed 422 00:39:15.569 --> 00:39:20.000 with finding the player 423 00:39:20.000 --> 00:39:23.140 and then calculating the direction towards the player 424 00:39:23.140 --> 00:39:25.190 If you want to do more 425 00:39:25.190 --> 00:39:28.800 you can click the "Add Pin" button here 426 00:39:28.800 --> 00:39:37.439 which will add another execution pin so you can connect the next node that should be executed in sequence 427 00:39:38.239 --> 00:39:40.739 If you don't need it, you can right-click 428 00:39:40.739 --> 00:39:45.760 and select "Remove Execution Pin" 429 00:39:45.760 --> 00:39:49.000 to make that pin disappear 430 00:39:49.679 --> 00:39:57.360 So, now we can organize nodes more intuitively using the Sequence node 431 00:39:58.520 --> 00:40:02.460 Let's summarize what we've learned in this session 432 00:40:02.460 --> 00:40:09.120 We've learned that using vector subtraction allows you to determine the direction towards a specific actor 433 00:40:09.120 --> 00:40:15.120 We've learned how to use vector subtraction to continuously follow a specific actor 434 00:40:15.120 --> 00:40:20.870 And executing the functionality to determine the direction towards the actor in the tick node 435 00:40:20.870 --> 00:40:23.360 allows the actor to continuously follow it 436 00:40:23.360 --> 00:40:25.790 We've learned that executing this in the Begin Play node 437 00:40:25.790 --> 00:40:29.890 sets the actor's direction once based on the initial position 438 00:40:29.890 --> 00:40:33.399 and moves in that direction continuously 439 00:40:33.659 --> 00:40:36.019 Vector subtraction Direction Vector: Right Get Actor Right Vector 440 00:40:36.019 --> 00:40:38.426 Forward Vector: Get Actor Forward Vector Left: Right Vector * -1 Backward: Forward Vector * -1 441 00:40:38.426 --> 00:40:40.626 Using vector subtraction to determine the direction towards a specific location 442 00:40:40.626 --> 00:40:42.926 Making a specific actor follow Normalizing vectors Making the vector size 1 443 00:40:42.926 --> 00:40:45.226 Functionality of nodes for determining actor direction Event Tick Node: Continuously follows the actor Begin Play Node: Sets direction once and moves in that direction continuously 444 00:40:45.226 --> 00:40:47.646 Determining execution order Sequence node