sivan:
I use no anim montages in my RTS project, only single node animations. To switch anim I would use an anim notify or checking anim percentage from Tick by this:
[FONT=courier new]float URTSSkeletalMeshComponent::GetAnimationPercentage()
{
float CurrentAnimPos = 0.f;
UAnimSingleNodeInstance* SingleNodeInstance = GetSingleNodeInstance();
if (SingleNodeInstance)
{
CurrentAnimPos = SingleNodeInstance->GetCurrentTime();
}
return CurrentAnimPos;
}
in my other project I have just started to use anim montages, and as a good source I checked Tom Looman’s Survival Game , where you can find a solution using a timer, here are the related functions (btw anim montages are blended into an Anim BP but might be okay for you):
void ASWeapon::OnEquip(bool bPlayAnimation)
{
bPendingEquip = true;
DetermineWeaponState();
if (bPlayAnimation)
{
float Duration = PlayWeaponAnimation(EquipAnim);
if (Duration <= 0.0f)
{
// Failsafe in case animation is missing
Duration = NoEquipAnimDuration;
}
EquipStartedTime = GetWorld()->TimeSeconds;
EquipDuration = Duration;
GetWorldTimerManager().SetTimer(EquipFinishedTimerHandle, this, &ASWeapon::OnEquipFinished, Duration, false);
}
else
{
/* Immediately finish equipping */
OnEquipFinished();
}
if (MyPawn && MyPawn->IsLocallyControlled())
{
PlayWeaponSound(EquipSound);
}
}
float ASWeapon::PlayWeaponAnimation(UAnimMontage* Animation, float InPlayRate, FName StartSectionName)
{
float Duration = 0.0f;
if (MyPawn)
{
if (Animation)
{
Duration = MyPawn->PlayAnimMontage(Animation, InPlayRate, StartSectionName);
}
}
return Duration;
}
void ASWeapon::OnEquipFinished()
{
AttachMeshToPawn();
bIsEquipped = true;
bPendingEquip = false;
DetermineWeaponState();
if (MyPawn)
{
// Try to reload empty clip
if (MyPawn->IsLocallyControlled() &&
CurrentAmmoInClip <= 0 &&
CanReload())
{
StartReload();
}
}
}
void ASWeapon::StartReload(bool bFromReplication)
{
/* Push the request to server */
if (!bFromReplication && Role < ROLE_Authority)
{
ServerStartReload();
}
/* If local execute requested or we are running on the server */
if (bFromReplication || CanReload())
{
bPendingReload = true;
DetermineWeaponState();
float AnimDuration = PlayWeaponAnimation(ReloadAnim);
if (AnimDuration <= 0.0f)
{
AnimDuration = NoAnimReloadDuration;
}
GetWorldTimerManager().SetTimer(TimerHandle_StopReload, this, &ASWeapon::StopSimulateReload, AnimDuration, false);
if (Role == ROLE_Authority)
{
GetWorldTimerManager().SetTimer(TimerHandle_ReloadWeapon, this, &ASWeapon::ReloadWeapon, FMath::Max(0.1f, AnimDuration - 0.1f), false);
}
if (MyPawn && MyPawn->IsLocallyControlled())
{
PlayWeaponSound(ReloadSound);
}
}
}
Thanks for the answer and posting the code. But as I replied to another replies. Carefully using a timer may eliminate the t-pose. But it not a solution if I have too many animations that have various blend time settings. It also defeats data-driven methodology.