Hello!
Im trying to smoothly rotate my character class to Look At a specified position.
I wrote this function and it works as expected:
void AMainPlayer::LookAt(FVector Target)
{
FVector Direction = (Target - ControlledPawn->GetActorLocation()).GetSafeNormal();
FRotator TargetRotation = Direction.ToOrientationRotator();
TargetRotation.Pitch = 0.0f;
TargetRotation.Roll = 0.0f;
SetActorRotation(TargetRotation);
}
However, if i try to make the rotation smooth the character stops Rotating.
This is the code i wrote with my attempts to Debug.
void AMainPlayer::LookAt(FVector Target)
{
UE_LOG(LogTemp, Warning, TEXT("Actor: %s"), *GetName());
UE_LOG(LogTemp, Warning, TEXT("StartingRotation: %s"), *GetActorRotation().ToString());
FVector Direction = (Target - GetActorLocation()).GetSafeNormal();
FRotator TargetRotation = Direction.ToOrientationRotator();
TargetRotation.Pitch = 0.0f;
TargetRotation.Roll = 0.0f;
FRotator NewRotation = FMath::RInterpConstantTo(
GetActorRotation(),
TargetRotation,
GetWorld()->GetDeltaSeconds(),
RotationSpeed
);
SetActorRotation(NewRotation);
UE_LOG(LogTemp, Warning, TEXT("EndingRotation: %s"), *GetActorRotation().ToString());
}
The unexpected behaviour I noticed is that StartingRotation always gets printed as (0, 0, 0) and EndingRotation is printed correctly.
It looks like The Actor Rotation always gets Reset at the beginning of the frame.
I tried to figure this by rotating the Character by a fixed amount in the Tick Function but instead of rotating, the Character keeps a 45 degrees rotation every single frame
// Called every frame
void AMainPlayer::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FRotator NewRotator = FRotator::ZeroRotator;
NewRotator.Yaw = 45.0f;
SetActorRotation(GetActorRotation() + NewRotator);
//LookAt(GetLocationUnderCursor());
}
This seems to confirm that GetActorRotation() always returns (0, 0, 0) at the beginning of every frame before i manually set it.
Why is it behaving like this? Is it an expected feature of Character classes?
The project is fresh and the only other things I touched the mesh position in the Blueprint and a basic system using Blueprints
I also defined the GetLocationUnderCursor function but that’s a const function and it works as expected