Hey, during development of my 2D platformer character prototype I met strange bug, which I couldn’t find solution for. When character is flipping (changing direction of the movement) I see strange flickering around the character, which shouldn’t happen. Sometimes character is rotating as expected without this strange visual bug, but very often this bug appears. I doesn’t change any animation.
Link to video:
Code fragment where I’m rotating my character (at the bottom):
void APlayerCharacter::Move(const FInputActionValue& Value)
{
// input is a float
FVector2D MovementVector = Value.Get<FVector2D>();
float MovementValue = MovementVector.X + MovementVector.Y;
if (Controller != nullptr)
{
UE_LOG(LogTemp, Warning, TEXT("Value: %f, %f"), MovementVector.X, MovementVector.Y);
// find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get forward vector
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
// add movement
AddMovementInput(ForwardDirection, MovementValue);
SetAnimationState(1);
if (movingDirection > 0 && MovementValue < 0) {
movingDirection = -1;
if (GetController()) {
GetRootComponent()->SetRelativeRotation(FRotator(0.0f, -180.0f, 0.0f));
}
}
else if (movingDirection < 0 && MovementValue > 0) {
movingDirection = 1;
if (GetController()) {
GetRootComponent()->SetRelativeRotation( FRotator(0.0f, 0.0f, 0.0f));
}
}
}
}