My goal for this update was to add a perfect dodge mechanic that allows a super punch when executed and charged correctly.
Enemy CPP
void ACompetitionEnemy::Tick(float DeltaTime)
...
if (GetWorld()->GetTimerManager().IsTimerActive(PunchTimerHandle) == true)
{
//Check if timer is within perfect dodge range
if (GetWorld()->GetTimerManager().GetTimerElapsed(PunchTimerHandle) >= 0.7f)
{
//set dodge window in player
ACompetitionCharacter* Player = Cast<ACompetitionCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
Player->DodgeWindow = true;
}
else
{
//set dodge window to false if its out of range
ACompetitionCharacter* Player = Cast<ACompetitionCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
Player->DodgeWindow = false;
This code will cast to the player during the timer that plays while the punch is winding up. If the timer is above a certain threshold, the enemy casts to the player and sets “DodgeWindow” to true. Otherwise it’ll still cast, but set the variable to false.
Player CPP file
void ACompetitionCharacter::Dodge()
...
if (Controller && (DirectionValue != 0.f) && PlayerState == "Inactive")
{
...
//if dodge is executed in the dodge window, slow global time
if (DodgeWindow == true)
{
UGameplayStatics::SetGlobalTimeDilation(GetWorld(), 0.1f);
PlayerState = "PerfectDodge";
PerfectDodge();
}
else
{
PlayerState = "Dodging";
...
void ACompetitionCharacter::PerfectDodge()
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Spam Click to charge the punch"));
//set a timer for the super punch
GetWorldTimerManager().SetTimer(PerfectDodgeHandle, this, &ACompetitionCharacter::SuperPunch, 1.0f, false);
}
If the player dodges within the window, the global time will slow and they will be prompted to click rapidly in order to charge the “Super Punch”.
void ACompetitionCharacter::Punch(const FInputActionValue& Value)
...
//Each click will charge the punch
if (PlayerState == "PerfectDodge")
{
PunchCharge += 1;
}
...
void ACompetitionCharacter::SuperPunch()
{
//this is where to set the charge limit
if (PunchCharge >= 10)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("SuperPunchhhhhhhh"));
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("NotEnoughCharge"));
}
//reset time speed
UGameplayStatics::SetGlobalTimeDilation(GetWorld(), 1.0f);
Here, each click of the mouse will be converted to a point of “PunchCharge”. Once the timer from the “PerfectDodge” function has finished, “SuperPunch” is executed. Within this function the charge is checked against a number and if the player reaches this threshold, a punch will be thrown (I haven’t added this yet to save time before adding animations). Either way, the time speed will be reset at the end of the function.