[UE4] Animation Montage C ++ related Notes

[UE4] Animation Montage C ++ related Notes

https://zhuanlan.zhihu.com/p/47767283

 

C ++ How to play the animation Montage

Assumption is performed within Character, wherein MontagePtr Montage pointer is:

float DeathAnimDuration = PlayAnimMontage(MontagePtr) / (GetMesh() && GetMesh()->GlobalAnimRateScale > 0 ? GetMesh()->GlobalAnimRateScale : 1);

If you just play Animation Sequence, you can use PlayAnimation ()

if (USkeletalMeshComponent *Mesh = GetMesh())
{
    Mesh->PlayAnimation(AnimSequence, false);
}
Note: PlayAnimation can directly play AnimSequence, but not mixing action. For example, when the attack after playing action, can not automatically return to the idle state, but can be done automatically by switching operation of the Montage Slot.

其他参考:
How can I play animations strictly from C++?
https://answers.unrealengine.com/questions/292345/how-can-i-play-animations-strictly-from-c.html

C ++ dynamic player Montage (created by AnimSequence)

keywords: UE4, C ++, AnimSequence, AnimMontage, Slot, dynamic creation, animation plays

To add the first node in the animation Slot blueprint, the default name for the Slot DefaultSlot. This name will be used in C ++ code

Assumed to have been acquired AnimSequence object pointer to be played: AnimSeq

float AnimLength = 0.f;
if (USkeletalMeshComponent *Mesh = MyActor->FindComponentByClass<USkeletalMeshComponent>())
{
    if (UAnimInstance *AnimInst = Mesh->GetAnimInstance())
    {
        if (UAnimMontage* Mtg = AnimInst->PlaySlotAnimationAsDynamicMontage(AnimSeq, TEXT("DefaultSlot"), 0.25f, 0.25f, 1.f, 1))
        {
            AnimLength = Mtg->GetPlayLength();
        }
    }
}
Use UAnimInstance :: PlaySlotAnimationAsDynamicMontage () when playing Montage, Animation Sequence parameters in Notify still valid Montage dynamically generated, namely: Montage will trigger the same Notify.

 

Use PlaySlotAnimationAsDynamicMontage () during playback Montage, if not loop the Montage, be sure to play after execution UAnimInstance :: Montage_Stop () stopped the current Montage, otherwise it will interfere with logical blueprint for the animation, resulting in ineffective when switching to other Montage.

Let gesture remains the last one when playing the animation Montage

keywords: UE4, Montage, C ++, playback speed, playback rate, accelerated playback, slow playback

Three modes
Mode 1: In that want to freeze frame, the SkeletonComponent of GlobalAnimRateScale property is set to 0.
For example, you want to freeze the role of bone in the third frame, then set the property at the time of the third frame of the animation to 0.

ACharacter::GetMesh()->GlobalAnimRateScale = 0.f;

Option 2:

ACharacter::GetMesh()->bNoSkeletonUpdate = true;

Mode 3:

ACharacter::GetMesh()->bPauseAnims = true;
Note: Normal finished playing AnimSequence, if not set to loop playback, playback will automatically stop after completion of the last one. Montage only play when you need to set more properties.

Specified in Section C ++ played Montage

Embodiment 1:
Specify Section third parameter StartSectionName

float ACharacter::PlayAnimMontage(class UAnimMontage* AnimMontage, float InPlayRate = 1.f, FName StartSectionName = NAME_None);

Option 2:
UseUAnimInstance::Montage_JumpToSection()

if (USkeletalMeshComponent *Mesh = Character->GetMesh())
{
    if (UAnimInstance *AnimInst = Mesh->GetAnimInstance())
    {
        Char->PlayAnimMontage(MontagePtr);
        AnimInst->Montage_JumpToSection(FName("step_03"), MontagePtr);
    }
}
Make sure that the character animation is in a state of play of the implementation of Section belongs Montage Montage_JumpToSection, otherwise JumpToSection invalid.

Montage Section invalid switch problem (2018-02-11)

keywords: UE4 Montage Section not working

Symptoms:
SetNextSection in the editor works fine, but is not valid in the packaged version.

Solution:
The SetNextSection replaced JumpToSection.

参考自:Montage unwanted section switching
https://answers.unrealengine.com/questions/172537/montage-unwanted-section-switching.html

Section If the time is very short, and the need to Blend In Blend Out piecemeal or set to 0 (default is 0.25), or JumpToSection no effect.

Montage Loop

If the switching control animation animation blueprint, the state machine directly to specify the animation can AnimSequence, no other settings, because the animation loop blueprint to automatically specified according to the state machine AnimSequence.
If you use C ++ to play Montage, you need to pay attention to the following details:

  1. If you are using existing resources Montage, Montage needs to be set to loop, as follows:
    Open the Montage editing window, first click position 1, and then click the position 2 (both the left mouse button click)

Preview Section and then the progress bar will have one X, Montage to loop at this time represents a

After the re-execution ACharacter::PlayAnimMontage()can automatically loop a specified Montage. PlayAnimMontage () only need to do this once.

2. If Montage dynamically create and play, you will need LoopCountto be large enough, such as 9999. In this way the current Montage will loop:

AnimInstance->PlaySlotAnimationAsDynamicMontage(AnimSeq, TEXT("DefaultSlot"), 0.25f, 0.25f, 1.f, 9999));
This way is feasible: Montage loop is not provided, but a long time is detected when played on Montage kept in the cumulative Tick when aired immediately perform a next playback. The answer is not possible! ! The net effect is: there will be between two players Montage short time to switch to the Idle state animation.

 

Montage stop playing

If you want to stop the currently playing Montage, execute StopAnimMontage():

void ACharacter::StopAnimMontage(class UAnimMontage* AnimMontage);
Animation fusion effect, remember to Montage's Blend Out Time greater than 0, such as the default value of 0.25 if you want to stop playing Montage.

No integration problems when switching Montage

Reason:
Montage parameter Blend Inis set to 0.

Solution:
The Montage parameters Blend In(Blend Option -> Blend In - > Blend Time) change is greater than 0, such as the default value of 0.25.

 

 

 

 

 

 

 

 

 

 

 

Published 80 original articles · won praise 38 · views 40000 +

Guess you like

Origin blog.csdn.net/kuangben2000/article/details/104488973