SetViewTarget and switching to a different camera

Hi,

I would like to have a toggle for two cameras in runtime

  • A 3rd person camera (spring arm and the camera component)
  • A debug camera (in front of the character) that will follow an arbitrary AI character (obviously different orientation than the 3rd person camera)

I setup the two cameras (3rd person and Debug) in the PlayerCharacter BP, but when switching to the debug camera, it gets positioned at the center of mass of the AI Character that’s been set as the view target.

After testing I found, when I set up a “third” camera in the AI Character, like the debug camera in the PlayerCharacter BP, it works!

I expect that only the Player Character should set up the two cameras and use the SetActive to turn on/off the ones you need, preserving the arm spring and camera relative locations as defined in the BP.

Is this assumption correct?
Why would I need to set up a “third camera” in the AI Character that’s been used as the new view target for this to work?

if (bThirdPersonCamera)
		{
			APlayerCharacter* PlayerCharacter = Cast<APlayerCharacter>(GetCharacter());
			PlayerCharacter->GetFollowCamera()->SetActive(true);
                        PlayerCharacter->GetFrontFollowCamera()->SetActive(false);
			SetViewTarget((AActor*)PlayerCharacter, Params);
		}

else
		{
			AActor* NewTarget = nullptr;
			APlayerCharacter* PlayerCharacter = Cast<APlayerCharacter>(GetCharacter());
			PlayerCharacter->GetFollowCamera()->SetActive(false);
			PlayerCharacter->GetFrontFollowCamera()->SetActive(true);

			for (size_t i = 0; i < FoundActors.Num(); i++)
			{
				AAICharacter* MyChar = Cast<AAICharacter>(FoundActors[i]);

				if (MyChar->Type == ETypes::SpecialType)
				{
			               SetViewTarget((AActor*)MyChar, Params);
					break;
				}
			}

		}

Thanks

There no point researching it as you can override this entire behavior

The way camera system works then actor is a view target camera manager asks that actor for camera positioning by calling CalcCamera function on every frame

The default code looks like this:

void AActor::CalcCamera(float DeltaTime, FMinimalViewInfo& OutResult)
{
	if (bFindCameraComponentWhenViewTarget)
	{
		// Look for the first active camera component and use that for the view
		TInlineComponentArray<UCameraComponent*> Cameras;
		GetComponents<UCameraComponent>(/*out*/ Cameras);

		for (UCameraComponent* CameraComponent : Cameras)
		{
			if (CameraComponent->bIsActive)
			{
				CameraComponent->GetCameraView(DeltaTime, OutResult);
				return;
			}
		}
	}

	GetActorEyesViewPoint(OutResult.Location, OutResult.Rotation);
}

So it simply looks for first found active camera ;]

All you need to do is override that function on your actor and set camera position in OutResult. And yes CameraComponent is technically not needed, it just a dummy component containing camera configuration which getting passed in this function to camera manager.

Thanks.
But yeah, looking at that code, since there can only be as many view points as the target view actor has, it seems mandatory to configure the camera component on the AI character.