How to Switch Cameras in C++ (Aim Down Sights)

Currently I am trying to figure out which functions to use in order to set the camera attached to the gun as the viewport character’s camera. I successfully deactivated the follow camera now all that needs to be done is activate the child class of the gun itself which is the ADSCamera. Any help would be much appreciated, thank you!

 void AWeaponBase::IsAiming()
    {
    	ATheProjectNameCharacter* Player = Cast<ATheProjectNameCharacter> 
       (UGameplayStatics::GetPlayerCharacter(this, 0));
    	bisAiming = true;
    	bool IsNewActive = true;
    	if (bisAiming)
    	{

    		Player->FollowCamera->Deactivate();
    		ADSCamera->Activate();
    		ADSCamera->SetActive(IsNewActive);
    	}
    	
    }

    void ATheProjectNameCharacter::AimDownSights()
    {
    //Calling the function.
    	if (CurrentWeapon)
    	{
    		CurrentWeapon->IsAiming();
    	}
    }

From my memory : Set View Target with Blend | Unreal Engine Documentation

//Here is the .h definition.
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “Camera”)
class UCameraComponent* ADSCamera;

AWeaponBase::AWeaponBase()
{
     	PrimaryActorTick.bCanEverTick = true;
	WeaponBaseMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("WeaponBaseMesh"));
	RootComponent = WeaponBaseMesh;
	ADSCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("ADSM4A1Camera"));
	ADSCamera->SetupAttachment(RootComponent);
}

It is a camera component class.

//I have tried that…

 void AWeapon::IsAiming()
    {
    	AProjectNameCharacter* Player = Cast<AProjectNameCharacter>(UGameplayStatics::GetPlayerCharacter(this, 0));
    	bisAiming = true;
    	FViewTargetTransitionParams Params;
    	APlayerController* Controller = Cast<APlayerController>(Player->GetController());
    	//FViewTargetTransitionParams Params;
    	bool IsNewActive = true;
    	if (bisAiming)
    	{
    		Player->FollowCamera->Deactivate();
    		ADSCamera->Activate();
    		Player->FollowCamera = ADSCamera;
//Error here on ADSCamera. Says the target has to be AActor.
    		Controller->SetViewTargetWithBlend(ADSCamera, Params);
    	}
    }

Can you show definition of ADSCamera ?

Yeah f*** me, i deal with this issue two month ago, can’t rememember how i solved it and i don’t have the source code under the hand :s

It’s weird cause it is easy to just place the camera inside of the character then activate and deactivate both cameras. But when calling the camera from a separate actor it is tougher for whatever reason.

//I figure out how to aim but now I need to set the camera transform back to the old location. Any tips?
void AWeaponBase::IsAiming()
{
ACharacter* Player = Cast(UGameplayStatics::GetPlayerCharacter(this, 0));
bisAiming = true;
FViewTargetTransitionParams Params;
const FTransform SocketTransform = WeaponBaseMesh->GetSocketTransform(FName(“ADS_Socket”));
//const FRotator SocketRotation = WeaponBaseMesh->GetSocketRotation(FName(“ADS_Socket”));

	if (bisAiming)
	{
		Player->FollowCamera->SetWorldTransform(SocketTransform);
		bisAiming = false;
	}
}
void AWeaponBase::IsNotAiming()
{

	ACharacter* Player = Cast<ACharacter>(UGameplayStatics::GetPlayerCharacter(this, 0));
	const FTransform SocketTransform = Player->GetActorTransform();
	if (!bisAiming)
	{
		Player->FollowCamera->SetWorldTransform(SocketTransform);
		bisAiming = true;
	}
}

I set the a socket on the firearm then I set the player character’s camera to the world transform socket of the firearm. I did not create another camera.

Simply save the old transform position, or use relative location / rotation.

//I created a socket for the follow camera where the head of the character is, then I transferred the camera back and forth between both sockets. The one for the gun that I had created and the one for the character’s follow camera.
void AWeaponBase::IsAiming()
{
AProjectNameCharacter* Player = Cast(UGameplayStatics::GetPlayerCharacter(this, 0));
FViewTargetTransitionParams Params;
const FTransform SocketTransform = WeaponBaseMesh->GetSocketTransform(FName(“M4A1ADS_Socket”));
if (bisAiming)
{
Player->FollowCamera->SetWorldTransform(SocketTransform);
Player->FollowCamera->SetFieldOfView(60.0f);
bisAiming = false;
}
}
void AWeaponBase::IsNotAiming()
{
AProjectNameCharacter* Player = Cast(UGameplayStatics::GetPlayerCharacter(this, 0));
const FTransform SocketTransform2 = Player->GetMesh()->GetSocketTransform(FName(“HeadSocket”));
if (!bisAiming)
{
Player->FollowCamera->SetWorldTransform(SocketTransform2);
Player->FollowCamera->SetFieldOfView(80.0f);
bisAiming = true;
}
}