I’ve run into an odd situation while working on some third person perspective camera features – I’d like to be able to rotate the camera around the player character while also keeping it pointed at the character. To that end, I’ve established a vector at the character’s feet that I rotate the camera’s relative location vector around (updating it with the camera’s Yaw so we can rotate around the character no matter where the camera is in the XY plane). At the same time, I keep the camera looking at the character by changing its world space rotation to be equal to the rotation of the vector difference between the character’s world space location and the camera’s world space location.
The code looks like this:
FRotator CamYaw(0.f, ThirdPersonCameraComponent->GetRelativeRotation().Yaw, 0.f);
// The camera starts behind the character, and both camera and character's starting right vector is the Y axis.
// Since the character cannot pitch or roll, we can just consider the Y axis to be our rotation axis,
// provided we rotate it in the XY plane to stay in sync with the camera's Yaw.
FVector SpinningMayaVector = CamYaw.RotateVector(FVector(0.f, 1.f, 0.f));
// Value is an axis input value from the mouse indicating how many degrees we should rotate
UE_LOG(LogTemp, Warning, TEXT("LookUp; before rotation over spinningmayavector %s by %f degrees, our offset vector says %s"), *SpinningMayaVector.ToString(), -Value, *ThirdPersonCameraComponent->GetRelativeLocation().ToString());
// The new offset vector will be the current relative location vector rotated around the SpinningMayaVector (our character axis adjusted for camera Yaw, defined above)
FVector OffsetVector = ThirdPersonCameraComponent->GetRelativeLocation().RotateAngleAxis(-Value, SpinningMayaVector);
ThirdPersonCameraComponent->SetRelativeLocation(OffsetVector);
UE_LOG(LogTemp, Warning, TEXT("LookUp; after rotation over spinningmayavector %s by %f degrees, our offset vector says %s"), *SpinningMayaVector.ToString(), -Value, *OffsetVector.ToString());
// update cam facing to face the character
FRotator FaceMaya = (GetActorLocation() - ThirdPersonCameraComponent->GetComponentLocation()).Rotation();
UE_LOG(LogTemp, Warning, TEXT("LookUp; prior to 3PP cam world rot to lookit Maya, its world rotation is %s. The direction rotator says %s"), *ThirdPersonCameraComponent->GetComponentRotation().ToString(), *FaceMaya.ToString());
ThirdPersonCameraComponent->SetWorldRotation(FaceMaya);
UE_LOG(LogTemp, Warning, TEXT("LookUp; after 3PP cam world rot to lookit Maya, its world rotation is %s"), *ThirdPersonCameraComponent->GetComponentRotation().ToString());
Attached is a video of the strange effect I’m seeing when I try to rotate over the top of the character’s head (-90 degree pitch if we stay in the original orientation with right vector as the Y axis) – it looks like the offset vector rotation gets ‘stuck’ and the camera rotation jumps around for some reason. Any idea what’s going on here and how I can fix it?