In this lesson, you will learn how to bridge the gap between user input and animation playback by using Animation Notifies (AnimNotifies) to ensure your character’s gameplay logic triggers exactly when the visual movement occurs.
The Problem: Timing
When you bind an InputAction to a function—like PerformAttack()—the logic often executes instantly. However, a sword swing animation takes time to play. If your "damage" logic executes the moment the player presses a button, the player hits the enemy before the sword even moves. We need a way to link the high-level input action to a specific frame within the animation timeline.
Bridging with AnimNotifies
Instead of handling damage inside the Input Action’s callback, use your input event to trigger the Animation Montage. Then, inside the Animation Montage editor, you place an AnimNotify at the precise frame where the weapon impact occurs.
- Input Stage: The
InputActiontriggersPlayAnimMontage. - Animation Stage: The animation plays until it hits the
AnimNotify. - Execution Stage: Your Character class (or AnimInstance) catches the
Notifyand executes the gameplay logic (likeApplyDamage).
Implementation Concept
In your C++ character class, you define a function that responds to the notify. It is often best to keep this logic clean by binding it to the Animation Montage's internal event system:
// In your Character's function bound to the Input Action
void AMyCharacter::HandleAttackInput()
{
if (AttackMontage)
{
PlayAnimMontage(AttackMontage);
}
}
// In the Animation Notify event (or via an AnimNotifyState class)
void AMyCharacter::OnAttackHitNotify()
{
// Now trigger damage logic here, perfectly synced with the frame
PerformMeleeOverlapCheck();
}
By decoupling the input from the logic and using the animation itself as the "trigger," you create gameplay that feels tactile and responsive. The player presses the button, the animation starts, and the world reacts only when the weapon makes contact.
Key Takeaway: Never trigger gameplay-critical logic directly from an input binding if it depends on character movement; instead, trigger a Montage and use AnimNotifies to fire the logic at the perfect frame.