Hello, my name is Hyunjin Kim, and I'll be teaching a basic Unreal Engine course for real-time content implementation
In this session, we'll learn about vector subtraction
and how to use it to make a specific actor follow another actor
Vector subtraction
Let's explore vector subtraction
Now, let's create a Blueprint class in the content browser
Right-click in the content browser, then select "blue print class"
and choose "actor" as the base parent class
Let's name it "bp_aiactor"
Double-click to open it
Let's design its appearance
Now, in the components panel on the left, click the add button
search for "static," and select "static mesh"
to add a static mesh component
Next, select the newly created static mesh component
In the details panel, choose the appearance for the static mesh option
Now, click on the added part to expand it
You can choose any shape you prefer
I'll search for "ball" in the search bar and select "sm_ball_01"
When you press the compile button, the material for this mesh is already set up
You can change the material to any one you prefer; feel free to select the material that suits your preference
Let's first make this actor behave
by moving around after it's placed in the level
Now, in the Event Graph
let's use the formula p=p0 vt to make it move
Last time, we used a similar formula p=p0 vt to move the player
So, I'd like you to pause the video for a moment and try creating a backward movement
similar to what we did last time, recalling the steps we covered
Alright, first let's create p0
Right-click in an empty space, then search for "Get Actor Location" and add that node
Click to add it
This node represents p0
It represents the current location value
Next, let's create v. This vector represents direction and magnitude, essentially the velocity
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
So let's create a variable to represent the speed or velocity
Go to the variables section, click the plus button to create a new variable
and name it "Speed"
Next, change the variable type to float
Then, click the compile button, and in the details panel on the right, set the default value to 200
Let's get the direction
When we get the right direction, we use 'Get Actor Right Vector' by right-clicking
Now, how do we get the forward direction?
You do "get actor forward vector"
Then there should be a 'Get Actor Forward Vector' that you can use to get the forward direction
So for getting the actor's direction, you have options for the forward vector, right vector, and up vector
How do we get the left, backward, and downward directions?
If you multiply the right vector by minus one, it will give you the left direction
Then, if you multiply the forward vector by -1, it will give you the backward direction
If you multiply the up vector by -1, it will give you the downward direction
So, when you want to get the backward direction, you can multiply the forward vector by -1 and use that
You'll right-click again
and search for "get actor forward vector" to add it
And here, you'll multiply it by -1
Drag the result of the Get Actor Forward Vector node and then multiply it by -1
Add a Multiply node
If you right-click in an empty space and search for "make literal float"
you'll get a node like this
This node simply creates a float value directly
and you add that also
Then, set the value here to -1
Then, multiply the result with the forward vector
Then, this itself becomes the backward vector
Now, you need to multiply this by the speed value
Take the result of multiplying the forward vector by -1, drag it into the graph
and then search for "multiply" to add a Multiply node
Drag the Speed variable from the left to get its value
Let's remove that again
When you hold down the Alt key while clicking and dragging the speed variable
you can bring it in as a node
that allows you to set its value
If you hold down the Ctrl key while clicking and dragging the speed variable
it will bring it in as a node that retrieves its value
So, when we retrieve this speed value
we can use a shortcut to streamline this process
Let's remove that again
Hold down the Ctrl key and click to bring the speed value in as a get node
Then, connect this to the second pin here and multiply it
Alright, now this becomes v
Next, you need to multiply v by time
You can multiply v by 'delta seconds'
So, after multiplying by 'delta seconds,' drag the result
and add another multiply node to multiply it by the speed value
Then drag 'delta seconds' and multiply it with the result
Double-click on the connection line to reroute it neatly without overlapping
This then becomes
the vt value
Now, after adding p0 and vt together
you can set this resulting value as the new location for this actor
Drag the final result of vt
then search for "add"to add an add node
Then, connect the result from p0 to the other input of the add node to add these two values together
So, the final result will be p
Drag it from the add node and search for "set actor location" in the search bar to add the node
Alright, then you just need to connect the execution pin from the event tick
to the execution pin of the set actor location mode
Drag the execution pin from event tick and connect it to the execution pin of set actor location
Let's tidy up the connected lines a bit
And then press the compile button
Now, go to the actual level, drag bp_aiactor onto the screen, and place it
Now, when you press the play button, you should see it moving downward as expected
Alright, let's place one more
Then, when you press play, you should see both of them moving in the same way
When you create content like this
the planning team can request to change the speed value of this moving actor
Then we just need to change the speed value in the bpai actor
But if we keep receiving requests to change these values
it'll end up being extremely inefficient and time-consuming
Therefore, we can set it up so that the planning team can continuously change these values themselves
Next to the data tile of the speed variable we created
there's an icon that looks like a closed eyes
When you select that icon, it changes to an open eye icon
In this state, click the compile button, then move the actual level
After selecting one of the AI actors placed here
and coming to the details window, you'll see
that the value for speed is exposed
within the default category like this
After making it closed eye again
compile it, and return to the actual level
That item will have disappeared
So, for variables like these
where the planning team needs to continually test and find the appropriate values
we expose them by opening the eye icon like this
Then the planning team can simply change these values as needed
I changed it to 50
I changed the speed value of the actor placed on the right to 50
The actor on the other side still has a speed value of 200
So, when you press the play button
one moves at a speed of 200, and the other moves at a speed of 50
So, once you find the appropriate value
you can change the default value in the bp actor to 50
I'll change them all back to 200 for you
I'll leave just one AI actor
Now, when you play
this actor moves backward
To change this
you'll want to make it so that the actor follows the player when the player moves
In this case, if the player is stationary
what direction should this actor move in?
It should move in this direction, right?
We want to determine this direction using vector subtraction
Let me first illustrate vector subtraction with a diagram
In this plane, the player is currently positioned here
Then, our created actor is positioned here
So, if we draw the position vectors of these two actors, it would look like this
Let's call the red vector "a vector" and the green vector "b vector"
Last time, we learned that adding vector a and vector b
results in the vector connecting to this meeting point
But this time
let's try subtracting vector b from vector a
Subtraction can be expressed as addition with negative values
We can express 10 minus 3
as adding -3 to 10
Using the same method
expressing a minus b as a (-b) is equivalent
Subtracting a vector means moving in the opposite direction of that vector
So, -b represents a vector that has the same magnitude as the green b vector
but points in the opposite direction
This is the vector -b
Let's add vector a and vector -b
Let me connect this vector from vector a
which has the same magnitude and opposite direction as b
Next, if we draw a vector from the end of -b that has the same direction and magnitude as a
they will meet at a point
Drawing a vector that connects to this point
will result in the sum of a and -b
When describing vectors, if we say they have the same direction and magnitude, we mean they're the same vector
So, if we position this vector that appears in blue here
what result do we get?
It shows the direction from position b towards a
If we subtract vector a from vector b
we'll get a vector pointing from a towards b
Using vector subtraction like this allows us to determine the direction towards a specific location
So, to determine the direction in which our created AI actor should face toward the player
we'll position it like this
The AI actor is positioned here
and the player is positioned over there
The AI vector
the player position vector
We need to determine a vector that points in this direction
This is achieved by subtracting the AI position vector from the player position vector
So, what I want you to understand is
how vector addition and subtraction are represented visually
Making a specific actor follow
Let's make a specific actor follow
Let's go to the BP AI actor
and use subtraction to make it follow the player
This part was about direction
So, let's modify this part
First, to determine the direction towards the player
we need to know the player's position
Yes, currently the AI actor does not know the player's position
So, at the start, we will find the player to determine their position
Alright, now go to the Event Begin Play section
Right-click in the empty space
and search for the "Get Actor of Class" node
Add that
In the added node, you'll see an item labeled "Actor Class"
If you select and expand the "Select Class" item
you will see a list of all the classes available in Unreal
But what class is the actor we're looking for?
The actor we're looking for is a blueprint class called bp_player that we created
So, if you search for bp_player here, you'll find it listed like this
So, when you select that item
this node will find the instance of the bp_player class in the level when we execute it
And right at the beginning, it will find the player
So, it informs us about the found result through the return value
So, you can drag this return value out and place it
If you select "Promote to Variable"
a variable will be created in the Variables pane on the left
The data type is "bp_player"
We've learned four data types
but we can also use our created classes as data types like this
That means this variable can only hold instances created from the bp_player class
So, let's name it "player"
It finds the player at the start and assigns it to this variable
Now that we've found the player, we can retrieve the player's position
Alright, drag the player variable from the Variables section on the left
then search for "Get Actor Location" and connect it
Let's bring it down here for a moment
Both of these are "Get Actor Location" nodes
The difference between these two is whether they are connected to the player or not
The one above is labeled as "Target: Self"
Here, it automatically refers to itself
But the one below is not referring to itself
it's saying, "Hey player, give me your location"
So, we've retrieved the player's location
Next, we need to retrieve our own location
Let's add another "Get Actor Location" node for the AI actor's position by right-clicking and selecting it from the menu
We need to subtract these values
After dragging and dropping the result of retrieving the player's location, search for "Minus" to perform subtraction
Then you'll see "Subtract" appear
Select "Subtract"
and then connect the current actor's location to the second node
Then, this result will give us the direction in which the AI actor is facing towards the player
Now, we can make the AI actor move in this direction
Let's write this command
This is the direction towards the player
So, instead of using the direction multiplied by -1 of this forward vector
we'll use the direction that comes out from here
So, go ahead and select and delete these three nodes
I'll position the direction towards the player and connect it for you
Let's tidy up the nodes a bit
You've organized the nodes like this
I'll represent the direction towards the player with a slightly different color
Let's compile and play to see how it works
When you play, the AI actor disappears
It's not disappearing; rather, if you select the AI actor in the Outline
you'll notice the location values changing rapidly
And eventually
the blue z-value changes to "inf" or infinity
To understand this situation
a more precise understanding of vector operations is necessary
Let's say we have
vectors a and b on this plane
Currently, we've calculated this direction
But what we're doing here is
multiplying it by the speed value
We currently have it set to 200
Multiplying a vector of this magnitude by 200 makes it 200 times larger
So, it results in moving a very long distance along this vector
Thus, momentarily, the AI actor ends up positioned in this direction
Alright, what's the next step?
We'll again determine the direction in which the AI actor faces towards the player
So, we're calculating this direction
Here, we're multiplying it by 200
We're already moving with such a large vector
and now we're multiplying it by an additional factor of 200
Then it will become extremely large once again
We're currently repeating this logic
So, what we need to do to modify this is
to normalize the direction vector after calculating it
So, we're creating a vector like this
Here, we're multiplying it by speed
and then by time
That way, we achieve the movement speed we desire
Normalizing a vector to have a magnitude of 1 is called vector normalization
It's called normalization, often referred to as "normalize"
So, we used vector subtraction to find the direction towards the player
Now, we'll normalize this vector to make its magnitude 1
Let's go back to the BP AI actor
and handle the direction towards the player
without directly multiplying the calculated result
I'll search for "normalize" by using this result value again
Then, there should be one "normalize" at the top
Add the corresponding node
Then, connect that result value
By doing this
the magnitude of the direction towards the player will be changed to 1 and returned
Now, after compiling and playing
you'll see that the actor will follow the player like this
Let's modify the current node a bit to make it move in a different way
First, let's temporarily disconnect the part that finds the player in the Begin Play event
Next, let's create a new variable
Please click on the plus button in the Variables section
Name it "target"
Next, if you search for "Actor" under object types
you'll find it like this
Selecting that option
will set the data type to "Actor" like this
So, now the "target" variable is capable of storing an actor
Let's temporarily disconnect the "Get Player" node
in the part where we calculate the direction towards the player in the Event Tick
Now, let's drag and use the "target" variable that we created earlier with a "Get" node
So, you've changed it to calculate the direction
towards the position of this "target" using its location
Then, click on the eye icon next to the "target" variable
to make it visible
and expose it in the details pane
Compile and move to the actual level
After selecting the bpai actor, if you look in the details pane
you'll see that the "target" is currently set to "None."
It means that currently, the "target" variable does not have any value assigned to it
If you play in this state, what will happen is that
an error will occur
because the target variable is not assigned a value
so asking for the position of something that doesn't exist causes an error
When we were making the actor chase the player earlier
we assigned a value to the "player" variable at the start
So, when we instructed it to move towards the direction of the player, there were no errors
If you play in the current state
you'll see that an error occurs like this
So, if you click on "None" next to "target"
you'll see a list of all the actors currently placed in this level
This means that all the actors listed in the Outliner here will be shown
Because everything placed in this level is an actor, that's why you see all the actors listed there
Now, if you select "BP Player" here, it should behave the same way as before
Because now the "target" variable contains something—it contains the player
Now it should be functioning to chase after the player
If you play now
it should continue to chase after the player
Go ahead and copy the BP AI actor while holding down the Alt key, then place it where you want
Now with both of these BP AI actors placed
they should both be chasing after the player
Let's go ahead and change some of the values here
Set the speed value for the BP AI actor on the left to 350
and set the "target" to BP Player for that actor
Then, set the speed for the BP AI actor on the right to 300
and set its target to the BP AI actor on the left
So, you're selecting this BP AI actor
Let's make some predictions before pressing the play button
The AI actor on the left has the player as its target, correct?
So, this actor will be chasing after the player
But the actor on the right has its target set to the BP AI actor on the left
Therefore, this actor won't chase after the player
instead, it will start chasing after the AI actor on the left
If you play now, you should see them following each other in a chase-like manner
Let's add one more here
Copy it again by holding Alt
and this time set the speed to 250
Set the target of the newly added actor to BP AI Actor 2
If you play now
you should see them moving in a chain-like formation, chasing each other
If you apply this in a slightly different way
you can create various other forms of movement
So, during your personal study time
I hope you try applying it in various ways
Alright, I'll modify this code to revert back to the blueprint nodes originally used to chase the player
Remove this target
and then you should connect it to the player like this
Next, make sure to connect the node that was in Begin Play so that it runs when executed
Select and delete the variable that was created for the target as well
Then, after moving to the actual level, select and delete the two additional actors that were added
Let's play again to verify that everything is functioning correctly
This time, let's modify it so that it calculates the direction based on the initial position when it starts
and then moves only in that initial direction regardless of where the player moves
The reason the BP AI actor
keeps following the player is because
the Event Tick node updates its direction each time it runs
If we set it up so that we calculate the direction only once at the start
and then move in that direction continuously
the actor won't follow the player even if the player moves
So, let's disconnect these nodes that calculate the direction towards the player
and bring this into the Begin Play event
Then, we'll create a variable to store the result of this direction calculation
So, you'll drag the normalized result
and use "Promote to Variable" to create a variable from it
Name it "dir"
For the variable type, it should automatically be selected as "Vector" for data purposes
Then, connect it so that it runs after finding the player
Then, upon starting, the dir variable should contain the direction towards the player
Now, let's come back down to the Event Tick
Then, you get this dir variable using get
connect it like this, and you're done
And then, after compiling, go to the actual level and play
and it will move towards the player like this
Then, play again and try moving the player
it won't keep following
you can see that it only moves in the direction that was initially set
I'll try moving the player further to the right
Then, although this AI actor previously moved in this direction
this time, it will come towards the new position
Let's try playing it
Even if you move the position, it will still move in the direction calculated initially, right?
So, you can confirm that the actor's movement varies
depending on whether you calculated the direction towards the player
in the Begin Play or in the Event Tick node
I'll tell you about a new node
So, in Begin Play, we're currently locating the player
And then we proceed to perform some other action, right?
First, we find the player
and then we calculate the direction towards the player
Then we need to do something else afterward
So, we need to connect another execution pin at the end, right?
If this increases, what will happen?
It will continue with nodes being connected one after another like a chain
As it continues like that, it might become less readable or inconvenient to navigate
So, there's a blueprint node called "Sequence"
Alright, if you right-click and search for "Sequence"
you'll find a node for it
When you add a Sequence node, it looks like this
It determines the order in which actions are executed
So it executes the first action as 0
and once that's finished, it moves on to execute the second action as 1
So, instead of directly finding the player in Begin Play
we'll disconnect and use this Sequence node to execute actions in order
In Begin Play, after connecting to the Sequence node
we link the first task that needs to be performed
Please also delete the connections from this node
Then, connect the second task that needs to be done to "Then 1" on this side
Using the Sequence node allows you to sequentially proceed
with finding the player
and then calculating the direction towards the player
If you want to do more
you can click the "Add Pin" button here
which will add another execution pin so you can connect the next node that should be executed in sequence
If you don't need it, you can right-click
and select "Remove Execution Pin"
to make that pin disappear
So, now we can organize nodes more intuitively using the Sequence node
Let's summarize what we've learned in this session
We've learned that using vector subtraction allows you to determine the direction towards a specific actor
We've learned how to use vector subtraction to continuously follow a specific actor
And executing the functionality to determine the direction towards the actor in the tick node
allows the actor to continuously follow it
We've learned that executing this in the Begin Play node
sets the actor's direction once based on the initial position
and moves in that direction continuously
Vector subtraction Direction Vector: Right Get Actor Right Vector
Forward Vector: Get Actor Forward Vector Left: Right Vector * -1 Backward: Forward Vector * -1
Using vector subtraction to determine the direction towards a specific location
Making a specific actor follow Normalizing vectors Making the vector size 1
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
Determining execution order Sequence node