Changing camera view trouble

Hi again guys.

I am having a bit of trouble figuring out how to set the players view to a different camera. I know that I have came close.
Basically I would like my Camera class to take the players view as soon as it is spawned. A different actor will be spawning it.
Here is the code that I currently have:



// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include “GameFramework/Actor.h”
#include “NewCamera.generated.h”

class CPP_CAMERA_EXAMPLE_API ANewCamera : public AActor

// Sets default values for this actor’s properties

/** Camera boom positioning the camera behind the character */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class USpringArmComponent* NewCameraBoom;

/** Follow camera */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	class UCameraComponent* NewCamera;


CPP file

// Fill out your copyright notice in the Description page of Project Settings.

#include “CPP_Camera_Example.h”
#include “NewCamera.h”

// Sets default values

// Create a follow camera
NewCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
NewCamera->SetupAttachment(NewCameraBoom, USpringArmComponent::SocketName);



If I am not mistaken I believe that I have to use SetViewTarget. I have tried it various different ways with no luck. I know I got close though. Any ideas? And thank you again.

Bump… Any ideas?

I don’t understand your question very well. You want to spawn a new camera and use the new spawned camera to view the pawn or you want to use a camera attached to the pawn?

If you want to use a new camera you should create an ACameraActor and not an UCameraComponent.
I saw that your camera inherits from an Actor, if you want to do something like a security camera I suggest you to use a Pawn instead of an Actor.
But if you want to use the camera attached to the pawn, just create a new project using one of the templates and see how the camera is attached to the character.

I do apologize, I should have been more clear. I want to use the camera for the players view. Essentially I want to make it so that when the actor is spawned in to the scene it immediately takes the players view. And it does not have to be the view of a particular object, it simply needs to take the view of the camera. I did also try ACameraActor however I was not able to make that work either. I know that I have came close though. And I thank you again for any help.

So, as I said before, you need to attach the camera to your pawn/character or actor that you want to spawn, and then when you spawn your actor, use the SetViewTarget(YourNewActor).

If you created your project from scratch I suggest you create a new project using one of the templates, so you can see how it’s done.

Here is a piece of code but I encourage you to take a look at the templates.

    SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
SpringArmComponent->bAbsoluteRotation = true; // Rotation of the character should not affect rotation of boom
SpringArmComponent->bDoCollisionTest = false;
SpringArmComponent->TargetArmLength = 800.f;
SpringArmComponent->SocketOffset = FVector(0.f, 0.f, -30.f);
SpringArmComponent->RelativeRotation = FRotator(0.f, -90.f, 0.f);
SpringArmComponent->bEnableCameraLag = true;
SpringArmComponent->CameraLagSpeed = 3.f;

    SideViewCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("SideViewCamera"));
SideViewCamera->AttachTo(SpringArmComponent, USpringArmComponent::SocketName);
SideViewCamera->bUsePawnControlRotation = false; // We don't want the controller rotating the camera

I don’t see that you’re creating the camera boom. For example:

// Create the boom
CameraBoomComp = ObjectInitializer.CreateDefaultSubobject<USpringArmComponent>(this, TEXT("CameraBoom"));
CameraBoomComp->SocketOffset = FVector(0, 35, 0);
CameraBoomComp->TargetOffset = FVector(0, 0, 55);
CameraBoomComp->bUsePawnControlRotation = true;
CameraBoomComp->AttachParent = GetRootComponent();

// Create the camera, attach it to the boom
CameraComp = ObjectInitializer.CreateDefaultSubobject<UCameraComponent>(this, TEXT("Camera"));
CameraComp->AttachParent = CameraBoomComp;