Hi everyone, I’m trying to make the Camera Rotate around the player by using the angle between the Spring Arm and the Target which in this case is the player, for that I’m using this code:
FRotator lookAtRot = FRotator(mainCameraSpring->RelativeRotation.Pitch ,UKismetMathLibrary::FindLookAtRotation(mainCameraSpring->RelativeLocation, mainPlayer->GetActorLocation()).Yaw, mainCameraSpring->RelativeRotation.Roll);
mainCameraSpring->SetRelativeRotation(lookAtRot);
Basically I only Want to move the camera around the player just like the blue arrow that I painted, for that I’m using the code in top to calculate the rotation factor.
I’m not sure what I’m missing to do this right because what happens is that the Camera moves around the X axis (the red axis).
Any idea how to solve this?
thanks.
Camera Movement Code:
void ACameraManager::CameraMovement(TEnumAsByte<TouchGestures> gesture)
{
if ((gesture & TouchGestures::Pinch) == TouchGestures::Pinch)
{
zoomLevel = FMath::Clamp(zoomLevel - 5 * CameraSpeed, minZoomLevel, maxZoomLevel);
}
if ((gesture & TouchGestures::SwipeLeftRight) == TouchGestures::SwipeLeftRight)
{
zoomLevel = FMath::Clamp(zoomLevel + 5 * CameraSpeed, minZoomLevel, maxZoomLevel);
}
if ((gesture & TouchGestures::SwipeUpDown) == TouchGestures::SwipeUpDown)
{
if (mainCameraSpring)
{
cameraSpringPosition.Y -= mainCameraSpring->RelativeLocation.RightVector.Y + (5 * CameraSpeed);
}
else if (mainCamera)
{
cameraSpringPosition.Y -= mainCameraSpring->RelativeLocation.RightVector.Y + (5 * CameraSpeed);
}
}
if ((gesture & TouchGestures::SwipeDownUp) == TouchGestures::SwipeDownUp)
{
if (mainCameraSpring)
{
cameraSpringPosition.Y += mainCameraSpring->RelativeLocation.RightVector.Y + (5 * CameraSpeed);
}
else if (mainCamera)
{
cameraSpringPosition.Y += mainCameraSpring->RelativeLocation.RightVector.Y + (5 * CameraSpeed);
}
}
}
And Camera Tick:
void ACameraManager::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
FString message = "Spring Arm Rotation: ";
message.Append("Yaw: ");
message.AppendInt(mainCameraSpring->RelativeRotation.Yaw);
message.Append(", Pitch: ");
message.AppendInt(mainCameraSpring->RelativeRotation.Pitch);
message.Append(", Roll: ");
message.AppendInt(mainCameraSpring->RelativeRotation.Roll);
message.Append(", Spring Arm Length: ");
message.AppendInt(mainCameraSpring->TargetArmLength);
GEngine->AddOnScreenDebugMessage(-1, DeltaSeconds, FColor::Red, message);
if (mainCameraSpring && mainCameraSpring->TargetArmLength != zoomLevel)
{
mainCameraSpring->TargetArmLength = FMath::FInterpTo(mainCameraSpring->TargetArmLength, zoomLevel, DeltaSeconds, CameraSpeed);
}
if (mainPlayer)
{
if (mainCameraSpring && mainCameraSpring->RelativeLocation != cameraSpringPosition)
{
mainCameraSpring->SetRelativeLocation(FMath::VInterpTo(mainCameraSpring->RelativeLocation, cameraSpringPosition, DeltaSeconds, CameraSpeed));
FTransform playerTransfrom;
mainPlayer->GetTransform().GetRelativeTransform(playerTransfrom);
FRotator lookAtRot = FRotator(mainCameraSpring->RelativeRotation.Pitch, UKismetMathLibrary::FindLookAtRotation(mainCameraSpring->RelativeLocation, playerTransfrom.GetLocation()).Yaw, 0);
mainCameraSpring->SetRelativeRotation(lookAtRot);
}
else if (mainCamera && mainCamera->RelativeLocation != cameraPosition)
{
mainCamera->SetRelativeLocation(FMath::VInterpTo(mainCamera->RelativeLocation, cameraPosition, DeltaSeconds, CameraSpeed));
}
}
}