Play Montage
Official Documentation: docs.unrealengine.com.
Description
Play Animation Montage on the character mesh. Returns the length of the animation montage in seconds, or 0.f if failed to play.
Useful to Play an Animation for a certain Event
or as an Action, like a Attack Animation
.
Define Variables
class AYourCharacter : public ACharacter
{
...
// Some Function to Play the Montage from
// In this example we're playing a Attack Animation
// This fucntion is bound to the Left Mouse Button, please check out EnhancedInput Example to implement Input
void Attack(const FInputActionValue& Value);
// For this example, we'll use an Array, but you can use a single UAnimMontage:
// UAnimMontage* SingleAnimation;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "YourGame|Animation")
TArray<UAnimMontage*> YourAnimations;
// These variables will help us define, which Animation to play
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "YourGame|Animation")
int32 ComboCount = 0; // current animation playing
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "YourGame|Animation")
int32 MaxCombo = 4; // the number of animations in your array
// Build your project in Visual Studio
// Next, go to Unreal's Editor - in your Character Blueprint
// under `YourGame|Animation` set as many Animation Montages as you want in that Array
// or just select the one if you defined a single animation montage
//
// You can also just use a single AnimMontage: UAnimMontage* YourMontage;
...
}
void AYourCharacter::Attack(const FInputActionValue& Value)
{
// call PlayAnimMontage, which is inherited from Character.h
// In the Arguments either add the Single AnimMontage of yours
// or the Array with multiple Animations as I did here:
PlayAnimMontage(YourAnimations[ComboCount]);
// Single AnimMontage:
// PlayAnimMontage(SingleAnimation);
// after playing the animation increase the ComboCount by 1
ComboCount++;
// if ComboCount is bigger or equal to MaxCombo, reset ComboCount to 0
if (ComboCount >= MaxCombo)
{
ComboCount = 0;
}
}
Optional:
Hit detection and dealing Damage with Animation Notifies
in C++ and minimal BP:
-
In the Editor:
Create an Animation Montage from your Animation Sequence, if you didn't already. -
Add
Notifiers
, by right-clicking your Notifies Track and selectNew Notify...
:
In my case I added Hit
and HitEnd
. You can call them however you want, but keep the naming in mind for later.
- In your
Characters Animation Blueprint
you can now add theNotifiy Events
. Target isYourCharacter Class
, you can just Cast to your Character Class.
@Note AnimNotify_[DefinedNameFromBefore]
:
- Where is this Function
OnNotifyBegin
coming from and whats it for?
In your Character Class add the following:
class AYourCharacter : public ACharacter
{
// define the function that should execute when the Notifies hit
UFUNCTION(BlueprintCallable)
void OnNotifyBegin(FName NotifyName);
}
Finally, lets implement some logic, when the Animation Notifiers
"Hit". (no pun intended)
void AYourCharacter::OnNotifyBegin(FName NotifyName)
{
if (NotifyName == "Hit")
{
// do something when the Notify "Hit" hits
// in my case I enable and disable the collision of my characters sword
if (EquippedSword)
{
EquippedSword->bIsCollisionEnabled = true;
}
}
if (NotifyName == "HitEnd")
{
// do something when the Notify "HitEnd" hits
// and disable collision on "HitEnd"
if (EquippedSword)
{
EquippedSword->bIsCollisionEnabled = false;
}
}
}
This was an example on how to Play Montages in Unreal Engine C++.