Character Idle Animations
MedievalCombatProject
If the Player does not perform any action on the Character for an amount of time(taken as 10 seconds), a C++ function “IdleEnd” gets called.
The code below in Main’s Tick function sets a Timer if the Player is not moving.
// If IdleTimer is not active and Player is not crouched
if (!GetWorldTimerManager().IsTimerActive(IdleAnimTimerHandle) && !bCrouched)
{
// If an Idle Animation is not already playing AND the Player is not moving
if (IdleAnimSlot == 0
&& (Cast<UMainAnimInstance>(GetMesh()->GetAnimInstance())->MovementSpeed == 0))
{
// Start the Timer and set it for IdleTimeLimit seconds.
GetWorldTimerManager().SetTimer(IdleAnimTimerHandle, TimerDel, IdleTimeLimit, false);
}
}
If the Player performs any action, the IdleTimer
is paused and the IdleAnimSlot
is set to zero.
ResetIdleTimer()
void AMain::ResetIdleTimer()
{
// Reset the Idle Anim Slot
IdleAnimSlot = 0;
// Pause IdleAnimTimerHandle if Active
if (GetWorldTimerManager().IsTimerActive(IdleAnimTimerHandle))
{
GetWorldTimerManager().PauseTimer(IdleAnimTimerHandle);
}
}
The below function chooses one random number from [0 -> NumberOfAnimations - 1] and assigns this value to a variable called IdleAnimSlot. NumberOfAnimations is unique for every Player State (i.e UnarmedNormal, Armed-OneHanded).
IdleEnd()
/**
* Every time after an Idle Animation plays, the base Idle Animation should be played.
* After the base Idle Animation is complete, one of the other Idle Animations will be randomly chosen.
*/
void AMain::IdleEnd(int32 PlayerStatusNo)
{
// If Character was in Original Idle state, play an Idle Animation.
if (IdleAnimSlot == 0)
{ // [0 -> NumberOfIdleAnims - 1]
IdleAnimSlot = FMath::RandRange(1, NumberOfIdleAnims[PlayerStatusNo] - 1);
}
// else return back to the Original Idle state.
else IdleAnimSlot = 0;
}
In the Character’s Anim Blueprint, out of a set of Idle Animations, one will be chosen at random and will play on the Character.
Once that Idle Animation finishes playing, it will return to the original Idle State and will wait again for the same amount of time (10 seconds), playing another Idle Animation if the Player does not do anything even then.
You can view the code of the project here!
In Action
- While Unarmed and not in Combat Mode
- While Unarmed and in Combat Mode
- While Armed with a One-Handed Weapon
- While Armed with a Two-Handed Weapon(OLD)
(Bug: Character rises above the ground. Will be patched soon.)Fixed 28/07/20
- While Armed with a Two-Handed Weapon(NEW)