Hello, this is Yeong-hoon Lee
Today, we are going to try making bullets and enemies
Using the Spawner system and input, we are going to fire the bullet
and learn about how to create the enemy
First, we'll learn about ways to make and move the bullet
Creating and Firing the Bullet
We are going to create the bullet actor and the enemy actor and control them
Let's try making the bullet first
Go to the project
Open the blueprint foler
Right click here
We are going to create a blueprint class
We are going to create actor
We're not controlling it because it's a bullet
It's supposed to travel forward by itself
We are going to create a blueprint class that is based off of actor
We'll name it "BP_Bullet"
Create the Bullet and double click on it
We're going to rotate it a little by using the Alt key
I'll rotate it like this
so that the X faces upward
How I'm going to make the object, is like this
If there's an object, so if there's a player here
The player is moving up down, right and left
I'm going to make an arrow here
And at the location of this arrow, I'm going to make the bullet appear
But the bullet won't be as fat as the player
I want to make a thin bullet that looks like this
I want to make a bullet like this
The bullet will travel forward in this direction
Since I will process it like this
When I look at the bullet, then it should look like this
This part is the front
I'll rotate it so that this would be the forward direction in the bullet's point of view
The bullet would travel towards the front side of itself
So we're going to make the bullet
In BP_Bullet, click the Add button of the components tab
We'll make a cube
Then it looks like this
When you press the Alt key, you can take a look like this
It would originally look like this
We're going to rotate it like this
So that the red arrow points up
And let the green arrow point to the right
We'll rotate it this way
Then we're going to compress this
Let's decrease the scale and make it longer
Press the R button, and you'll be able to adjust the scale
And the green here
Hold the green axis with the left click of the mouse
Decrease it like this
When you look here, the value for green
The Y value will be something like 0.25
Let's shorten the height as well, to about this much
Then the settings should be at 0.75, 0.25, 1
Compile this
We're going to move the bullet forward
It will travel this way, right?
We'll do this job at the Event Graph tab
At Event Graph, if you hold onto right-click and move, it will move like this
Come over here, and since it's going to move
It's not going to move just once after appearing
It has to continue moving
So we're not going to use BeginPlay
We're going to use Event Tick
We're going to be here
And here as well, it's going to move forward
In order to create the forward direction
For forward direction, there's something called Get Actor Forward Vector
This is the vector for the forward direction
Our forward direction
Or since it's forward direction, we can also use Make Vector
There's a difference between these two and we'll also look into this
Then forward means that X will be 1
Enter the value like this
And since the object will move forward
We'll also use the motion formula
Last time, we used the equation P=P0 vt
to move
We'll try moving it using the same method
Let's type it here (in the comment box)
We'll put P=P0 vt
Let me talk about this a little more
Last time, we learned about moving with P=P0 vt
as well as velocity
We'll need more explanation on delta time, or delta second
So we'll continue on from here
When we move, we use the motion equation P=P0 vt
We call this velocity
Velocity is a quantity that has direction and magnitude
This of course, is in vector
These are all vectors
Then what's t?
For time, let's say in animations or
dramas, what happens when we stop time?
All movements stop
Look at this equation
There's direction and magnitude, but if the time is 0
The initial location and the current location becomes equal
Meaning, it doesn't move
So the concept of time is very important
Then we can think of time like this
Let's say that there are two computers
One computer can calculate twice in 1 second
Twice in 1 second
And another computer is twice as more advanced than this
So let's say that it can calculate four times in 1 second
Then let's take these computer A and computer B
And let's say that we'll play a game
We'll play a game of running forward
Then this, calculating twice in 1 second
Every time I draw, if it moves 1 meter
And this also moves 1 meter, then it's pay-to-win, right?
Those who pay, those with better computers will win
This can go 2 meters and this can go 4 meters
Then we need to think about how to make this fair
Then we can do this
This is twice in 1 second, right?
Then let's divide this by 2
This is four times in 1 second
Then divide this by 4
Let's add up now
1/2 is 0.5, so 0.5 and 0.5, right?
This becomes 1
This too, 1/4 is 0.25
0.25
Add up all the 0.25, and you'll get 1
This is the concept
So these values, like 0.5 or 0.25
becomes delta time, or delta second
What effect does this have?
It becomes fair. And let me express this in a more technical way
Our computer doesn't draw out the screen at once
It may be 30 times a second or 60 times
Monitors these days draw much more times, such as 144 times
There are some that draw more
So it's the concept of "how many times to draw in 1 second"
These number of times are called frames
So we call it FPS
Frame Per Second
We call it Frame Per Second
It expresses the number of frames for each second
Then what this is
This is two times for 1 second, so 2 frames
So the FPS for this is 0.5
No, 2 FPS
The bottom one is 4 FPS
This is slow
The computers these days draw out 60 FPS
In the actual unreal, to see what we're drawing
If you look here, you can check the FPS
Press Show FPS, and it says 19 FPS right now
When you play it, it is about 60 FPS
Millisecond is 1/1000 second, so it's about 17/1000 seconds
That was the concept of FPS
So this concept of delta time, or delta second
Is very closely related to FPS
It's the amount of time it takes to draw one frame
So the time between current frame and previous frame
This is delta time, or delta second
So that's the concept
So where we multiply this, is
We usually multiply this by the transform value
There are 3 types of transform
Location, rotation, and scale
For example, if we're making a running game
Then multiply the location by delta second, and it will become fair
But there's a difference, though
When there are more frames, the frames become smoother
For the same 1 second,
If it's 60, it's 60 times per second, so it's smooth
If there are 20 frames,
It's 20 times per second, so it will feel choppy
But the result is the same
Making it fair like this
Is what delta second does
This is how we process it
So taking this, we're going to actually move it
Let's set X to 1, and now we need to make speed
Type Speed here
This is a bullet, so let's make it move fast
About twice as fast as the player. The player is 500, or 5m, so
10m. Let's move it with the value of 1000
And now we just need to multiply the speed here
This is the direction, so multiply
Multiply the speed like this
Then we also need to multiply delta second
Add another pin and multiply right away
Then this part becomes vt
Then go to Get Actor Location
This is P0
P0 plus... We need to set to add
Do P0 vt, and it becomes New Location
Over here, put Set Actor Location
Set it like this, and now it will move forward
Connect them like this
When you follow after me, watch the flow of lines carefully
If you miss something, it will not move properly
Don't forget these connections
Make it like that
Let's see if it actually works. Take out the bullet
It looks like this, and once we play it
It looks like it's staying still
That's because it's moving forward
If you look at the Bullet,
X keeps increasing
But since we have direct projection
Since there's no perception
It looks like it's not moving because the size is the same
So we're going to rotate this
Go to the y axis
Rotate the y axis 90 degrees
Then it will become like this
It looks like there's no change in axis
But that's because it's in global
Click this
And you'll see the red arrow now pointing up
Let's set the location to a little above the character
Let's move up
Huh why isn't it moving
Oh~ Right, of course it doesn't move
Because look
Play it, and the X is moving, right?
We rotated it, right?
But X is moving
That's because we used Make Vector
And said, move in the direction of X
This is like the concept of NSEW
We said, move inward towards the screen, right?
So in the world coordinate, we said move towards the front of the world
That's how we processed it
So regardless of our rotation
It continues to move forward
So instead of this, let's change this
Cut this off, and change it to Get Actor Forward Vector
Like this
Then play it, and you'll see how it moves upward
It moves up
You can see it traveling up
Process it like this
The difference between these two is very important
Make Vector sets the absolute coordinates
So it's the difference between forward direction of the world coordinate
Versus the forward direction in my point of view
This allows various methods of processing
After this, then let's say that
We are trying to rotate this
I'll try to rotate it this way
I'll tilt it this much, toward the right
Then play it, and see what happens
If you look
You can see it moving in the diagonal direction
So whether you're going to move it in the absolute direction
Or move it in the relative direction
Or rotate it instead of moving it, same thing with scaling
There's the absolute concept and the relative concept for all
So it will be good to make use of that
We'll do it this way
We'll use the Actor Forward Vector and make it move
So there's a difference between the world direction and the local direction
Should we leave this here?
Then we'll come back here and delete the bullet
We're not going to locate the bullet here
But there's one thing you need to keep in mind
We'll turn this back like this
Let's say that we located in the same place as the character
If we place this object on the start position of the player
See what happens
The bullet comes out, but the character doesn't appear
This is the caution point. When we use player start
And create the character automatically
There can't be other objects in the place of the player
It has to be out of the way
It has to get out of the way, or place the object
In another location
Here, it says BAD size
Do it like this, and it will properly
Work properly like this
This is one of the common mistakes
So let's process it like this for now
Then, when the character presses the button
We want the bullet to appear when the player left-clicks the mouse
In Unreal, the action of appearing is called Spawn
Then let's try spawning
Delete this first
Save it first
There's the character here, BP_Player
It's here, and at the location of the player
We want to make the bullet appear there
Should we first make it appear once?
How to do this, is first type Spawn here
There's something called Spawn Actor From Class
It's a node called Spawn Actor
Using this, we can place the actor
And locate it on the world level
There's the Class part here
This refers to what class the actor will appear under
What we made is BP_Bullet, and it's here
Let's choose this
Then its name changes as well
It becomes Spawn Actor BP_Bullet
Then connect it like this
Of course, we're not going to do it here
Later, we're going to create an input
And process it every time there's an input
But we'll test it out here first
Then compile it, and there will be an error
That's because the transform of Spawn
Is not properly set, that's what it's saying
It's saying that we didn't enter the information for transform
So we need to fill in the transform information
We can make a new one
Or we can refer to an existing one
We can make it through Make Vector or Make Transform
For example, we need to enter the location and rotation value
If you see how the bullet rotated
When we took out the bullet, we rotated it like this
We took the green part and rotated it to change like this
Then if you see here
We can set the value to 0, 90, 0
We just need to set the y to 90 and rotate it
Then taking this, we can also enter the code manually
Let's try that way first
On the player, we're going to enter a value
If you right-click the mouse on the Spawn Transform of Spawn Actor,
Right-click on it, and we can split it
We're dividing it
Click it, and it divides into 3
To combine again, use the Recombine menu
So when you refer to these
"Huh? It's the same Spawn Actor but the input pin looks different"
That's what you may think but
That's because splitting is possible
These are all available for splitting and you can even go further
Let's Recombine again
I only want to edit the transform and the rotation part
I won't touch the scale
Then the location right now, for this object here
We're going to place it 1m above this object
Then the rotating direction, like how we've done it before
Huh, did it get deleted?
Anyways, we just need to rotate the y part
So if that's how we want to do it
Go to Player Event Graph, and since it's 1m above
My current location, so on Get Actor Location, increase it by 1m
Then we just need to add it by the vector of Get Actor Up Vector
The Up Vector is 1m
The basic length is 1
Since it's 1, we need to multiply this by 100
Multiply.. since this is 1, over here
If we want to change it to Float, right-click here
Right-click here and choose To Float
Change it, and it will change like this
Then multiply this by 100 and it will become 1m
But it's 1m in the upward direction
Add this and this together and it will become the new location
Add, then connect it like this
Rotation is about 90
Should we do it like this?
Then compile it, and there's no error anymore
And play it, and a bullet
Shoots out like this, right?
But if we set the coordinate every time
Through this calculating method, it will be pretty difficult, right?
We might make mistakes too, so rather than this method
This is an option, it's not mandatory
Some may be more used to this calculation method
Rather than this
It's better to approach in a visual form
We'll Recombine this
On the component, we'll add an Arrow
Add an Arrow
Then when you go to Viewport, there's an arrow here
The Arrow usually faces forward
See the red arrow?
Should we move it up a little?
It looks like this
We'll rotate the y 90 degrees
Then it becomes like this
We can make an arrow like this
Locate the arrow at a good location
Like this
Compile it, then come over here
Take the Arrow and bring it here
And when we search Get Transform on the Arrow, many things pop up
There's Relative Transform here
There's Socket Transform, etc
See the World Transform?
We're going to use this
Bring the World Transform and insert it right away
Then after this, compile it
Here, there's Collision Handling Override
It's in Default, but open it up
Always Spawn, Ignore Collisions
Try to adjust location, but always spawn
There are options like this
There's also try to adjust location, but don't spawn if still colliding
You can try them out
But it doesn't always work out
So most of the time, just use
Always Spawn, Ignore Collision as the best option
We're trying to spawn anyways
Let's set it like this
Then play it, and it shoots out like this again
It shoots out this way
We don't need to set the direction
The direction of the arrow is already rotated
The direction and coordinates of this
When the bullet is made
The coordinates get entered automatically
That's a simple way to set it up
Then let's change it so that we can fire it with a button
Go to Project Settings, and Input
It's here
We're performing an action this time
When we perform the action of left-clicking the mouse
The bullet is going to appear like this
Once the bullet appears, it travels toward on its own
So we're going to create action, and axis is not an action
You have to go to Action
So we're going to add an action mapping
Click the plus sign, and add it like this
We're going to name it Fire
And open this
The key value will be Left Mouse Button
You can set your own key
I chose Left Mouse Button
You can open this up
And set it as Left Mouse
Or you can press it yourself
We'll set it like this
Then it's the same method
Go to Player
We're going to disconnect this because it's not when it appears
Disconnect it
We created the action called Fire
Then type Fire here
There's Fire in the Action Event
This appears here because we typed it as Fire
If we put FireBullet instead of Fire
Search it here, and it changes to FireBullet, right?
The name that we type appears as is
We'll change it back to Fire
It will save automatically
For this
If you see here, Content
Right-click and click Show in Explorer
You can see the window here
Go back one folder
There's a folder called config
And these contents are in the Defaultinput folder
Shall we see?
If you see here
"Fire" appears here
The things we entered are here
On AxisMapping as well, there's Horizontal and Vertical
They're entered here, right?
So for Project Setting, even if we don't save it ourselves
It becomes a file on its own
We don't need to save it
It saves automatically
That's how it is
Come back to play and create the Fire Event
Put Fire
For Action, there's the Pressed and Released concept
It's the concept of pressed or released event
So we're going to choose if it will happen when we press or when we release
If we use this, we can make automatic firing
And things like that
Should we do it like this first?
Pressed this way
Like this, and compile
Save it, and if we left-click
The bullet fires as we press
If you don't like the location of where the bullet is firing
We'll edit it a little
Change the location of the arrow a little bit
You can move it down a little
So that's how we can fire the bullet
This time, we're going to create the enemy
Creating the enemy works the same way as the bullet
To make the Enemy, we'll make another blueprint
On the blueprint class, we're going to make BP_Actor, BP_Enemy
Right-click the mouse, and go to blueprint class
Then make the actor
We'll name it BP_Enemy
We're going to make the enemy
Double-click, and place the enemy
The enemy will also move forward
Let's move forward first
We need speed for this as well
Enter Speed, and set it to Float
It will be too difficult if the enemy is faster than the character
So we'll make it the same speed as the character
Enter the value of 500
Then come to Event Graph and we'll move it
Let's make the Enemy move forward first
It's exactly the same as the bullet, right?
It will move in the same method
The bullet moved this way
So the Enemy will be the same
Then for this as well
Use the equation P=P0 vt
For the enemy as well, we'll move forward
Not down, but forward
We'll rotate it like this
Later, we're not going to do it right now
Later, we can move it forward with C
Then make a creator so that it will appear
In certain intervals
Just like how the character makes the bullet
Considering those, we'll also make this move forward
We'll produce it that way
We need to make direction first
Forward means Get Vector Forward Vector
Do this, and we can obtain the forward direction
Right, Forward, Up
These are all obtained through Forward, Right, Up here
And we're going to move this
On v, we need to multiply speed onto direction
Multiply, and make two pins
Make one first, set it to Float, then we'll add another pin
Enter Speed as is
Then we're going to enter delta second here. You see the Tick here?
I'm going to set it aside
On Event Tick, I'll move it like this
Now we made v. We made vt
Then we need to bring it
Go to Get Actor Location, then bring P0
It's the same method
Huh? Do we not Normalize here?
Why? The length of the forward vector is 1
It's an already normalized vector with the length of 1
You don't need to normalize
So add these together like this, right?
Do or Add, then put Set Vector Location
Enter this value into New Location
Then of course, we need to connect the pins
That's how it will move
Compile it
Should we place it now?
We're going to drag it and place it here
Oh I didn't make its outer look
Let's make the outer look of the enemy
We'll make it into a box
Click Add, and add Cube
We'll use the enemy as is
Compile, and now you can see the enemy
Set the location. Should we place it above?
Play it, and it looks like this
We didn't rotate it, so it's probably moving forward
The character is too centered
Let's place the character a little lower
We'll lower the Player Start
Raise this a little, and now we need to rotate it
It's moving forward, so rotate it this way
Like this
I think that's good
Z is upward, and since this is right, this part will become the front
Let's move it to the side a little
Play it, and you can see it moving forward
Then let's make it so that
It moves toward the direction of the character
Let me explain vector a little more
We added vectors before, but now
Let's look into subtraction of vectors
For example, there's a vector that looks like this
Let's call it vector A
Then there's a vector that looks like this
We'll call it vector B
Then what we want to do
The end of this vector is ultimately its location
We want to make a vector that looks like this
That's how B will move toward A
So that's what we'll talk about
If we call this vector C
Vector C is A subtracted by B
Then we can make vector C
These are all vectors
If we change the sign of vectors, it becomes the opposite direction
What is means, is that
If we want to make a vector that looks like this
This will be -vector B
Then should we add this and this together?
If we add these two
This is the clone of -vector B
And a clone of vector A
This vector will be created
If you look here, these two vectors are the same
This is possible because
This works for vectors
It's the same thing as Vector A plus negative Vector B
Subtracting a vector is the same thing as
Adding a negative vector
This is the same as vector C
We can do this
To put it in an easier way
If vector A is our target
Target subtracted by my location is
Gives vector that looks like this
So the subtraction of vectors is usually used
To determine the direction that I want to face
Using this, let's say that the enemy is here
And the character is here, and we want a vector that looks like this
And for this vector
The length of the vector may not be 1
It may be far
Let's say the distance between these two objects is 100 m
Then the magnitude of this vector will be 100 m
Then after this step
We have to normalize it
Using this concept, we'll move it towards the character
Let's try it
Go to Enemy, and go to Event Graph
Then the direction is this
We're going to set the direction of when it appears
Set the direction at BeginPlay
And on Tick, we want to make it continue moving in that direction
Then these two events can't connect, right?
So we need to understand these two as separate spaces
We want to share the information between two separate spaces
We can do this by creating a variable
Create a variable, enter the value for that variable at BeginPlay
Then use that variable value on Tick
That way, we can share the same information between two spaces
So we'll make a variable here
We're going to set it to Vector
Its name is Direction
If we carry on the existing one
We just need to set the Direction
And bring this as is
Disconnect this
This way, the Actor Forward Vector is set
Then it can move towards the direction of Direction
So as long as we just change this Direction
It will move in that direction
So separate it, then bring it
What we want to do, is this
Let's try probability
So 30% of the time, go towards the player
And for the rest, go forward
We want to process it like this
Then these will become the other percentage
Then it will be 70%. 30% and 70%
Separate them like this
Should we place them on top of each other?
Then we need to make probability
How to we make probabilities?
We usually think of dices for probability
Let's say there's a dice like this. A cube
It has six sides
If there's 1 here, there's a 5 on the back
If there's a 2 here, the bottom is 4, is it right?
Is it 1 and 6? 7?
There's 6 across from 1, 5 across from 2
This is how it goes
What the probability of rolling a 1?
It's 1/6 chance, because it's one side out of six
Then the probability of rolling a 2 is also 1/6 chance
2 also takes up only one side
This is probability
Then what's the probability of rolling less than 2?
Probability of less than 2?
Then it becomes 2/6 chance
Oh is it 1/6?
Then should we say less than 3?
Probability of less than 3 is 2/6
Because there are two: 1 and 2
Then it reduces to 1/3
We're going to make probability like this, and it's going to be 30%
What does this mean?
30 times out of 100
So probability has a range
For example, if there's 0 to 100, then 99 will be the range
We can also say 1 to 100
But 0 to 29 is 30 numbers, right?
Less than 30, right?
When one is pulled out from 100 of them
The probability of it being less than 30 is 30%
That's how we're going to process it
Then we need to concept of random, right?
Random Range
Is it Random Range? Like this?
If you see here, there's Integer and there's Float
Float is usually 0 to 1, right?
Something like 0.1
This is used for those
But Integer is used often as well
Let's try Integer
For integer, 0 to 99 right?
Hover the mouse on top
And if you look at Min and Max
Greater than or equal to Min, Less than or equal to Max
Then that means it's including Min and Max values
Then we can just set it from 0 to 99, right?
Then it becomes 100 of them
Then if it's less than or greater than
Then it will be like this, right?
For greater than 70, it will be 71 to 99, right?
Or 70 to 99, right?
That will work, but isn't it easier to start from the minimum?
So less than 30 is 0 to 29, so there's 30 of them
We can make probability like this
So if it's less than 30, then Branch
Or there's also a shortcut key
Press B and click, then Branch appears
Should we use shortcuts?
Then this needs to be pulled over as well
Then now we made probability
True for 30% of the time, False for the rest
These are the other percentage, so these will become the False part
Then for True
Earlier, we made direction by doing target subtracted by v
So find the character
Subtract my location from the location of the character
Create the direction, normalize it
Then enter this into direction
Let's try that
This is 30% of the time, towards the player
We need to find the player first
There are various ways
We can do this
Normally, when looking for Actors on the level
There's a node called Get Actor of Class
Using this node, our character is BP_Player
We can find it like this
This is one method
Or we can use Possess
The player is possessing the pawn, right?
For those instances, we can look for it through Get Player Pawn
The Pawn that player 0 is possessing
You can use either of those methods
I'm going to try this
I can use this, but I'll use this for now
You can choose either one
Bring the Pawn that player 0 is possessing
And bring its location through Get Actor Location
Then this is Vector right?
The Return Value is at Vector
So we can obtain the world coordinates of this Actor
World location
Then we also need to bring My Location
Use the Get Actor Location as well
Then the name is the same
But the target is different
Self is mine
The other target is this, right?
So you have to watch for the target
So target subtracted by v
Subtract, and subtract it like this
Then this becomes the direction that I'll face
But as mentioned earlier
My, and by me, I mean the Enemy
If the distance of the Enemy is greater than 1
Then the size of this vector becomes greater than 1
So change the value to 1 through normalizing
We'll make it into a normalized vector
So Normalize
Make the Normalize Vector
This is the error range
These are all ultimately Float types
If you have time, search up Floating point operation
There are rules for how the computer performs Floating point operations
There are some errors for this
And we're talking about the error range for this
So for example, it's like this
1 is the same as 0.9999
That much of an error is allowed
So make the direction, and set this direction
Go to Direction, and Set
So when it's True, it connects this way
So randomly, it either goes toward the character or forward
That's how it will move
Compile it, and let's see the results
We need to try multiple times because it's a 30% chance
It comes this way once, or it goes forward like this
Using Random like this
We've learned that we can create probability
Like this, we created the bullet and enemy actors
Let's summarize the content we learned today
Creating and firing the bullet Creating and moving the bullet Create a new blueprint file BP_Bullet using Actor class as the parent class
Use the Get Actor Forward Vector node to make the bullet to fire towards the forward direction Create the Speed variable of Float to set the speed of the bullet
Use the transform equation P=P0 vt to move Bullet Firing Input Key Binding Add Action Mappings on the Project Setting Input Tab then create the InputAction Fire node in the graph panel
To create the bullet with a key, connect the Spawn Actor From Class node
Add the Arrow component on the Components panel to set the location and direction of the bullet Connect the world transform of the Arrow component to the transform of the bullet
Creating the enemy Enemy creation Create a new blueprint file BP_Enemy that uses Actor class as the parent class Connect the node so that it move toward the forward direction
Moving the enemy Use the same method as moving the bullet
To move it towards the player, obtain the direction vector by subtracting the enemy vector from the player vector then connect the normalized direction vector of the enemy to Direction