Utilizing montages and handling shooting direction
Hello, my name is Young hoon Lee
In this lesson, we will learn about how to utilize montages
And we will learn about how to create shooting effect
In this chapter, we will learn how to create animation motages
And how to play them
In animation blueprints
Additionally, we will learn
How to attach grenade launchers and sniper rifles
To the hands
And how to handle camera motion during shooting
Animation Montage Creation
Let's implement an animation of shooting gun
Where the main character fires a weapon
If you take a look at this animations folder
And search for fire
You will be able to find an animation called Fire Rifle Ironsights
We will implement this animation
This animation include character's shoulder movement
Now, we will control this animation
By inserting it into the animation blueprint like this
It seems a bit challenging to insert it into the existing state machine
Of course, you can do it here
Like this, add the state
You can add shoulder movement, state of attacking
Instead of doing it that way
It's better for the animation to return to the original state after activating
So, we will control the animation to have that effect
There is a feature that gathers animations into one place
It collects animation sequences
Making into one allowing us to control them more easily
In Unreal Engine, we call it an Animation Montage
Let's take a look at the definition of montage
This is what it means
Often used in movies and films
Combining multiple scenes together
So, montage is the concept of combining
Multiple elements into a single animation
Now, Let's create a montage and control it
Let's name the montage as Fire
In the Blueprint folder, right click and create an Animation Montage
By selecting Create Animation Montage, from the context menu
Now, you'll need to select the skeleton
We are using this now
Set it up like this
Since it's a montage
I'll rename it as Montage Fire
Now, let's open it
This is how it will look like
A montage is structured like this
There is montage
And then, at the top
This part is called sections
If you right click here
It's name appears, sections
The very top one is called sections
The second one is called a slot
Basically, montages are linked together through slots like this
Montage is a slot
That's how you should remember it
We can add slots
These slot names you see here are
Just basic default setting
You can add new slots
And when you do that
There is something called slot manager
If you click it
you can add slots right here
This menu is over here as well
If you look at the top over here
Should be in the window catergory
You can see Anim Slot Manger in the window category
This has the same features
Clicking this also adds a tab
So that was about how you can create a new slot
You'll see notifications and whatnot
Now, let's insert an animation here
I will bring Fire animation
From this asset browser
Because that's what I told you we were going to do
Okay, this is it
Drag it over here
Then now, you have composed a single animation sequence
Of course, you can add more into it
Like so, you can add more
So then, two, three mixed together
And when you play this
You can see multiple animation
Forming one single sequence
This is what we call an animation montage
Alright, for now in the part where the player controls shooting
Let's try controlling it
With just one animation sequence inserted
Like this
Here on the left side, you can see there's a concept called Blends
Blends are the concept where they provide smooth transitions
Between existing animations
And a new animation during the transition
For example, you can think of it in this way
If the animation ends like this
But the shooting animation is set up like this
Then the arm movement wouldn't be smooth
Transitioning directly like this
Would feel very awkward and abrupt
But if you use blends, it will be like this
Additional frames are automatically generated
So, the arm gradually stiffens like this
Gradually lifts up
I'm not good at drawing, but anyway
I want to say that it's done in a way that gives this kind of feeling
Like this, and then it transitions into this
It would be nice if it could be drawn more beautifully like this
This is how you create intermediate frames like this
This is what we call blending
There are various terms
The term blend is used
And sometimes terms like cross
Or crossfade are also used
People also use terms like fade
And terms like blend
In Unreal Engine
We use the term blend
So, there's certain time for blending
If you hold down a keyboard control key
And then scroll the mouse wheel, you can reduce it like this
Alright, let's continue like this No, that's not it
Timeline bar at the top here
If you drag it like this
The total time is 0.23 seconds
If the blend time is 0.25 seconds
The animation wouldn't work
It will not even move
If blending is done while moving
The movement will not be made
Therefore, we need to reduce the time
Will reduce a lot to 0.05 seconds
I'll reduce this to 0.05 seconds to use it effectively
Let's set it up like this for now
And let's save it
Then I'd like to integrate this
Into the animation blueprint
Since we haven't created a separate state here
What you want is to smoothly transition
From the current animation
To briefly play the shooting animation when you shoot
And then return to the original animation
We can do that by inserting animation
In the middle of the action
Let's try
If you left click here
And search default slot, this comes up
You can add slots like this
Like this, connect the slot
For now, create a node
named Default Slot and insert it
Then select this slot
And on the right side, you can see the option to rename the slot
You can change it like this
The existing names
May not be suitable for what I'm trying to do
So, I'll add a new slot
And write it here
Then you go to the Slot Manager here
And click Add Slot
I will name it fire
Click add slot and create and name it FireSlot
Name it like this
You can delete all the default slots
You can insert in the middle like this
Or create a new group
For now, I'll just add a slot
To the default
Well, just adding it like this isn't enough
This is labeled as Default Slot right now
We should rename it
Let's rename it as FireSlot
If the animation isn't reflecting properly for a moment
Try detaching it briefly and then reattaching it
This should make it reflect correctly
It works right?
This is it
If it doesn't work for you
Then, save everything
Try closing it and opening it again
After adding and renaming
Switch back to the Animation Blueprint
Then proceed to rename the slot
Select it like this and replace it with FireSlot
Compile them
What happens is
That there is no chage
Because once we've inserted this slot
It will perform its action when triggered
Just inserting it alone isn't enough
So, in a way, you can think of the concept of slots
As intercepting or capturing
So, when we shoot the gun
We intercept and handle it through this mechanism
Let's test it briefly in the blueprint
To see how it works
Open up the main character blueprint
TPS Player
Yes, this one
In the legacy input settings
For the left mouse button
it's already registered as Fire
It's not there, let me add one then
let's name it fire like this
Let's do this and register the left mouse button
I'm not going to use this
I just created it for testing purposes
Once you've done it
Let's go back to our main character
Then you would use 'fire' here
I see an event
Since we've configured the enhanced input now
We don't need to use it
But I'm just trying to do a test
When we shoot a gun
In real life we left click on our mouse
Then we play the shoulder movement animation
And this is how
Search Play Anim Montage
This one here
Create a Play Anim Montage node
There are several nodes for playing animation montages
If you search Play Montage
Play Montage is one of them
There's also a Play Anim Montage node
Like so, these are examples
Use whatever suits you
I will use this one
Once you are done, connect these pins
When you press it and indicate that you want it to perform this action
Next, expand it to see
Which montage you want to play
And here you'll see our created montage listed like this
Then, select it like this
So the returned value
It will return the entire time
Playing the actual animation
If there's only one animation montage registered
Only that montage will be played
If there are multiple montages, it returns a combined value
You can control animations using this value if you want to
However, when you control animations
In the middle tab here
You can see it set up like this
In this tab, you can play animations for each section individually
Also, when you look at playing here
You'll see at the very bottom it says Start Section Name
So, sections here refer to segments or intervals
You can control playback for each segment separately
In such cases, it's important to note that
You're not necessarily returning the time of each section
So don't rely too much on that
So there are other ways to handle that
Let's try this method first and see how it goes
After doing this, compile and run it
And when you try it out, the shoulders should go twitch, twitch, twitch
You should be able to see the movement like this
No matter what gun we choose
Pressing the left mouse button
Will make the shoulders twitch like this
Well, there's a bit of an issue with this
Let me walk forward
And shoot my gun
Do you see the problem?
Look at the leg when I shoot
The shoulder movement is played with standing posture
And If you shoot while walking
The leg stops while walking
And tries to stand
So we need to deal with this problem
Well, one way to solve this is
When playing the animation
We can separate the upper and lower body
And play the animation separately
So we'll use this approach to control this
In order to fix this
The base animation
And the animation through this montage
Need to be differentiated and handled separately
In Unreal Engine, if animations are set up like this
They can be temporarily cached
When we typically talk about caching
It refers to temporarily storing data
I'd like to cache the results
From this move FSM if possible
So I'll capture this and set it up for caching like this
I will use New Cached Pose
This way, we can temporarily save it like this
Since the name is move FSM
We should call this save move FSM
Let me jot this down briefly
So, when you retrieve this
If you try using move FSM again
You can retrieve the cached data
It looks like this
As I mentioned earlier
We're going to separate the upper and lower body animations
we'll use the original animation for the lower body
And for the upper body
we'll use the result from the fire slot
We need to divide it into these two parts
But when using this slot
The original animation should come in
And based on that
we'll decide which new animation to select
And then it will return in this structured manner
So, we need to place this cache here as well
You have to fill this up to prevent any errors from occurring
Shall we look at these two animations like this?
Let's disconnect this for a moment
The structure is formed like this
Yes this is the structure
To achieve separating upper and lower body animations
We'll need some functionality that allows us to do that
There's a node called Layered Blend Per Bone for that
We'll be using this one
Add the original animation
Into the base pose
Then add the new animation to be performed
Into the blend pose
This is it
For better understanding
Think of it this way
There is the structure
We will be standing on it
The player's character makes A pose
Standing like this
Little odd
Anyway, the character will be standing like this
The character's pose
We call it the A pose
This here should be its feet
The Bottom center of the feet
Here is where the first pivot point is now
Typically
Here we have these bones
You see bones, like in the feet
Knees, or hips
Typically positioned centrally
The hips
Shoulders
Elbows
Hands, neck, head
Be aware of these skeletons
They can be found in such places like this
So, with these skeletons
The order of the skeleton usually
Goes up from the bottom like this
Earlier we had the base
There was a blend
Let's take a look, we have base pose here
And we have blend pose
Given that
We are starting from the bottom
Then, the bottom should be the base pose
From where to where should the base
And from where to where should be the blend
Is the basic concept of this
So, we need to define a specific point where we establish
The boundary between the blend and the base
So, I'll set the boundary at the middle of the spine
Around the waist area
So, the lower body will be from this spine
Down to the beginning and from the spine
To the remaining end
That's how we'll distinguish it now
Let's take a quick look at the skeleton
Here, you can see an image at the top
If you look here, the one all the way on the left
Is what's really a skeleton
If you click this
You should see the skeletons
There's the pelvis
And then there's the spine
And it's set up like that
So, I'm going to divide it like this
Using the waist spine 01 as the reference point
So spine 01
I'm going to copy the name itself
Copy
Go over here
Select Layered Blend per Bone
Go to the right side
It's set up like this
And in details, we have layer set up
If you expand it
You can add
What's called a branch filter
Click this
If you open it up, it looks like this
Right here, in the name section
Remember the name, Spine 01?
Let's paste it in here
If you put it here
This Spine 01 which is about where the waist is
The body will be distinguished as upper and under parts
And there is a space
It can spin or handle like this
If we have divided bodies
The real animation is made
Using the transform information
The concept is that we will be seeing
The replay of the recorded transform
That is the animation of the Unreal Engine right?
So the spin and whatnot are included in there
So we should enable this
so that the character can make the spin
In the MeshSpace
Here's a simpler way to put it
You have your own space and then there's the world space
So instead of just processing things in world space
you first handle them in your own space
And then move that to the world space
Just remember it like this
So I'll go ahead and do just that
Implement these changes and then compile.
Then after that, we'll need to connect it here
I'll set it up so that this becomes the output like this
So after doing this, when you come back and test it
If you shoot the gun, you'll notice that
The lower body doesn't move anymore
You'll see that only the upper body jerks or moves
Then I'll now try implementing this in C
Excluding the animation is done here
the shooting part was handled by the character
Now I will delete this part
Now let's go to our main character
currently, the shooting action
Is handled by the protagonist
So, when we go to the protagonist's header file to process it
Ultimately the animation will be controlled
Through that when shooting the gun
Right?
So, it's set up like this
I'll implement it on the TPS player side for now
If you come down like this
In the bottom, write, I want to play a montage of
Shoulder movement when shooting
So, we want this
Then, we need to make a montage
UPROPERTY, EditAnywhere
Class and then
UAnimMontage
With this
This one we use Fire
FireAnimMontage
Like so
And then
We go back to our character
We can find the shooting part over here
Right here
OnIAFire
Right here we shoot guns
Doesn't matter what kind of weapon it is for the shoulder movement
So, we write it here
Use function called PlayAnimMontage
Let's add FireAnimMontage here
And we should do this when the value is not null
So, check for the null
Write it like this
Compile this
Come to the main character and in the class default
Right here
In here let's put our custom made Montage Fire
When you start the game, it works just fine
Shoulder twitches
Upper and under body parts are separated well
Camera motion handling while shooting
Let's synchronize the gun and try it out
Right now, the gun is separated
If you look here
I'll attach the gun directly to the hand and try controlling it that way
Do you remember when we added the socket
For shooting the gun to handle this?
So, we'll add the socket here
And use the coordinates for that
In a similar way
We'll create a socket on the hand for each of these weapons
And attach them accordingly
To control them like this
If you look at the mesh
this is the one the character uses
Let's double click to open it
And If you look, there's the skeleton
I will attach it on the right hand of the skeleton
Find the right hand
It's not easy to find
From here, you should look for the right hand
Let's search for it
Let's try hand
There, I see the hand
The name is Hand r
I'll add a socket here
Like this
Right here
Select this and right click on it
Use Add Socket to add a new socket
So, add one like this
Let's use the name
Hand rSocket
Make sure to save it
Let's go ahead and save then come back
Let's go to the source file
I'll move to the character's constructor to check this
Here you can see that we have the gun mesh and the sniper rifle mesh like this
We've created them and attached them like this
You can see them in two places
When attaching these, you can see here that as the second parameter
You can input the socket name like this
Then if you just write the socket name here
It will attach to that socket
So I'll write the name here
Let's say Text
so doing it like this
you can replace the name hand rSocket if you want
But I'll go ahead and use the automatically generated name for now
Do it like this
And here as well, I'll attach it the same way
I'll restart Unreal Engine once
And then build it here
Let's go ahead and run it
Let's go to the character
If you look at it, the gun is attached
But it's way up there
So we need to really attach it
And adjust its location
So, let me proceed with these tasks
Let's do this one by one
Temporarily turn the sniper rifle off
Let's set up the gun first
I'll turn the visible of the sniper rifle off
And it will be hard to control if it moves
If you look here in the mesh, you can stop the animation
Expand this
Expand the advanced settings in animation
You can make a pose
Pause the character
Now let's place the gun
bring it down like this
And place it nicely
We've placed it
If you use the menu over here, it would be better
With Perspective menu, like front, you can micro manage the location
I think it's good enough
Go to the right side perspective
adjust a bit
If it's too big,let's turn off this setting and adjust the spacing
Turn this thing off
Try to place it nicely
Feels like the gun is too big
Will go for it
Now second one, will do the same for the sniper rifle
Turn off the gun as well for this one
Visible routine
Turn off the reserve like this
And then turn the sniper rifle on
This one also is out of place
Place it near where it's supposed to be
Just like before, front perspective
I'll give it a little spin
Place it in a suitable position
Just place it nicely like this, and it should be good
Now, once you are done with the setting
Now we should make sure it's fully visible
Turn on the visible routine again
The animation with the pose
Must be placed on the right spot
And then you will see it working well
The camera follows well
Press number 2, the gun is set nicely
By doing this, you can synchronize the animation with the gun's position
And handle it smoothly and naturally
Then adding a slight shake when shooting the gun
can enhance the impactfulness
So, I'll go ahead and try creating a feature that shakes
The camera like this for added effect
I'll start by creating the blueprint for this
Try right clicking here like this
Find Blueprint class
Then select All classes like this
I'll search for camera shake here
Okay, so there's a Camera Shake Base available
Alright, select this Camera Shake Base, and then click Select
Okay, then I'll name this BP CameraShake
if you double click to open it
you'll see it looks something like this, empty
But here on the right side in the details
You'll see there's something called Camera Shake Pattern
If you look at the root camera pattern
You'll see there are four options listed here like this
Then if you look below
You'll see there's this Wave Oscillator
Which is what we typically think of as
Camera vibration or oscillation
So creating an amplitude with this allows us
To shake the camera using that concept
If you look at the second one, there's something called a sequence
Sequence, as the name suggests, is used to create animations
We typically refer to animation sequences as animation sequences
Right?
Okay, so we have this here
The concept of Perlin noise
Involves defining a specific region
And then varying the intensity within that area
It uses values between 0 and 1 as coordinates
It can also be used for rotation
So it represents randomness in this way
Think of it as embodying this concept
Finally, Composite refers to using
A combination of these three concepts together
So, let's go ahead and create a Wave Oscillator first
Click on this
If you expand it, it looks like this
Then I'll adjust just the rotation value once
I'll go ahead and adjust
The rotation values to see how it moves
It's designed so you can adjust three values, pitch yaw and roll
So
I'll set this one
At the top to 1
Usually, amplitude refers to intensity
And frequency refers to the concept of frequency or rate
So you can use this to control as needed
Set Pitch to 0.5
And Frequency to 10 like this
And Yaw controls the rotation from side to side, like nodding up and down
I mean yaw rotation
I won't do this one
Let's set this one to 0
In a sense, pitch corresponds to nodding up and down
While yaw corresponds to swaying side to side
Think of this as a swaying motion
The thing is how are we going to rotate the camera?
And I'll set Roll to just 0.2 like this
Set this one to 0
Next, there's the FOV and timing options here
In the timing section, it looks like this
The duration is set to 1 here
And there's also a blend time like this
So, I'll reduce the blend time a bit
This one with 0.05
You can think of the blend time as similar to when you blend animations earlier
You have the camera
In its original controlled state
And then you want it to momentarily shake
Before returning to its original state
That's what we're talking about when we mention the blend time
Make it short, like 0.05 seconds
Let's set it up like this and give it a try
Since this is now in blueprint
To use it in the source files
we'll need to create a variable for it
And retrieve it for use
So let's go to the character
Not the animation
And handle it from this side
Under the montage
We need to declare variables
For handling camera shake
So we'll also use U Property for this
Edit Anywhere
I'm going to use T SubclassOf
Instead of 'class' for this
That's also the concept of U Class
So I'll call it a class
Basically, that will be UCameraShakeBase
Here it is
Set up like this
Alright, I'll name this camera shake
Let's go to where we shot the gun earlier
It should be located below
Here, this is it
Let's make sure to include the shake here as well when we do this
So we'll only do the camera shake
When the shake variable is not null
So to shake the camera
We need to access
The player controller's camera manager
Or player camera manager
I'll use Get World
To retrieve Get First Player Controller
Here you can see there's a Player Camera Manager
Here you'll find constants like Start Camera Shake
Here we'll put in
the T-SubclassOf type we created earlier
Let's assign camera shake here like this
Well, actually, going straight ahead like this
Can be a bit risky
For example, while it's unlikely
Most of the time when we fetch this
It could be null, right?
So, in cases like this, it's often a good practice
To assign it to a variable
Check if it's null, and then proceed accordingly
Well, since we don't expect that possibility
I'll go ahead and write it like this
I'll go ahead and compile it in Unreal
And we'll have to fill up the value
Here you select Class Defaults
Right here
Then choose our BP Camera Shake that we created
Replace it with that, compile
And then run it to see if the camera shakes when you shoot
It shakes quite vigorously
Now you can adjust
These shaking values to your liking
Let's adjust the settings then
I'll set this one to 0.1
this one to 1
And it seems the duration of the shake is a bit long
So I'll reduce it to 0.1
This one here, maybe we can speed it up a bit
Let's try 0.5
You can continue tweaking these values
Until you find the right fit
If it's too intense, it might not feel right
So adjusting like this
Should help you
Decide what works best
let's try making some sound
To make sound, we need to look at
Where the sound files are located in our model
Right here
Rifle
Let me go ahead and play this sound for you
Similarly, let's go to the main character header file
Right here
Put in UPROPERTY
And EditAnywhere
Write Class
USoundbase
Like this
And then BulletSound
So we've been filling this out
In the blueprint all along
Shall we fill this in the constructor this time?
Let's go to the constructor of the protagonist here
Come down to the bottom, and fill it out
We'll also use Constructor Helpers for this
Put in FObjectFinder
This one should be USoundBase
And then TempSound
I mean TempBulletSound
I'll name it like this
Then we need to know the path for that
Select it like this, and you can copy the reference for this as well
I'll paste it here
And it should look like this
And for the asset
Like this, using F-Object Finder from Constructor Helpers
Alternatively, you can use F-Class Finder
To read everything at once
After doing this
Write If TempBulletSound
If it succeeded
Assign BulletSound to the variables
You can directly insert the object of
Temp-Bullet Sound here
You just need to insert it like this
There's also the option to insert it through a function like this
They are the same
Ultimately, since the object's type is
Exactly what we want as the sound base
You can simply insert it like this
Do this and save it
Compile again and see the result
But since we've made change in the header file
In that case, it's usually a good idea to reopen Unreal Engine
Then, we should definitely try shooting the gun to see
Let's move down and shoot the gun
Right here
I'll make the sound
If Bullet Sound is not null
Then we can proceed like this
UGameplayStatics PlaySound2D
Let's use this one
Here I add in the world
And then the sound file
Then, put in GetWorld and BulletSound
Let's compile this in Unreal Engine and see the results
Run it and you should see that the sound plays correctly
I will now summarize the content we learned in this chapter
Animation Montage creation Animation Montage Produce animation Montage using the Fire Rifle Inronsights asset in the Animations folder
Drag Fire Rifle Ironsights from the asset browser and place it into the slot Modified the animation montage blend time
An animation montage applied via blueprint Inserted a shooting animation into the middle of the existing animations in the ABP TPSP animation blueprint
Set spine 01 as the pivot point to divide the upper body for shooting and the lower body for movement animations in handling
Blueprint applied animation montage Declared the shooting animation montage and its playback function in TPSPlayer.h file
Added the implementation of the shooting animation playback function in TPSPlayer.cpp file Assign Montage_Fire to the Fire Anim Montage field in the details panel of BP TPSPlayer
Handling camera motion during shooting Handling camera motion during shooting perspective Create a Blueprint Class inheriting from CameraShakeBase and name it BP CameraShake
Set Root Shake Pattern to Wave Oscillator Camera Shake Pattern and assign values to each property
Declared properties for camera shake handling in TPSPlayer.h file Using the GetFirstPlayerController function to access the PlayerCameraManager
Getting the member variable and passing it as an argument to the StartCameraShake function
In the details panel, assign BP CameraShake to the Camera Shake field Handling the playback of shooting sound Added a variable for bullet firing sound in TPSPlayer.h file
Declared a variable TempBulletSound of type USoundBase using ConstructorHelpers' FObjectFinder in TPSPlayer.h
Copied and pasted the asset path for Rifle Sound Wave into TEXT as a constructor argument When the load is successful using the Succeeded function
Assigned the loaded value to the BulletSound variable Played BulletSound using UGameplayStatics' PlaySound2D function