How to rotate an object according to the camera axis?

This is the code I am using:

FRotator DestinationRotation = Apartment->GetActorRotation() + FRotator(0.0f, ApartmentYaw * -Speed, ApartmentPitch * - Speed);

But when the object is in the opposite direction from the camera , then the axes are reversed .

Thx, in advance.

What do you mean the axes are reversed? Does it spin in the opposite direction than you expect, are the Yaw and Pitch reversed, or what? What are you trying to do overall?

These images should explain.

// Called to bind functionality to input
void AApartmentType::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
InputComponent->BindAxis(“CameraPitch”, this, &AApartmentType::PitchCamera);
InputComponent->BindAxis(“CameraYaw”, this, &AApartmentType::YawCamera);
InputComponent->BindAction(“ZoomIn”, IE_Pressed, this, &AApartmentType::ZoomIn);
InputComponent->BindAction(“ZoomOut”, IE_Pressed, this, &AApartmentType::ZoomOut);
}

    void AApartmentType::PitchCamera(float AxisValue)
    {
    	ApartmentPitch = AxisValue;
    }
    
    void AApartmentType::YawCamera(float AxisValue)
    {
    	ApartmentYaw = AxisValue;
    }

CameraYaw uses MouseX event.
CameraPitch uses MouseY event.

How are you calculating ApartmentYaw and ApartmentPitch?

EDIT2: wording/spelling

EDIT: in case you wanted to know how to rotate an object according to camera axes, regardless of the object’s current rotation (or can’t reconstruct them for whatever reason), I’ll add that answer as well:

FVector Fwd = GEngine->GetFirstLocalPlayerController(GetWorld())->PlayerCameraManager->GetActorForwardVector();
FVector Up = GEngine->GetFirstLocalPlayerController(GetWorld())->PlayerCameraManager->GetActorUpVector();
FRotator DestinationRotation(Apartment->GetActorRotation().Quaternion() * FQuat(Up, ApartmentYaw * -Speed) * FQuat(Fwd, ApartmentPitch * -Speed));

PREVIOUS ANSWER:
To me, it looks like your convoluting your matrix. Since your always using the Apartment matrix as a reference, but then changing the Apartment matrix every tick, you’re changing what ‘left’, ‘right’, ‘up’ and ‘down’ means. Try building the rotator from components instead:

FRotator DestinationRotation = FRotator(0.0f, ApartmentYaw, ApartmentPitch);
...
void AApartmentType::PitchCamera(float AxisValue)
{
         ApartmentPitch = FMath::Clamp(ApartmentPitch + (AxisValue * Speed), -80.0f, 80.0f); // clamp to avoid gimbal lock
}

void AApartmentType::YawCamera(float AxisValue)
{
         ApartmentYaw += (AxisValue * Speed);
}

Btw, the order of Eulers is FRotator(Pitch, Yaw, Roll) - you have Pitch and Roll swapped (by name at least, if not by intention). Also, to me ‘YawCamera’ and ‘PitchCamera’ seem like misnomers but that’s up to you as well.