Hello, my name is Hyunjin Kim and I will be teaching the basics of Unreal Engine for implementing immersive contents
In this lesson, we will learn about vectors and its addition
How to receive user input
and learn how to make the actor move with the received input
Vector's Concept and Addition
Let's learn about the vectors and addition of vectors
First, we will talk about what a vector is
A vector is something that has a direction and a size
Let's say we have a car, like this
The car moves towards the right
Let's say it moved in 100km/h speed
What I just described is the vector
It has a size of 100km/h
and direction of towards the right
We call this speed
So vector is anything that expresses direction and size together
The weather forecast mentions both the wind direction and wind speed
The southwest wind is blowing
at a speed of 20km/h
This also represents a vector
We use the arrow
to express vectors as symbols
the length of this tail tells us the size
and where the arrow points tells us the direction
There is something that only has the size and not the direction
That is called a scalar
We used a car as an example
This car drives in 100km/h
to where?
to the right
The two of these combined
is a vector
But what do we call this value that only represents this number shown here?
This is what we call scalar
So y도
speed and velocity in school
Scalar represents the speed value
while velocity has both speed and direction
the addition of vector means the addition of all the powers
A person throws a ball
with a speed of 10m/s to the top right
This itself is a vector
because it has speed and direction
Would this ball reach the sky?
No, it moves parabolically like this
Why is that?
Because the ball floats up
with the force of throwing
but the power of gravity is added
and it keeps going down
in a parabolic move
these kind of calculations can be done with the vector addition
The wind could be blowing this way too
so this curve
could fall steeply more and more
In this case the throw-power, gravity, and wind power
are all added for this effect
I will illustrate how the sum of vectors works
We have a graph like this
We will call this x and this y
I will draw a vector on this plane
a vector starting from the origin
and another vector from the origin going this way
The red vector will be v1
and the blue one will be v2
I will illustrate how these two vectors add up
First, duplicate this red vector
and place it at the end of the blue one
Again, copy the blue vector and
place it on top of the red
Then, the blue and the red will meet at the end
So if you draw a vector
from the origin to the meeting point
this will be the addition of vector v1 and v2
If you memorize this drawing
You will be able to recognize when two vectors are added
So what we are trying to do with this addition
is by using the keyboard's W, A, S, and D key
we will move an actor
We will make it move right with the D key
move left with the A key
move forward with the W key
and move back with the S key
But what should happen when W and D is pressed together?
It should move diagonally
So the direction of movement is based on
the addition of the W and D key's vectors
when the keys are pressed
So basically the addition of vector going forward and to the right
So I've said that
we would connect these meeting point
Then when the two vectors are added
we get a diagonal facing right
So we will move the actor in this way
Receiving User Input
Let's learn about receiving the user's input
I will make a new level
Click File - NewLevel - Basic - Create
click the save button on the top left
I will make a folder under the Contents folder
Right click and New folder
Let's name it Actual Practice
So double click Actual Practice
I will name this level the Actual level
Save
Now select the Actual Practice folder in the Contents browser
I will make a blueprint class
Right click
Blueprint Class
and we will inherit a Pawn
Pawn is a class created by inheriting actor
But this Pawn
is a Blueprint class that is ready to receive user input
and because we have to receive user input now
we will inherit this class called Pawn and make the Blueprint
Select it
and name it BP_Player
Double click to open
When we get a separate window like this
assemble it as a tab at the top
There is the Component window on the left
This is a space that shows
what components this actor and pawn consist of
Actors are made of components
So let's add a component here that represents appearance
Click the Add button
Write Static on the search bar
and at the end there is the Static Mesh
Click this component to add
Now you see we have a component added
Select this component
If you look at the detail window on the right
There is Static Mesh
If you press the clickable button here
Here are all the appearance meshes that can be set
Now search 'Cube' at the search bar
You will see 2 cubes
When you hover over
select the one with
'Approx size 100x100x100' in the details
If you don't see such cube
click the gear button to the right of the search bar
Check the menu called 'Show Engine Content'
Then you will see the list
Now search Cube again
and choose 'Approx size 100x100x100'
Then you can see what the actor looks like
on a window called Viewport
Let's try Compile
You will see a list called Materials
This is a space where
you can add color or designs to the exterior
Click the drop down here
and you will see all the possible settings
Choose the one you want
I will choose Arrow Material
You may choose anything to your liking
Let's shift this to the right
To do a movement
You can implement it through a blueprint node using the Event Graph tab at the top
Right click and search 'Get Actor Location'
and a list pops up
Click it to add a node
This node tells us the Actor's location
Now click right again to add a node called 'Get Actor Right Vector'
This
The direction this actor is facing is in red
The upward direction is this blue
and the right direction is green
So the direction indicated by this green color is a node that indicates the vector
It tells you what direction value the right side of this actor is
Let's drag this
and if you search X, then you will find Multipy
add this node
On the Variables under My Blueprint on the left side
Click the plus button to increase a variable
Plus
The variable's name will be Speed
Then drop down this data type and make it float
I will click the Compile button and set
Speed's default to 400
Then drag the speed variable
to Get Speed
I'm going to multiply that value by this vector on the right
Then drag the right vector multiplied by the Speed ​​value again
Add another multiply node
I will move this node to the top for a second
Now drag out the pin on Delta Seconds in the Event Tick
and connect it to the second multiplier input pin
Multiply these two values ​​and drag again
this time adding an Add Node
and then add this Get Actor Location
which is the current location
and right click on a blank to search
Set Actor Location and click that
it gives you this node
this has an Execution Pin
So connect the execution pin from the Even tick
to the Set Actor Location node
In the Input Pin called New Location
connect the final result
calculated so far
Now we click Compile
Now move to the Actual level
drag the BP_Player we made earlier
and place it on the View Port
For the location of the transform of the detail
we will set it to 300, 0, 0
300 and 0 and 0
now if you play it
you can see a cube moving to the right
We've made the actor move
So the formula to make the actor move is
P = P0 + VT
P is the final location value
P0 is my current location
V means velocity
T is for time
So the result value is P
Get Actor Location is P0
Get Actor Right Vector and the speed
equals V
and the delta time is T
So I multiplied V and T
and to there
I added P0 to get the final P
which tells what the actor should do
When you want to move something using unreal
you will be using this formula the most
Let me make some distinctions with Comments
Select the product node of Get Actor Right, speed and delta time
and press C
This means VT
then select the Get Actor Location
separately, press C and it means P0
Select the sum node of VT and P0 and press C
The value here will be P
So the result value tells the actor to set a new location
When you want to leave a comment for a node
you can use this Comment function
but if you hover over the node
there is a icon that looks like ...
If you click that, you can write descriptions of this node
P means result value
So use these features wisely to keep the nodes organized
As of now, the actor automatically moves to the right
Now let’s move it by receiving user and keyboard input
I will go to Actual level to make another file
Right click
Under Input, you will find Input Mapping Context
add this file
we will name it input mapping
we will add another file now
right click
not choose Input Action under the Input category
I will name it IA_AD
double click IA_AD to open it
click the drop down where the value type is digital
and change it to Axis1D float
now click the save button
Comeback to the Actual level and open the Input Mapping file
Click the plus button on Mappings
then if you click 'none'
there are files regarding the input action
We only made one
so we can see this IA_AD
Select this file
When you click the button to expand
you can see which keyboards you can set
We will first register the A key
If you click this keyboard looking button
it turns yellow like this
If you press the actual keyboard now, it will be registered
I will press A
Now click the expand button again
You can see Modifiers here
click the plus button again here
click where it says 'none'
and select Negate
We've pressed the plus button on IA_AD,
I will do that again
you will see the keyboard again
Click the keyboard icon and we will press D now
What happens now
When you press the A key, you will get a value of -1
When you press D, you will get a value of 1
Insert Negate on the a value
If you don't do this
You will get a value of 1 when A key is pressed
The Negate here
sets i so that you get the opposite value
Now we go back to BP Player
We will make the initial settings on this Begin Play node
so that it can receive and process the input
Right click on a blank
and search Enhanced
Under Controller and Local Player Sub-system
You will see Get Enhanced Input Local Player Sub System
Select that to add a node
Right click on a blank again
Search Get Player Control
There is a Get Player Control node
Click it to add the node
So we connect the value from Get Player Control
to Enhanced Input Local Player Sub System
And for the value of Enhanced Input Local Player Sub System
If you search Add Mapping here
There will be Add Mapping Context
Add this node
and connect it to the Begin Play node's execution pin
This is the input's initial setting
If you look at Add Mapping Context node
You will find Mapping Context
If you click the Select Asset button
You will see the input mapping that we've made
Select that file
We've added settings for
the A and D key in this input mapping
Now we are saying that we will be using those input
Let's move to the bottom to actually try receiving this input
right click
and the action we've made
which is the IA_AD
So if you search IA_AD we see these two things
We will add the bottom one, Enhanced Action Value
What the value of this is
The value will be -1 when you press A
and it will be 1 when you press D
If you don't press any
this node will give you 0 as the value
We can use this node by creating IA_AD
So let's put it to the test
Right click and search Print String
Take the return value of IA_AD and drag it to the In String
and we need to connect it to the execution pin
Connect the execution pin on the Event tick
to the Print String
Press Compile and we will move on to Actual Level
If you press the play button and the A key
only the screen moves
and the output value on the left is only 0
This is because our player, Pawn
is not receiving the inputs
Right now, the default input settings are being forced on the Pawn
So we need to configure here once more
so the Pawn receives the new input
Select the BP Player
Go to Details and under Pawn
there is Auto Possess Player
It is set to Disabled
choose Player 0 from the drop down menu
if you play now
What happens when we press the A key?
we see the value as -1
and 0 when you are not pressing
and 1 when you press D key
with these values
we can make movements such as going to the left or right, or staying still
We will stop
Come back to the event graph of BP Player
We will make direction of movement using Get IA_AD
To have it simply move to the right
This right here is the setting for that
This says move to the right with a certain speed
We will change this part a bit
Erase this yellow connecting line from Get actor right vector
Press Alt and click
Erase some of the prints here and there
We will only leave this Get IA_AD
I will multiply these two vectors
and insert the product here
It would've been a right vector
but this time the right vector will be
multiplied by a value of -1, 0, or 1
Let's see what happens
Compile
Let's go to Actual level to press play
Press the A, D key to see if it moves
It does not
Go back to the blueprint
this Select Location Execution pin was not connected
Only the data nodes were connected
What actually executes it was not connected so
We will connect Event pin's Execution pin to the Select Location
Compile again
Go back to Actual level
Press play
Now when you press A it moves like this
and when you press D
you can see how it moves
Now let's move back and forth using W and S key
We need to make another action
Right click and add the Input action
I will name it IA_WS
Double click IA_WS
Set the value type to Axis1d from the drop down
Now go to input mapping
Click the plus button
From the drop down menu where 'none' is
select IA_WS
Expand it
Click the keyboard icon and set the W key
Now we add another one in IA_WS
Click the plus buton on the right of IA_WS
click the keyboard icon and click S
Now when you press W, we will get a value of 1
and when S is pressed, you get a value of -1
Keep the W as is
on the S's modifiers
add negate
Press the plus button on Modifiers
expand on none
and select negate here
save now
move back to BP Player
and we will expand this part
right click on a blank space
and search Get Actor Forward
There is Get Actor Forward Vector
Add this node
Right click again
search IA_WS
and we will use the enhanced action value on the bottom
we will multiply it here
Drag the returned pin in get actor forward
and add a multipy node
multiply the Get IA_WS value
and then we will add the value of these two
For now, disconnect the product of the right vector and IA_AD from connecting here
drag it out and then
add an add node
then add the product of Forward vector and IA_WS
To this value, multiply the speed
Compile, then go to actual level
When you press play, now you can see it moves forwards and backwards
left and right
The camera is too close right now
so set the camera way back here
and we'll watch how the player moves
In the search section of place actors
search camera, and you will see a camera actor
Drag this actor and place it here
The camera view screen appears at the bottom
but we can't look at the player
So right click here
and we can control the viewport camera like this using WASD keys
so select the view like this
We can set the camera to show
what we are seeing in the screen below
Go to outliner and see if camera actor is selected
click right on camera actor
and choose snap object to view
then the camera switches to the position of the viewport
What we are seeing on the bottom right corner
we can see from the camera
Now use the mouse to rotate
You can see the camera has moved here
to make the camera actor move
There is auto player activation under the details window
this is disabled right now
choose player 0 from the drop down menu
If you play it now
We can now watch the players from a distance
If you move the WASD key in this state
We can not watch the player
from above
The product of
the Get actor right vector we made here
and the values that come in through input
is the final direction the player moves in
Let's organize the comments
Select this node
press c to make a comment inside
I will write that this is the
final direction
This is hard to distinguish
If you select the space above the comment
There is Comment Color under details
Click this
and you can change the color of the comment
Let me explain this further
When we have an axis
Let's say we have a player here
Which is this far from the origin
This location is in a vector so it also has a direction
This is the player
This is the player's right direction
If you bring this to the origin, it is the same vector
If the vectors are here like this, they are all converging to the origin
So the size and direction is all the same
and these mean the same vector
So when these are set apart like this
they are basically all gathering to the center
So this will be the Get Actor Right vector
If you multiply numbers to the vector
the direction does not change, only the length does
So we simply took the right vetor or the forward vector
and multiplied it
So the directions do not change
directions will change when there is addition
If you multiply something like speed
or the input values from the keyboard
and if you multiply the time
only the size will get bigger or smaller but the direction will stay the same
However, if you multiply a value of -1
you will get the opposite direction
We get -1 when we press the A key
That is why we go to the left
We go to the right when D is pressed
Let's say we pressed D and got a right vector
then P = P0 + VT
This part is P0
Let's say the VT is this vector here
We will add the P0 and VT
The I said it would take it as is
Copy this as is
So what is the meeting point of these?
This will be the P
The meeting point here will be the new location
This is the new location vector
This will be P
So the player will move here, to the right
What would happen if this action is repeated?
If we add the right vector
to this blue vector, we get this location
we move here again and again
Right now the player is moving
by the sum of the vectors
Here is another example
Let me draw out what would happen
when we press W and A together
Draw an axis like this
The player is here
which would have such a location vector
We pressed A and D key
then we would have a left vector and a top vector
We will add these vectors
What do we get?
We will have a vector from the origin to this point
This is the direction of movement
we get the VT
So we move by adding
the P0 valuen and VT which is made by
pressing W and D
If we draw the addition formula
The red would go like this
and if we connect the meeting point to the origin
This is what we get
What is this'?
This will be our P
Resulting in the player moving in this diagonal direction
These processes continue to take place very quickly
which makes the player actor move
Internally, it is made up of the sum of vectors
It may come around as difficult, but I hope you take your time in understanding
Let's wrap up what we've learned today
We learned that vectors are composed of direction and speed
We learned how to obtain the direction the actor should move by receiving user input
We also learned how to calculate the next position of an actor using vector addition
Vector Concept and Vector Addition Vector: something that has direction and velocity Scalar: having size Sum of vectors: sum of all forces
Actor Movement Formula: P = P0 + VT P: final location value P0: current location V: velocity T: time
Receiving User Input Using keyboards WASD keys to move the actor