I'm trying to use ACharacter
for my characters, but the game uses ONLY static cameras (ie. they shouldn't even logically be attached to the mesh of the characters). But it seems Unreal's ACharacter
will always have a camera spawned, there doesn't seem to be a way to disable it.
I've butchered code from this tutorial. It uses SetViewTargetWithBlend
to change between the character's camera and the static one.
When I used that code, it crashed Unreal, and the reason I found was because he hard-coded '0' element of the found array. I added a check to see if array is empty and this stops the crash. Of course this means it is empty and hence the line inside the if block doesn't fire.
But it seems to me I have done exactly what they did in the tutorial. I have ensured that in the character BP I set the variable fixed_camera_classtype
:
.h:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Camera/CameraComponent.h"
#include "Player_PnC.generated.h"
UCLASS()
class VOLCANOTOWN_API APlayer_PnC : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
APlayer_PnC();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TSubclassOf<ACameraActor> fixed_camera_classtype;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
int movement_state = 0;
};
.cpp
#include "Player_PnC.h"
#include "Components/SkeletalMeshComponent.h"
#include "Camera/CameraActor.h"
#include "Runtime/Engine/Classes/Kismet/GameplayStatics.h"
// Sets default values
APlayer_PnC::APlayer_PnC()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void APlayer_PnC::BeginPlay()
{
Super::BeginPlay();
//Cast to Player Character's PlayerController
if (APlayerController* PlayerCharacterController = Cast<APlayerController>(GetController()))
{
//Array to contain found Camera Actors
TArray<AActor*> FoundActors;
//Utility function to populate array with all Camera Actors in the level
UGameplayStatics::GetAllActorsOfClass(GetWorld(), fixed_camera_classtype, FoundActors);
if (FoundActors.Num() != 0)
{
//Sets Player Controller view to the first CameraActor found
PlayerCharacterController->SetViewTargetWithBlend(FoundActors[0], 0.0f, EViewTargetBlendFunction::VTBlend_Linear);
}
}
}