So anyways I am pretty new to C++. Did a lot of Blueprint Tutorials and decided to do some C++ Tutorials to learn C++. I am also new to the forum and if I mess this up I’ll edit. Currently I have two Functions one is to detect which direction my Character is facing based off an Enum and setting the Enum to that direction. The other is to Animate the character according to what Enum is set. So if i’m facing Up it will do the UpWalk Sprite Flipbook ect. Now all I want to do is to get that same Enum that was set and when I press the Attack button to Attack according to what was set on the Enum and that is where I fail. I don’t know what I am doing wrong because there is no errors when I debug and when I compile. Below is the Code and a Screenshot of the InputAction Nodes I created in Blueprints.
Edit: Sorry Functions that are used are Animate, SetCurrentAnimationDirection and AttackAnimate.
#include "PaperCharacterBase.h"
#include "..\Public\PaperCharacterBase.h"
APaperCharacterBase::APaperCharacterBase()
{
bAttacking = true;
OnCharacterMovementUpdated.AddDynamic(this, &APaperCharacterBase::Animate);
GetCapsuleComponent()->SetCapsuleRadius(70.f);
}
void APaperCharacterBase::BeginPlay()
{
Super::BeginPlay();
OnAttackTimerEndNative.AddUObject(this, &APaperCharacterBase::OnAttackEndNative);
}
void APaperCharacterBase::Animate(float DeltaTime, FVector OldLocation, const FVector OldVelocity)
{
TOptional<FMinimalViewInfo> ViewInfo;
if (!IsPlayerControlled())
{
UWorld* World = GetWorld();
if (World)
{
APlayerController* PlayerController = World->GetFirstPlayerController();
if (PlayerController)
{
ACharacter* Character = PlayerController->GetCharacter();
if (Character)
{
Character->CalcCamera(DeltaTime, ViewInfo.Emplace());
}
}
}
}
SetCurrentAnimationDirection(OldVelocity, ViewInfo);
if (OldVelocity.Size() > 0.0f || GetCharacterMovement()->IsFalling())
{
switch (CurrentAnimationDirection)
{
case EPaperAnimationDirection::Up:
GetSprite()->SetFlipbook(PaperFlipbooks.WalkUp);
break;
case EPaperAnimationDirection::Down:
GetSprite()->SetFlipbook(PaperFlipbooks.WalkDown);
break;
case EPaperAnimationDirection::Left:
GetSprite()->SetFlipbook(PaperFlipbooks.WalkLeft);
break;
case EPaperAnimationDirection::Right:
GetSprite()->SetFlipbook(PaperFlipbooks.WalkRight);
break;
case EPaperAnimationDirection::UpLeft:
GetSprite()->SetFlipbook(PaperFlipbooks.WalkUpLeft);
break;
case EPaperAnimationDirection::UpRight:
GetSprite()->SetFlipbook(PaperFlipbooks.WalkUpRight);
break;
case EPaperAnimationDirection::DownLeft:
GetSprite()->SetFlipbook(PaperFlipbooks.WalkDownLeft);
break;
case EPaperAnimationDirection::DownRight:
GetSprite()->SetFlipbook(PaperFlipbooks.WalkDownRight);
break;
}
}
else
{
switch (CurrentAnimationDirection)
{
case EPaperAnimationDirection::Up:
GetSprite()->SetFlipbook(PaperFlipbooks.IdleUp);
break;
case EPaperAnimationDirection::Down:
GetSprite()->SetFlipbook(PaperFlipbooks.IdleDown);
break;
case EPaperAnimationDirection::Left:
GetSprite()->SetFlipbook(PaperFlipbooks.IdleLeft);
break;
case EPaperAnimationDirection::Right:
GetSprite()->SetFlipbook(PaperFlipbooks.IdleRight);
break;
case EPaperAnimationDirection::UpLeft:
GetSprite()->SetFlipbook(PaperFlipbooks.IdleUpLeft);
break;
case EPaperAnimationDirection::UpRight:
GetSprite()->SetFlipbook(PaperFlipbooks.IdleUpRight);
break;
case EPaperAnimationDirection::DownLeft:
GetSprite()->SetFlipbook(PaperFlipbooks.IdleDownLeft);
break;
case EPaperAnimationDirection::DownRight:
GetSprite()->SetFlipbook(PaperFlipbooks.IdleDownRight);
break;
default:
break;
}
}
if (GetCharacterMovement()->IsFalling())
{
GetSprite()->SetPlayRate(0.f);
GetSprite()->SetPlaybackPositionInFrames(0, true);
}
else
{
GetSprite()->SetPlayRate(1.f);
}
}
void APaperCharacterBase::SetCurrentAnimationDirection(const FVector& Velocity, TOptional<FMinimalViewInfo> ViewInfo)
{
FVector Forward;
FVector Right;
if (ViewInfo.IsSet())
{
Forward = UKismetMathLibrary::GetForwardVector(ViewInfo.GetValue().Rotation);
Right = UKismetMathLibrary::GetRightVector(ViewInfo.GetValue().Rotation);
}
else
{
Forward = GetActorForwardVector().GetSafeNormal();
Right = GetActorRightVector().GetSafeNormal();
}
const float ForwardSpeed = FMath::Floor(FVector::DotProduct(Velocity.GetSafeNormal(), Forward) * 100) / 100;
const float RightSpeed = FMath::Floor(FVector::DotProduct(Velocity.GetSafeNormal(), Right) * 100) / 100;
bIsMoving = ForwardSpeed != 0.0f || RightSpeed != 0.0f;
if (bIsMoving && !GetCharacterMovement()->IsFalling())
{
if (ForwardSpeed > 0.0f && abs(RightSpeed) < 0.5f)
{
CurrentAnimationDirection = EPaperAnimationDirection::Up;
}
else if (ForwardSpeed > 0.5f && RightSpeed >= 0.5f)
{
CurrentAnimationDirection = EPaperAnimationDirection::UpRight;
}
else if (ForwardSpeed > 0.5f && RightSpeed <= -0.5f)
{
CurrentAnimationDirection = EPaperAnimationDirection::UpLeft;
}
else if (ForwardSpeed < 0.5f && abs(RightSpeed) <= 0.5f)
{
CurrentAnimationDirection = EPaperAnimationDirection::Down;
}
else if (ForwardSpeed < -0.5f && RightSpeed >= 0.5f)
{
CurrentAnimationDirection = EPaperAnimationDirection::DownRight;
}
else if (ForwardSpeed < -0.5f && RightSpeed <= -0.5f)
{
CurrentAnimationDirection = EPaperAnimationDirection::DownLeft;
}
else if (abs(ForwardSpeed) < 0.5f && RightSpeed > 0.0f)
{
CurrentAnimationDirection = EPaperAnimationDirection::Right;
}
else
{
CurrentAnimationDirection = EPaperAnimationDirection::Left;
}
}
}
void APaperCharacterBase::AttackAnimate(const float TimerValue)
{
if (bAttacking)
{
switch (CurrentAnimationDirection)
{
case EPaperAnimationDirection::Down:
GetSprite()->SetFlipbook(PaperFlipbooks.AttackDown);
break;
case EPaperAnimationDirection::Up:
GetSprite()->SetFlipbook(PaperFlipbooks.AttackUp);
break;
case EPaperAnimationDirection::Right:
GetSprite()->SetFlipbook(PaperFlipbooks.AttackRight);
break;
case EPaperAnimationDirection::Left:
GetSprite()->SetFlipbook(PaperFlipbooks.AttackLeft);
break;
case EPaperAnimationDirection::DownRight:
GetSprite()->SetFlipbook(PaperFlipbooks.AttackDownRight);
break;
case EPaperAnimationDirection::DownLeft:
GetSprite()->SetFlipbook(PaperFlipbooks.AttackDownLeft);
break;
case EPaperAnimationDirection::UpRight:
GetSprite()->SetFlipbook(PaperFlipbooks.AttackUpRight);
break;
case EPaperAnimationDirection::UpLeft:
GetSprite()->SetFlipbook(PaperFlipbooks.AttackUpLeft);
break;
default:
break;
}
bAttacking = false;
GetWorldTimerManager().SetTimer(AttackTimer,
[this]()
{
bAttacking = true;
if (OnAttackTimerEndNative.IsBound())
{
OnAttackTimerEndNative.Broadcast();
}
},
TimerValue, false);
}
}
void APaperCharacterBase::OnAttackEndNative()
{
if (OnAttackTimerEnd.IsBound())
{
OnAttackTimerEnd.Broadcast();
}
}