Hello, this is Kim Hyun-Jin on Unreal Engine Basics for Immersive Contents
Today, we will learn how to get the event for actor collision
To visualize the particles of that location of collision, while destroying that actor
Getting Event for Actor Collision
Let's get the events for actor collision
At Unreal, there are Block and Overlap
Block is something physical
When a ball hits the ground, it bounces, it rolls away
But for Overlap, we do not see whether there is a collision
Because it does not have any physical result; it just passes through
Let's see how these are different
At Content Browser, let's create a Blueprint Class
Right click, Blueprint Class, choose 'Actor'
Let's name it BP_CollisionTest
Double click to open
Let's add a Static Mesh Component
Click on Add, search for Static Mesh
Let's choose its form as well
At Details, go to Static Mesh, click on 'none'
And search for 'cube' to make it a cube
And compile to get this part where we set its material
Compile, and choose any material you want
I will go with yellow
At Static Mesh and go to Details, there is this part called Collision
Let's expand on this Collision Preset
These are some options on how this actor will process a collision
Now, what we have for Collision Preset is
Block-all-dynamic
Let's expand this and change to Custom
Then we can change all these things below
See how Collision Enabled is set as
Collision Enabled Query and Physics
It means it will process both Block and Overlap
At its dropdown menu, we have No Collision, Query Only,
Physics only, Collision-enabled, Probe only, and Query and Probe
We need to know about No Collision
So set it as No Collision, which simply means that there will be no collision
And Query Only means it only processes Overlaps
Physics Only, only the Bocks
Collision Enabled means it processes both Block and Overlap
Down here, for collision Response
All the things below are checked for Block
This means this will only allow for Blocks
Let's make this actor move physically
We have a tab called Physics
And we have Simulate Physics, tick on that
And we also have Enable Gravity option ticked
It means it will use gravity
Let's compile, save, and go to Actual Level
Let's put some BP_Collision tests in front of Player
Let's move it a bit up
And play, now we see how it collides with the ground
Let's now move Player to collide
Now, see how it just pushes around the cube
This is the Block processing
Now, let's try these options for Block to Overlap
Compile and play here, and we do not get physical collisions
It just falls down into the ground
With all the options for Overlap
It means that it only Overlaps with other actors
For Overlap, it seems that there is no collision
But it is calculating internally whether there has been a collision
So for a collision, we could make it act a certain way with that as an event
Let's take a look at the options at Collision tab
Object Type is setting actors into a certain group
We have Player And AI, and for these, their Static Mesh Components are
Set into a group called World Dynamic
And you may want to switch up the group a bit
For that, we can create an object type
We can create an object type to, for example, set Player into Player Type
And AI into AI Object Type, to group them differently
For Collision Enabled, let's say Player is set as 'No Collision'
It means it processes for neither Block nor Overlap
Neither Block nor Overlap
Query Only means it only processes Overlaps
Physics only, Blocks only
Collision Enabled, both Overlap and Block
We have Response option here
For Response, there is Ignore, Overlap, and Block
Ignore ignores all collisions
Overlap processes only overlaps
Block, only blocks
And let's look at this in more details
We have Player and AI
Let's say the object type of Player is 'Player'
And that of AI, 'AI'
For Collision Enabled,
Let's say I used Collision Enabled for both
And for Object Response, we have all the object types listed like so
World Dynamic, their own types, so Player and AI as well
And there is a box to tick on
Let's say Ignore here
Overlap here
And Block here
Same thing, we have the Objet Response tab set up for AI
And for this, for Player, it ignores collision with World Dynamic
And between Players, they Overlap
And with AI, it Blocks
That's the setup we can have for each
Now for AI, it Overlaps with World Dynamic, for instance
Ignore collision with Player
And Ignore collision with AI, we could set it up like so
And for Player, what collision does it have with AI?
It has Block
And for AI, it Ignores collision with Player
And with this, when these two collide, there is no Block happening
Here, Ignore is the priority; so it follows the option for AI
And let's say we changed AI Actor and Player collision to Overlap
Then when they collide, it is either Block or Overlap
And between these, it prioritizes Overlap
So to make it Block, both should be
Ticked for Block so that it happens
So the prioritization goes, Ignore first
And then Overlap, and finally Block
Let's remove our BP_CollisionTest Actor for now
When AI Actor approaches Player Actor
When they collide, let's destroy AI Actor
And add an effect in that locationn
So first, let's set things up for their collision
Open BP_Player, add a Component
At Add Component, search for Box
And you get Box Collision
Add this Box Collision
Box Collision Component does not have its form
It only has an area for collision
So when we look at it at Viewport
We see a yellow box here
We could check for collision at an area
That is identical to the form of the content
Or we could make it a little bigger than
This given area here to check for collision
If we use it for this Collision at Static Mesh, it won't have such extra space
So let's add Box Collision Component
And to the right, at Shape, we have option for Box Extent
This decides the width, length and height of it
It is set as 32, 32 and 32
Which is smaller than this cube here
When we change it to 50, 50, 50, it is exactly the same
For Static Mesh Cube, I think it was 100, 100, 100
But here, we set Box Extent as 50, 50, 50
And the size is identical
If I want 100 for width
You just set the half of that value
If we want more space for collision check, we make this bigger
Then, the form stays, but the collision area
It gets bigger than the form itself
For now, let's set the size the same, 50, 50, 50
And this Box Collision Component
Drag it to drop down at Default Scene Root
Then this Box Collision Component becomes the top component
And then we have its child, Static Mesh for form
Now, it will collide with Box Collision
As well as with Static Mesh
So let's disable one of the two
Choose Static Mesh Component
At Collision tab, expand Collision Preset
And set its preset as 'No Collision'
All the menus at Collision Preset
They are the saved values for option settings
That Unreal has made for us in adavance
So using 'No Collision' option makes
Object Type as World Static, Collision Enabled as No Collision
It means, whatever other setting is done
With this component, there will be no collision processed
Now next, choose Box Collision Component
At Collision tab, expand on Collision Preset and make it Custom
Let's keep our Collision Enabled
Query Only, it only processes Overlaps
And Collision Response, let's set it as Ignore for now
Now, let's set Object Type
Click on what says World Dynamic, and we see all the
Default Object Types set by Unreal
These are not enough for all object groups
So we can make our own Object Type to set it here
For that, go to Edit, Project Settings
And we get this window
To the left, at Engine, we have a menu called Collision
Choose that menu, and at Object Channel
We can set for Object Type
To the right, click on New Object Channel
And we get a small window like so
Here, name it 'Player'
And Default Response as Ignore
Default Response means that whenever a new actor is spawned
The response for Player will be Ignore
That is the default setting we are adding
And then click on 'Accept'
And that's how we add the Object Type
Close the window, back to BP_Player
We need to compile it once
To renew this Object Type list
So compile now
And click on Object Type to expand
Now we see Player at the end
So let's switch it into Player
Here, at the end, it now has Ignore for
Ignoring collision with Player object
Why is it Ignore by default? It is because when we made our Player object type
We set its default value as Ignore
If we said Block there, when we compile
This Player Collision Response option will have Block ticked in default
Let's check that again when we set for AI Object Type later
Let's now open BP_AIActor
Same with Player, let's add a Collision Component
Click on Add, and search for Sphere to get Sphere Collision
Add Sphere Collision
Let's drag Sphere Collision component into Default Scene Root
Now, choose Static Mesh Component for the exterior form
At Collision tab, click on Collision Preset, set as No Collision
And back to Sphere Component, let's set for collision options
Expand Collision Preset
Overlap is now set as All Dynamic, so click on it to change to Custom
Leave Collision Enabled as it is for now
For Object Type, we will be making an AI Object Channel to set it as AI
At the end here, Player is set as Ignore
So, for Collision Response, check for Ignore so that
It ignores all collisions for all object types
And for this AI, let's make it Overlap with Player
Then this AI Player will Overlap with any actors
That have object type as Player
Now, let's create the AI Object Type
Edit, Project Settings, Engine, choose Collision
At Object Channels, choose New Object Channel
Let's name it AI
Now, Default Response is Overlap
And Accept
Close the window
Now, if we haven't compiled it, we see no AI under Object Type
So compile
And click on Object Type, and now we see AI at the end
Choose AI
And at the end, at Collision Response, we have AI added
We chose Overlap as default, so we see Overlap as selected here
Of course, at BP_Player, try and compile
And we also see AI here, whose default setup is Overlap
Now, at BP_AIActor, let's go to Collision Response and change AI option to Ignore
This will make AI types overlap with each other
That's it with the collision settings
But when we play here
It seems that they collide, but we do not get any cues, right?
So let's print a sentence to tell us that there is a collision
So at BP_Player Blueprint
Choose Box Collision Component
At Details, scroll down for Events
Here, we have On Component Begin Overlap
And a plus button
Click on that button
And where it allows for Event Graph and Blueprint Node
We get another Event Node
This Event Node runs automatically whenever this Overlap happens
And in the list, we have Other Actor
This Other Actor Pin contains BP_Player
And the Actor it collides into
So let's make it print the actor name that has collided into BP_Player
So click over Other Actor Pin
And search for Get Display
And under Utilities, we have 'Get Display Name'
Choose that node
This node returns the actor name
It returns this name at Outliner, at Actual Level
So when BP_AIActor is the one, it will say 'BP_AIActor' here
Let's add a node here
Let's drag in Return Value and use Append
Which means it adds more characters following this name
Let's just say 'Collision with'
'Collision with so-and-so, exclamation mark'
Then the result of combining A and B will be at Return Value
Let's print that
Right click, Print String, and this final content
Let's connect it to In String of Print String Node
And whenever there is a collision, let's say that this Print String happens
Compile, go to Actual Level, play
When collision starts, we get 'Collision with BP_AIActor'
As printed on the screen
Choose Box Collision Component again, and this time go to On Component and Overlap
Let's add this event using the plus button
This is called when Overlap is over
So Actor approaches, and when collision happens, what is being called?
On Component Begin Overlap
And now, it is in collision
And what happens when the collision ends?
On Component End Overlap is called
Let's get these three nodes, copy and paste using Ctrl C and Ctrl V
Let's print the same thing when On Component End Overlap is run
Let's connect it to get name from Other Actor
And then for B, 'End of Collision with so-and-so, exclamation mark'
Compile, back to Actual Level, play; it is called for collision
And also called when the collision ends
Let's move to BP_AIActor
See Sphere Collision Component here?
Choose that component, scroll down Details and we have the same event here
On Component Begin Overlap, let's add the event
Same thing, from Other Actor, drag out to add Get Display Name
Add Append Node and write 'Collision with'
And print it
Connect the Exec Pin
Return Value, connect it to Print String
Compile, and before we play it, let's think about it
AI Actor and Player are in collision
We made sure that this event at Player is called
And we want to see whether the ones we just added, On Component Begin Overlap
Also run as well
Play, and collision with BP_Player
And Collision with BP_AIActor; we have different things printed
For BP_Player, what is this Other Actor?
It is AI Actor
That's why it prints 'Collision with AI Actor'
And for this AI Actor, who is Other Actor of this On Component Begin Overlap?
Player, right? That's why it prints 'Collision with Player'
This shows us that we can get event from each for a collision of two things
Same thing End Overlap
Let's add On Component End Overlap
And copy and paste these three nodes
For Other Actor, for Get Display Name, choose Object
And say 'End of Collision with so-and-so'
And connect to the Exec Pin
Compile, back to Actual Level, play
We get two events of collision, and later
Two events printed for end of collision
Though, each has different name for to what it collides into
Particles on Collision Location
Now, let's add Particle for the collision location
And destroy the AI Actor collided
At BP_Player Blueprint
Let's remove all the content for On Component Begin OVerlap
ANd let's destroy this Other Actor that collided
So over at Other Actor, click to drag
And search for Destroy Actor
And like so, we have Destroy Actor on top
Add the node
Then it connects automatically to Exec Pin
And what does it mean?
When Player collides into it, what do we do to that Actor?
It destroys that Actor
Compile and play
And when they collide, it disappears
It also disappears from this Outliner
So in case of collision
What if we want to destroy
Both the Other Actor as well as Player itself?
What do we do for that?
Right click, add another Destroy Actor node
And just connect the Exec Pin
What does this node do?
Destroy the Actor that collided into Player
And the one after, destroy Player itself
The one that destroys me has 'Self' as the target
Meaning, destroying me or Player
And destroying the actor that collides has 'Other Actor' set here
Meaning, destroy Other Actor
We had something similar to this before
At BP_AIActor, we had importing location of Player at Begin Play
This target could be either Self or other Actor
And that made a difference in whose location it imported
And the same thing is happening here
Compile, at Actual Level, play and in collision
We see both actors disappearing
Let's modify this same action a little
Let's snap this collision part
And move it upward
And let's have on Component Begin Overlap just destroy me
Compile
And at BP_AIActor
Let's remove the connected node to On Component Begin Overlap
Right click anywhere, add a Destroy Actor node
And let's set target as self here
Compile, and what happens?
When Player collides, this is called; it destroys me
And for AI, the same node runs
So it destroys this another self
So we can see that it again destroys both
So whether to process it all at once
Or at each other's part
That's a decision you should make for your working environment
Let me change it back here so that everything is processed at Player's end
Remove Destroy Actor at BP_AIActor
Back to Player, and in between
Connect this Other Actor destroying node
And connect that with the one that destroys Player
Let's remove On Component End Overlap
We just need to get event for the beginning
So we will just leave our Begin Overlap node
Remember how we talked about the node Sequence last time?
We will also be using Sequence here
To section them in order
Let's disconnect the Exec node
Right click, search for Sequence
Add a Sequence node
Connect Exec Pin to Sequence
First, we need to connect it to
'Then 0' of our destroy-other-actor node
And for destroy-me node, connect to 'Then 1'
So this will be number 0 and number 1
Now, when in collision, they are destroyed
And what happens is
To this location of AI Actor in collision
We will add an effect
Which in Unreal is called a Particle
So under Content Browser, next to Content Folder
See Startup Content?
We have a Particles folder there
And in that folder
We have some sample Partilces
Here, let's add this P-Explosion effect
To be displayed on screen
Double click on it
To get a preview
Let's close that
Back to BP_Player
Right click
Spawn Emitter
We have a node called At Location
Choose that node
And choose Add Pin of Sequence
So that Then 2
Let's connect it to Spawn Emitter at Location
And see this first in the list?
Emitter Template
Click on selection Action of it
And we see all the list of Particles we saw before
And here, we said we are using
P-Explosion
So let's select that
And Location means
Where we are going to show
This Particle on screen
And we can choose the location
Of where AI Actor is getting destroyed
So let's drag it from Other Actor Pin
And search for 'Get Actor Location'
To add a node
And connect that value to Location here
As we change to Sequence
We just left that other-actor-destroying part
And its target as 'Self'
We had 'Other Actor' connected before
Compile, and at Actual Level
Let's play
It is destroyed but there is no Particle
Let's pause
And we get an error message
This is because
The order is not right
We destroy Other Actor
So it is destroyed
So it is not in Outliner anymore, it is gone
And what are we doing after that?
We are adding a Particle
Where on the screen?
To the location of Other Actor
So, even before Spawn Emitter at Location node
Runs at all
Other Actor is already gone
That's why
It has no idea what Other Actor is
Because it is destroyed
That's why we get an error and the Particle does not work
Then what do we do?
We should first show the effect
And then destroy Other Actor
This is a very important point here
Whenever you work with these nodes
You need to think about
What should come first
So right click on Then 2
And Remove
We can just create one more
Before Then 0
So right click on Then 0
And see Insert Execution Pin Before?
Select that menu
And it will create another Exec Pin
Before the current location
Below means it will be positioned later
For us, we need it before
So choose Before
And the ones after are
Now Then 1 and Then 2
And we get a new Then 0
So this part now goes to
Spawn Emitter at Location
Let's reorganize the nodes
I've set the nodes like so
Let's compile
Back to Actual Level
And Play
For every collision now
We see an effect
Let's summarize
What we learned today
First, we learned about
How to set things up for a collision
We also learned about
How to change Object Channel to make it collide into a certain actor
We also used Collision Preset
So that we can change all collision options
At once
And we also learned to add
A Particle effect for
An actor collision
Getting Event for Actor Collision Make it collide with a particular actor with Object Channel setting
Blueprint Class - Actor - Add Static Mesh Component Static Mesh - Details - Expand Collision Preset
Physics - Tick Simulate Physics - Tick Enable Gravity - Compile and save
Ignore, Overlap, and Block for Object Response
Edit - Project Settings - Engine - Collision - Object Channels - Object Type
Using Collision Preset to change all collision options at once - Add Collision Component - Search for and add Sphere Collision
Name change for actor : Other Actor - Search for Get Display - Utilities - Get Display Name
Call Message for Beginning and End of Actor : On Component Begin/End Overlap
Each actor can get event for the collision event
Adding Particle for Collision Location How to add Particle effect for Actor collision
Drag in Other Actor, search for Destroy Actor Particle: Effect for that collision event
Content Browser - Content - Starter Content - Particles
Choose Spawn Emitter at Location Node, Location to choose where to add the effect
Make sure what comes first when creating nodes