Get Camera Rotation in Editor Preview

Hey all,

I’m working on creating a custom static mesh component that I want to always face the active camera. I’ve got it working while playing, but I would also like it to work in the editor preview before you hit run. Unfortunately, the path I’m currently using to grab the camera doesn’t seem to work in preview mode. Here’s the code in my TickComponent function that works while playing the game:


	
        if (GEngine && GetWorld()->WorldType != EWorldType::EditorPreview)
	{
		auto pController = GEngine->GetFirstLocalPlayerController(GetWorld());
		if (pController && pController->PlayerCameraManager)
		{
				auto cameraRotation = pController->PlayerCameraManager->GetCameraRotation();
				SetWorldRotation(cameraRotation);
		}
	}	


So if I can get the rotation of the Editor Preview Window, I should be able to get it to work there too. Thanks!

EDIT: Also note that the bAbsoluteRotation = true

We use this to get camera rotation/location in editor:



#if WITH_EDITOR

	FVector CameraLocation = FVector::ZeroVector;
	FRotator CameraRotation = FRotator::ZeroRotator;

	for (auto LevelVC : GEditor->LevelViewportClients)
	{
		if (LevelVC && LevelVC->IsPerspective())
		{
			CameraLocation = LevelVC->GetViewLocation();
			CameraRotation = LevelVC->GetViewRotation();

			break;
		}
	}
#endif


There is also SetViewLocation/Rotation if you want to do the opposite.

That looks like it’ll work, but the build fails on the GEditor reference. What include do I need to access GEditor?

Thanks!

You need to add UnrealEd as private dependency module.

For includes, we have those two:



#include "Editor.h"
#include "LevelEditorViewport.h"


Awesome, works like a charm. Thanks for your help scha!