Hello, this is Younghoon Lee
This session, we'll work on creating an Enemy
And handling multiple spawns
In this chapter, we'll learn how to create an enemy actor in C++
And how to make it move in random directions
We'll also learn how to use
An enemy spawner to periodically spawn enemy actors
Creating Enemies and Setting Direction
To make an enemy actor
Let's make enemy class with C++ first
Select C++ class from tools
Here, select the actor
Click Next
And right here, Enemy Vector
We will create enemy spawn with direction upon creation
It may go forward
Or for like 30% chance
Making it head to the player right away
That's the kind of the enemy we want
Let's start by creating an enemy that moves forward
If you do this, the class will be created in C++
Called Enemy Actor
Then we need to handle the appearance
And collision object for the Enemy Actor
Go down to the bottom
Start with public
Then, UPROPERTY
EditAnywhere for now
After this
We need the collision object and the appearance
So let's do a forward declaration
And then collision object
Write UBoxComponent
And then write code for appearance as well
With Static Mesh Component
Static Mesh Component
And then, MeshComp
What we want to do is this
Decide the direction to move to when spawned
While it lives, it will want to move to certain direction
This is What we want
For spawning
We have a function called BeginPlay
And then there is Tick
With these two functions
Enemy spawns at Begin Play
Tick will handle while lives part
So, we are using the same concept with different functions
We now need an attribute of directrion
This one starts with UPROPERTY as well
We don't have to do this
But You may want to see it
Then let me put EditAnywhere
FVector
And then direction
We don't have to write this
But if we have it
We will be able to see it in blueprint
That's it
Let's go to C++ source file
And now here, we decide the direction when spawned
While it lives, it will want to keep moving
That's how I want to implement this
We need to work on appearance and collision object as well
I want to create collision object component
And place it in the root
And create appearance component
Attach it to the root
This will do the job
Let's make it happen one by one
First, let's work on collision object component
BoxComp
CreateDefaultSuboject
Do you remember now?
Write it like this
This one is UBoxComponent
Write the name here
TEXT
And put BoxComp here as well
I've mentioned this before
The name that goes in here
Feel free to write however you are comfortable
However, there can't be two of the same name
Just keep that in mind
I will add BoxComp to the root component in SetRootComponent
The reason for the error is
Because the header wasn't included
Therefore, let's add the header file
So, I'll mention this again now
I am using Visual Assist
That's why it appears this way
You all can type it out yourselves
So we have these two
You can type like this, Component and BoxComponent
Let's make the appearance
Make MeshComp first
Write CreateDefaultSubobject
And UStaticMeshComponent here
Write it like this
And then it must be attached to Root
MeshComponent will be
Attached to Root
Through function called SetupAttachment
RootComponent
Or you can write BoxComp
Place them like this
Go to the bottom
Now let's make it move
We will need direction in order to move
Write Direction
I will call it foward
Direction is GetActor and Forward Verctor
Write it like this
If you are going to move things
You need to set the direction, size and speed
We haven't made the speed
Let's go this way and create Speed as well
Write float Speed
Since it's an Enemy, let's set it to 500
Likewise, we'll give it the UPROPERTY attribute
It's okay not to give this one
In this structure
However, I wrote this down
Was for me to check
This could be modified by the director
Therefore, that's why I wrote it
It means that it can now be seen from the outside
That's that and come back
Now let's move
We've already made movement formula multiple times now
P equals P0 plus Vt
P0 to FVector
P0 is current location, GetActorLocation
Like so
And then make Velocity
Direction and Speed are already prepared here
So, up to Vt, you just need to multiply these three together
Now, I will use SetActorLocation here
Like this P0 plus Velocity
We don't have Velocity now
Direction times Speed
That will give us Velocity
I need to capitalize it
It's my habit to use small letter
Let's capitalize it
You can use the Rename function
You can change it all at once
And we need to multiply this by delta time
I'll set it like this
I'll turn this off for now
Now it will make it go forward
Save all for now
The name here also changed
Save all
Let's compile it
Wait till compiling finishes
Now let's make the enemy
For the Enemy actor
Go find C++ header
Make the Enemy Actor into blueprint
And place it here
Create Blueprint Class
Give the blueprint folder, suffix BP
Now I've made blueprint class
Then, we will put the same cube in the mesh
Let's repeat the same process
Here, find Basic Shape Material
Like so
Let's see if it moves forward
What's the value of Speed
The Enemy actor is set to 500
You can see the direction here like this
That's how we check this
It might be hard to tell like this
But anyway, this is how we bring out the Enemy
Let's place it in the center
Since the Enemy moves forward
It will move into the center
So now, I'm going to rotate it a bit
Rotate the Enemy like this
And make it face downward
It looks like the top is facing inward on the screen
Make it face towards us
It's normal to make it face towards us
Because it's correct for the right side to move this way
This is how it should be
So I will rotate it like this
Just like this
Set the location by 180, minus 90 and 0
I think we can handle it like this
So, after doing it like this and moving it slightly aside
you can see it going straight ahead when you run it
So that was how to mak an enemy
Let's make the enemy move randomly then
Find the Player
Let's create it so that it moves in the direction of play
Move to the Enemy
You can do this task in BeginPlay
We will decide the direction
Like this
We will make it random
With 30% chance
Toward the player
And remaining percentage of chance
To bee moving forward
I want to handle it like this
What we previously worked on will be the forward direction
Then we need to create a 30% probability
When we implemented this in the blueprint
We used randomness
To create the percentage
So let's work on it the same way we did
If you look there is FMath
This one also start with F
The structure called FMath
Although it's technically a structure, we learned it as a class
There's not much difference between a structure and a class
Basically, they have the same structure
It's just that a structure is public by default
While a class is private by default
That's the main difference between them
If you look at FMath
You'll find something
Called RandRange
You can use this function
If you want to see this again
Place your mouse cursor over it like this
Then press Shift and Ctrl simultaneously, and then press the spacebar
This will allow you to view it again
I see there are four overloads now
One of them is for doubles
Then there's one for floats and various other types
There's Int32
In Unreal Engine we use Int32
By specifying the numbers 64 and 36
You can consider this as defining the size
This is included here as well
It includes both the min and max values
So, if you want to make it 100%
you would use a range from 0 to 99, which would give you 100 count
Or you could use a range from 0 to 9
Then, if you have 3 out of 10, that would be 30%
Let's write it like this
Let's say 99
By doing this, it will return a random number
within the range from 0 to 99
It could be 0, or it could be 99
And it could also be any value in between
So let's go ahead and take this
I'll receive it as an Int32
I will say randValue
I have received a random value
If this random value is smaller than 30
It becomes 30%
Since it is30 out of 100 right?
So, if randValue is less than 30
This would represent 30%
And if it's else, it covers the remaining probability
Like this
The ActorForwardVector and similar vectors
Have a magnitude of 1
The Length is 1
So, there's no need to normalize it separately
So, what we have to do here is
We need to find the player and since it's enemy
We find the destination
Want to find the destination
And them, want to find the direction of the destination
And then Normalize
I want to normalize the vector
So, we need to complete these three tasks
So, when wanting to locate the destination
Do you remember? Let's take a look
If we briefly look at the enemy
When trying to find the protagonist
There are two things we can do
Broadly speaking
One option was a node called GetPlayerPawn
You can use this or
Or, we have GetActorOfClass
Additionally, there's also AllActorsOfClass
You can find all of them if needed
Or you can find only one
This is because the protagonist exists only once in this level
So, we can use this method to find the actor if there's only one in the level
However, if there are multiple actors in the level
That we want to find
Then this method won't work for finding just one
In that case, you won’t know what will be returned
It might not be what we want
So, you need to distinguish between these options and use them appropriately
Right now, we can either do this or try that
Let's go ahead and try both of these
First, let's try this one
You might find
This quite challenging at first
One useful hint is
To start by implementing it in Blueprint
And then move it to C++; this can make things easier
You can double click this
Then, you will see a symbol like this
This will also appear if you double click it
There are some things that appear like this, and some that do not
When the symbol appears like this, it makes the task easier
Functions like this exist in UGameplayStatics
If we use this function
We can apply the same functionality in C++
Let's go through each one of these
Let's start with GetPlayerPawn then
The destination
Would be the protagonist
Since this is a function from UGameplayStatics
We'll need to include the corresponding header, of course
Let's search for this
If you search for this, you'll find that the header is like this
Kismet's GameplayStatics
Now, please add this here as shown
This is the simplest way to find it
Once you get used to it and memorize it
You can type it out directly
Also, using tools or extensions
Like the ones I use can be very helpful
You can use the Visual Assistant
By pressing Shift Alt Q.
If you look in the Extensions Manager
You will see two options like this
There’s also something called ReSharper
And using this program can make things more convenient
You don't necessarily need to manually add it here
As it is automatically included
You can choose to do so if you prefer
I'll go ahead and do it this way
I'll start by basing it on what we don't use
Like so
Then, it will be ready to use
The function, GetPlayerPawn
if you open the parentheses
Takes two parameters here
The function takes WorldContext
And Index as parameters
If you're developing in standalone mode
For a local play app rather than a networked one
You can just use 0 for the index
The local player is player 0
So, like this
Use GetWorld
And provide 0 like this, you can retrieve it
As you can see, the return type is APawn
It returns a pointer to an APawn
So, as you can see, we have found the player pawn
Additionally, there is APlayerPawn and also characters
Do you remember
We talked about this before?
We have an actor
And there are also inherited pawns
And there are also characters that inherit from it
It's set up like this
Fundamentally, both of these
We can manipulate them
They can receive input
So, there are two types
Depending on whether we set the protagonist
As a player or as a pawn
You should differentiate and use these accordingly
Since it's set as a pawn, I'll use it this way for now
So, we need to get this
You can write it as APawn times Target
However, even though it's a Pawn, you can also receive it as an AActor
Casting is possible due to C++'s
basic inheritance structure
Since the casting is done as parent type, it's an example of upcasting
It's about changing the size of the child to fit the parent type
And we call it upcasting
So, casting like this is possible
Then, after finding the destination, you can calculate the direction
For the direction
Target minus me should do it
As I mentioned, subtracting one vector from another
It gives you the direction that results
To determine the direction to the target
You need to subtract your vector from the destination vector
Therefore, subtract your location from the destination location
So, Target, then GetActorLocation and subtract it
Me is actually this
If you align it with the line of this, you can write it like this
You can speed this process
GetActorLocation
This should do the job
Now, you can put this into the Direction
But when you put it in like this
It can be very far
It becomes a bit ambiguous if it ends up being farther than 1m
If you only know the direction
you can make the size 1
When you multiply 1 by any number, 1 disappears
1 multiplied by 0.1 is 0.1
Similarly, that's why we make this length 1
By normalizing
You make the size 1
So that it can be used only for direction
That's the reason for normalizing
It's to make the vector meaningful
As a direction
By setting its magnitude to 1
This is how you should understand it
Now, I'll normalize the direction
So, by doing this, it will be implemented
This is the first method
The method of using GetPlayerPawn
Before trying the second method
Let's demonstrate it first
So with that, compile it
And then run it
Save all
Save and then compile all together
Like so
The compile I mentioned is not this compile
I meant the blueprint compile
Compiling doesn't work
Okay do this and run it
You can see it move
You can see it working well
Now, this time, as you saw earlier
I tried it with this one
But now I'll try it with this one
If you double click this node
You'll see the symbol appears just like before
Now, I'll copy this exactly as it is
Now, I'll come back here and write it like this
If you open the bracket
Here, we also include the WorldContextObject
And this time we use an Actor class
Let's get to work
For now, I'll comment this out
Now, if you look here, to add a comment
There are things like these
We have shortcut like Ctrl+K, C, Ctrl+K, U
Using this, you can easily add comments
Select the block, then press Ctrl
And, in sequence, K and C
To uncomment, press
Ctrl and, in sequence, K and U
First, comment it out using Ctrl+K, C
Now, I'll implement it using GetActorOfClass
Similarly here, you need to put in World, so use GetWorld
If you need to provide variable value for
A parameter named World
You should always use GetWorld
Secondly
This is where you need to put an Actor class
This is a UClass, right?
Do you remember that last time
We used TSubclassOf
Instead of a UClass pointer?
Similarly, you can think of TSubclassOf
As a UClass
So you just need to put the UClass you want to find here
So, what we're looking for is the main character
Yes, the main character is set as PlayerPawn
Then, write APlayerPawn here
Yes, like this, You should write it this way
To use this, you'll also need to add this
In here
Let's include it, # include
Please add the PlayerPawn header like this
Then, when you come back, you'll be able to use it
After doing this, you'll see there's something called StaticClass here
This is the UClass
As you can see, it returns a UClass
Do you remember what I mentioned last time?
When you create an actor with this header tool
It creates two things
A UClass and a UObject
This way, you create a UClass instance and CDO
And this instance is what you put here
By doing this, you can find it like this
It returns as an Actor
So, actually, you can just apply the same content
You wrote here to the rest
Like so
We wrote it like this before
You need to write this part here like this
The semicolon is missing here
Like so
Now, this is quite simple, right?
When you use this
You can use it like this
But if you look inside the content here
It looks like this
The key point is this
Here, we have something called ActorIterator
Which is an iterator
In Unreal Engine, you can use such iterators
To find objects Of the desired type
That are present in the current level
So actually, writing it this way is also a method
Instead of using UGameplayStatics, let’s write it this way
So, not using that, and instead, if we write another example, we do it like this
instead of that, we use a for loop
Then write TActorIterator
And then what we want is
We want to find APlayerPawn
After that, we create an iterator with this
At this point, make sure to include GetWorld here as well
Since this is a repetition we use for
This is how we write for line
Next, if it is not null,
Then you can write it like this
Now we need to add the header
Hold on a second, if you look here
There is something called EngineUtils
You should add that
And EngineUtils looks like this
Or you can search for it
Similarly, if you search like this
You can see information about headers
It's formed like this
Let's write it out exactly as it is because it's too long otherwise
Let's proceed
So once you've done that you can use this
If you look, write APlayerPawn
This will be the target
The target is that the iterator is set up with double pointers
So, you can insert the value into this pointer like this
So, there are times that this could be null
Because what it does is to keep searching
Then, you check all the actors at that level thoroughly
It's about finding one that matches this data type
So, if it's null, you can just skip it
If it is null, then
Shall we write like this?
if and nullptr
In Unreal Engine, with C++ pointers, instead of checking for
'Null' like this, please avoid doing so
Please add ptr so it becomes nullptr
You should check it like this
Here, you can use continue
Then, it will keep repeating until it succeeds
Keep going until it’s finished, and then
Then, if it succeeds
Then, you can just do this task
It would be good to remember that there are various methods like this
Of course, you don’t need to master all of these yourself
You can do it like this, or like this, or like this
You can do this in whatever way is most comfortable for you
The reason I'm explaining all of this is because
When you're working on projects later
You'll often need to read and understand
Code written by others
This is true in real world work environments
In such cases, some people might have done it this way, and that's what I mean
Since people work in various ways
We need to be able to read that code
So, that's why I'm introducing this to you
Alright, then let's give it a try using this last one
So like this
That's done
Let's compile it now
Did I save it
Compile it
When we run it
You can see it work the same
But if this moves
It feels a bit awkward when it comes exactly like this
So, it would be nice to give it a little spin
So, let's try rotate it
If you look here
To rotate it
We can rotate in this direction
Since we have set the direction in BeginPlay
To this Direction
I want to rotate
In the direction
I want to make this happen
For rotating, you can apply various methods
The simplest of all is
Using this
Let's go to blueprint
If you look here, you can find a series of Make Rot from
This one means, will look toward the direction
You can think of it like this
IF you use Make Rot from X
It means setting the X direction to a certain direction
Now, there is a single one and XYZ coordinates
There are also coordinates like XY, XZ, and so on
What it means is, for example
Let's say there is an object that looks like this
This side is the front
Instead of looking at it this way, consider that our airplane is oriented like this
Let's say it's like this here
So, this one is going in this direction
So, it is saying that it’ll rotate
To face this direction and where it's headed is
That it is moving forward
Since this is rotated
Let's say our protagonist
Moved aside like this
Now, it can set the direction like this
So, currently
It comes like this
Even if you set the direction in this state
It still comes down like this
I don't want to rotate it like this
Instead, what I want is
For it to rotate like this
Let's say we view it from the side
So, the direction it’s moving is this way
This is what it looks like now
Let's assume this direction is the front
Then this side is up
Then I'm going this way
For example, if it rotates like this
It might not show here, but let's assume it has rotated anyway
So, if it moves in this direction
Moving like this also means it's going in this direction
Even when it’s in an inverted state
Moving like this still means it’s going in this direction
These two situations are the same
The goal is the same
However, if it rotates in a certain way, that wouldn't work
Right, so we'll keep the top of the object fixed
And only rotate the front
When doing this, you use these two components
Use make, route, and from
So, the second one is the fixed axis
Think of the first value for the front is the rotation axis
Think of it not as the axis of rotation but as the direction of rotation
The second one refers to the fixed axis
In that case, from our perspective here
We want to fix the top side
Keep the top side as it is
So, we want to ensure that this area remains the top
So, we want this area to be the top while this part rotates like this
So, whether we look this way or that way
The axis we want to fix would be the top axis
So, we’ll use this
Then, with MakeRotFromX
Since I'm dealing with the front side, I'll use X for the rotation
Since the fixed axis is the top, it would be Z
So, we have two arguments here, with these two values
The direction and the fixed axis
Like so
If you double click this
We have them again, very convenient
Looking at it, you'll find that it’s in the UKismetMathLibrary
By using this, you can easily perform rotations
This means you can use this function here
Then, we need to add the header for this
Similarly, if you search here
You’ll find that this is also in Kismet
Let's break it down
First, add this, and then
Once you do this, you'll be able to use it
Then, if you look here and open the parentheses
You'll see that there are two arguments
They are x and z
x is direction
The direction where I want to go toward
Next, what’s the axis you want to fix?
It's UpVector
So, you should use GetActorUpVector
And add UpVector
Just using this doesn’t fix it
It won’t rotate just by using this
You need to actually perform the rotation
This is just obtaining the direction
So, as you can see here
It returns the result as a specific data type
So, let's define the new rotation
Using FRotator here
Then, you need to use this as the new rotation
Then, use SetActorRotation to apply it
Write newRotation here
And it will apply the rotation
Save
After compiling
Oh, I'll remove this one
Now, compile
Let’s run it, Like this
As you can see, it rotates immediately
When going down, it moves straightforwardly
And when you do it this way, it rotates
To make sure it’s flipping correctly
Let’s apply an asset and check it out
We have an asset we created
This spaceship is the main character
The drone can be used as the enemy
So, let's include this asset here
Make a folder
I'll create a folder named models
Then, I'll place these two files directly into that folder
Import all
It’s been added correctly
I’ll go ahead and set up the drone now
Right now, it looks like this
Save for now
This is the mesh, and it’s set as a static mesh
Next, this is the material
This material is applied
And it's showing up in pink
It seems like the connection might be broken
So, I’ll recreate this again
Let's delete this
Here, you can see several options
Here, there are normals
There are two options for metallic and smooth
There's metallic, smooth, emissive
And basic color
Here, you can see the base color
Connect the base color here like this
And then, next
You'll see the normal
You can connect the normal here
Normals usually have colors like blue or purple
Then, this is labeled as metallic and smooth
Typically, metallic is here
And smoothness is referred to as roughness
These are stored in the R and G channels
So, assign the metallic to the R channel
And roughness to the G channel
Next, this is the emissive
IT says emissive here
Here, you can see the emissive color
I'll place the emissive color here
Now, the color has changed like this
Save now
If you look at the drone, the color's different
This is a bit of an homage to Star Wars
This was created by our art instructor
So, we have the license
I’ll go ahead and use this
Let's open the Enemy for a moment.
The Enemy mesh is currently a box, a cube
Let's replace that with the asset called drone
I'll replace it with the drone
replace it like this
Then, it will change like this
To go back, you can do this, and it will be updated accordingly
After that, we’ll set the position like this
This way is the front side
Let's rotate it so that it faces forward
The shorter side is the front
Thus, rotate it like this
I’ll position it like this
Let's place it Inside the cube
Within the box collision object
Currently, the size doesn’t quite match, so to fix this you can adjust it
By viewing from the top and using the scale
I'll increase the size a bit to make it look better
That's that
While we're at it, let's also adjust the protagonist
Open the spaceship
It looks like this
Let’s go to the spaceship and change the texture to this one
All of these
Drag and drop
The top one
This one is the base color
Connect the base color like this
This blue one is the normal
Connect it accordingly
Next, the dark one here is the emissive
Connect the emissive here
Here, you can see
It's set up with Occlusion, Roughness, and Metallic
Occlusion Roughness Metalic
This is Ambient Occlusion
And then roughness
Then, add the Metallic here like this
This will properly configure the colors
This will also correctly apply to the main character
If you look closely, you’ll see that the license is
Listed as ARA XR Lab
This is also something we created
The copyright belongs to us
Let's use this to proceed
Double click the main character
And make changes in the same way
Our protagonist is facing up
So the name here is the spaceship
Place it like this
Since this is a bit small, let's make it bigger
Change the color here in the same way
It should look good from the back view
Like so
Let's rotate a bit
Let's check and rotate it from here for now
We need to make sure it faces this way
Let's rotate it like this
Let's look at it at a back view
And then try to fit it in the box
This should be good to go now
Now, let's compile and play
We can set it up like this
Creating Enemy Spawner
Now, let's create an enemy
We'll create an enemy spawner
We’ll create a new class
And make an actor here as well
Now next
Let's name this EnemyManagerActor
Create class
In the last session
we worked on firing automatically
So, we set up the gun to fire bullets at frequent intervals
We can do a similar thing here as well
So, after a certain amount of time has passed
It will spawn an enemy
This will be the basic concept
So, let's go to manager's header
We don’t need to worry about the appearance for this
Only thing we need to do is just to place objects
First, time flows
And then after certain amount of time
Spawns an enemy
And place it at a certain point
Arrow will make it more convenient
Place the Spawned creature at arrow's position
Let's set it up like this
To do this, first we need to use UPROPERTY
EditAnywhere
Next, we'll need to place an arrow component
However, you shouldn't place the arrow directly on the root
Because the arrow needs to be rotated and transformed
So, we'll first create a parent component
Here, there's one more thing to explain
For components
There is actor component
And there is also a SceneComponent which inherits from them
There’s also a PrimitiveComponent that inherits from SceneComponent
So these are components
In blueprint, it creates these two
In C++, you can use this one
But basically these two are used often
So, the main difference between an actor and a scene component is
That this Actor component has no transform
But the Scene component has it
The big difference is this
A SceneComponent can be placed in a scene
Which essentially refers
To a space like a world or level
So, it’s structured like this
In reality, the terminology can be quite mixed and overlapping
Strictly speaking, a level is a concept contained within a world
But you can think of a scene as being synonymous with a level
Since it can be placed in a scene
It has a transform associated with it
The transform is required
For us to be able to utilize
Location, rotation and scale
So, it’s structured this way and the PrimitiveComponent is placed here
he PrimitiveComponent includes a transform
And it also handles collision and overlap functionalities
You can think of it as a component that handles all of those functionalities
So, hope you keep this in mind as you learn
So, since I’m going to be placing it
I’ll create a SceneComponent
Components start with U so we'll use USceneComponent
Let's name this Root
And then RootComp
And then I'll make the arrow
Declare it as class UArrowComponent
And we need to do this as well
Time will flow
First, let's declare float CurrentTime
We need to have current time
And then float
We also need something like MakeTime
The program is slow
Let's wait for a bit
Okay MakeTime
Set the time interval to 1 or 2 seconds for MakeTime
So 1 enemy would spawn every two seconds
To spawn enemies
We need to have a UClass for the enemy
Then, you can use TSubclassOf to create it
"Make sure to use UPROPERTY
With EditAnywhere
Next, use TSubclassOf
And then write AEnemyActor
Them, EnemyFactory
Set it up like this
Now we have to work on this
Come to the constructor, and first, create the root component
Since the name Root might be conflicting, we should choose a different name
Let's name it RootSceneComponent for clarity
That seems like a good choice to avoid confusion
We've used root component
Let's use RootSceneComp
Like so
Use CreateDefaultSubobject
Name it exactly as RootSceneComponent
Here, use USceneComponent
Then, set the name to RootSceneComponent exactly as is
Write SetRoot like this
And now we need to make Arrow
Then create, just like before
Like so
Arrow component goes here as well
UArrow component here
Now you need to add the header
Since SceneComponent was inherited
I mean since it is already included
you can just use it
Now we need to add it
Now attach it to the root
SetUpAttachment
I’ll set it up as RootSceneComponent
Now we need to make it
Make the time flow
We can do that with Tick
First of all, the first timer will be set
Secondly, when it becomes spawn time
Third, spawn enemy at arrow
I want to spawn it
Where? At arrow
Arrow's Transfrom
Like so, and reset
Current time to 0
Write it up like this
Let's start one by one
CurrentTime
Amd DeltaTime
Let's say if
CurrentTime equals MakeTime
Then, you should spawn the enemy
Place Spawn at GetWorld line
GetWorld
Since we did this before, I’ll quickly go through the setup
GetWorld
Then SpawnActor
And then
The object we want to make using Template
Is the enemy
EnemyActor
It will make this
And then the header must be added
Let’s divide the view at the top and take a look
For the top part
The header has been added
And then
Here
There are two things
That go in here
UClass
The UClass we made
Is the Factory that is made of TSubclass
So put that in here
And then in the ArrowComp
Add GetComponentTransform
Place it like this
Also, we need to reset CurrentTime
Set CurrentTime to 0
And then
Save all now
Go to the Unreal Editor and compile
We can see it working well
Remove the enemy for now
Let's take it out
Next, set up the EnemyActor
I mean the manager
We need to position the manager here
"I'll create and place this as a Blueprint
Like this
Write BP
You can see the Arrow like this
We need to rotate it
Like so
We are looking at it like this
Did we set this
To 180 and minus 90 earlier
Like this
Rotate it
And then
Let's place it
Shall we place it here like this?
Place it in the middle first
And move it up a bit
And then like this
You can input Ctrl and D to replicate
After copying it
Place 5 of the copies
Place them like this
Make sure all of these are selected
And set the X value to 0
Front and back should be 0
And then let's play it
Wait not yet
We need to fill the factory
After doing all that
Replace the default factory part
with BP EnemyActor here
Now compile
And then play it
The enemies are spawning
Every two seconds
Five of them all at once
flying towards the player
That's it for today
Let's summarize what we've covered in this session
Creating and Orienting Enemies Creating Enemies Facing Forward Create a new Actor class and name it EnemyActor
Declare the collision component as UBoxComponent* BoxComp Declare the visual component as UStaticMeshComponent* MeshComp
Create the BoxComp with CreateDefaultSubobject UBoxComponent TEXT()
Create the MeshComp with CreateDefaultSubobject UStaticMeshComponent TEXT()
Set the actor's location using SetActorLocation P0 plus Direction times Speed times DeltaTime Create and place the Enemy actor using Blueprint
Random Direction Determination Feature Method 1: GetPlayerPawn function Method 2: GetActorOfClass fucntion
Method 3: Use a TActorIterator loop
Creating an Enemy Spawner Function to Spawn Enemies at Regular Intervals Create a new Actor class and name it EnemyManagerActor
Declared USceneComponent RootComp Declared UArrowComponent ArrowComp
Created RootSceneComp with CreateDefaultSubobject TEXT() Created ArrowComp with CreateDefaultSubobject TEXT ()
Declared and implemented the Tick function with DeltaTime
The End