I am trying to get a 2D Sprite to change Flipbooks based on which direction it’s moving/facing. I followed a tutorial that worked well with the old input mapping, but it doesn’t seem to work with the new Enhanced input.
Here is a copy of the Function that is supposed to track IF the character is moving. And if they are, track which direction, and then, based on that direction, the sprite will use a different Flipbook. The game runs, and everything is “working,” but when I look at the “bIsMoving” boolean during play, it doesn’t trigger. So, in my mind, it means the math used to determine movement and direction doesn’t work.
Can someone help me learn how I can refactor my code to track movement with the new input please? Thank you!
void APBaseCharacter::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)
{
if (ForwardSpeed > 0.0f && abs(RightSpeed) < 0.5f)
{
CurrentAnimationDirection = PAnimationDirection::Up;
}
else if (ForwardSpeed > 0.5f && RightSpeed >= 0.5f)
{
CurrentAnimationDirection = PAnimationDirection::UpRight;
}
else if (ForwardSpeed > 0.5f && RightSpeed <= -0.5f)
{
CurrentAnimationDirection = PAnimationDirection::UpLeft;
}
else if (ForwardSpeed < 0.5f && abs(RightSpeed) <= 0.5f)
{
CurrentAnimationDirection = PAnimationDirection::Down;
}
else if (ForwardSpeed < -0.5f && RightSpeed >= 0.5f)
{
CurrentAnimationDirection = PAnimationDirection::DownRight;
}
else if (ForwardSpeed < -0.5f && RightSpeed <= -0.5f)
{
CurrentAnimationDirection = PAnimationDirection::DownLeft;
}
else if (abs(ForwardSpeed) < 0.5f && RightSpeed > 0.0f)
{
CurrentAnimationDirection = PAnimationDirection::Right;
}
else
{
CurrentAnimationDirection = PAnimationDirection::Left;
}
}
}