Related with my previous question: Rotate Character Towards Object - C++
I managed to make my character rotate and move in front the center of the object, but how do I smooth it out? I saw another Question Rotate the character exactly to the center from the object which does it in BP using a timeline, but I was wondering if there is a way to do it in C++ without using a timeline?
It’s a FPS, my camera is attached to the head socket (my character has the mesh of the whole body since I need it for some stuff), if this can help.
EDIT: Before someone points it out: I know about the whole Interpolation ufnctions FMath comes with, but none of these seem to work when it comes to the rotation.
This is what I have so far.
The Interp on the SetActorLocation seems to be working.
The on on the Rotation is not.
void AMyCharacter::OrientTowardsTarget(AItem* Target)
{
if (Target)
{
FVector TargetLocation = Target->GetActorLocation();
FVector TargetForward = Target->GetActorForwardVector();
FVector DesiredPosition = TargetLocation + (TargetForward * 20.0f);
// Smoothly interpolate position
FVector CurrentLocation = GetActorLocation();
FVector SmoothedPosition = FMath::VInterpTo(CurrentLocation, DesiredPosition, 1.5f, 1.0f);
SetActorLocation(SmoothedPosition);
// Calculate desired rotation
FRotator TargetRotation = UKismetMathLibrary::FindLookAtRotation(GetActorLocation(), TargetLocation);
// Smoothly interpolate rotation
FRotator CurrentRotation = GetActorRotation();
FRotator SmoothedRotation = FMath::RInterpTo(CurrentRotation, TargetRotation, 3.f, 1.0f);
SmoothedRotation.Roll = 0.0f;
SmoothedRotation.Pitch = 0.0f;
GetController()->SetControlRotation(TargetRotation);
}
}