AnimMontage Playing... Sort of

Hello Community,

It’s been a while since my last question. So here I am again… oh boy. Anyways…

The Issue
The issue I am having is I have created an animation montage for my model. In the editor I can see it working just fine. However, I want to be able to call these on demand via code and it’s causing abit of a problem. I’ve tested to make sure that it is infact running the montage but when looking at the character it’s simply “stuck” is the best way I can put it.

A few ideas

I suspect that it’s either repeating over and over and over and over again (Even when I turn off the loop it is still going!) so I am pretty sure this is the issue. So the question is how do I fix this? If I want to play it one single time or loop continually while say, running, then what exactly would be the best solution for this? Thanks.

InputComponent->BindAction("Jump", IE_Pressed, this, &AThePlanetCharacter::Jump);

// ...

void AThePlanetCharacter::Jump()
{
	ACharacter::Jump();

	stuff = GetMesh();


	if (stuff->AnimScriptInstance->Montage_IsPlaying(Mon) == false)
	{
		stuff->AnimScriptInstance->Montage_Play(Mon, 1.0);
		GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::Red, FString::FString("I am definitely on!"));
	}

}

Update:

I’ve tried a few different things from UpdateAnimation(1.0) to simply the arrangement of the code. Still nothing yet. Any help would be appreciated. :slight_smile:

Another new update. I’ve tried the following from The Shooter Game Example and still nothing. Any assistance would be appreciated.

float AThePlanetCharacter::PlayAnimMontage(class UAnimMontage* AnimMontage, float InPlayRate, FName StartSectionName)
{

	GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::Yellow, FString::FString("I am in Anim Montage!"));

	USkeletalMeshComponent* UseMesh = GetMesh();
	if (AnimMontage && UseMesh && UseMesh->AnimScriptInstance)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::Yellow, FString::FString("I am returning the anim montage!"));
		return UseMesh->AnimScriptInstance->Montage_Play(AnimMontage, InPlayRate);
	}

	return 0.0f;
}


void AThePlanetCharacter::Jump()
{
	ACharacter::Jump();

	float DeathAnimDuration = PlayAnimMontage(Mon);

	GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::Green, FString::SanitizeFloat(DeathAnimDuration) + FString::FString(" Death Anim Duration"));

}

Ah, well… I finally figured it out after a few days of experimenting around. Turns out the montage needed a few settings checked in the actual montage. For instance the actual animation references start and end time.

26254-answer1.png

So anyways, the following code works perfectly. Now to blend 'em and to sync. Basically, if you plan on doing it via C++ then you’ll want to make sure that you follow this. Then do what you need to do via various functions and timers. Hopefully this helps you guys. It definitely helped me. :slight_smile:

float AThePlanetCharacter::PlayAnimMontage(class UAnimMontage* AnimMontage, float InPlayRate, FName StartSectionName)
{

	GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::Yellow, FString::FString("I am in Anim Montage!"));

	USkeletalMeshComponent* UseMesh = GetMesh();
	if (AnimMontage && UseMesh && UseMesh->AnimScriptInstance)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::Yellow, FString::FString("I am returning the anim montage!"));
		return UseMesh->AnimScriptInstance->Montage_Play(AnimMontage, InPlayRate);
	}

	return 0.0f;
}


void AThePlanetCharacter::Jump()
{
	ACharacter::Jump();

	float DeathAnimDuration = PlayAnimMontage(Mon);

	GEngine->AddOnScreenDebugMessage(-1, 5.0, FColor::Green, FString::SanitizeFloat(DeathAnimDuration) + FString::FString(" Death Anim Duration"));


}

26255-answer2.png