Hi UE4 community!
So I’m experimenting with one of Unreal Docs tutorials, the Game-Controlled Camera one. I first modified the camera-switching conditional in the Tick() so that it smoothly runs between Camera One and Camera Two, instead of just blending to Camera Two then jerking back to Camera One. That worked fine. Then, to challenge myself, I wanted to try making it depend on a button prompt: pressing x would switch the cameras instead of a timer. It seemed simple enough, but I now realize that since the CameraDirector’s parent class is the actor and not the pawn, there is no player input component, so my ‘Super::SetupPlayerInputComponent(InputComponent)’ is triggering a syntax error (line 88). Is there any way to create the input component? Here is my code:
CameraDirector.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Actor.h"
#include "CameraDirector.generated.h"
UCLASS()
class HOWTO_AUTOCAMERA_API ACameraDirector : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ACameraDirector();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent);
//These makes it possible to drag and drop cameras into the class
UPROPERTY(EditAnywhere)
AActor* CameraOne;
UPROPERTY(EditAnywhere)
AActor* CameraTwo;
float TimeToNextCameraChange;
//Button for switching camera manually
//Input functions
void StartSwitch();
void StopSwitch();
//Input variable
bool bSwitching;
};
CameraDirector.cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "HowTo_AutoCamera.h"
#include "CameraDirector.h"
#include "Kismet/GameplayStatics.h"
// Sets default values
ACameraDirector::ACameraDirector()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
//Set this up to be controlled by the lowest-numbered player
//AutoPosessPlayer = EAutoReceiveInput::Player0;
}
// Called when the game starts or when spawned
void ACameraDirector::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ACameraDirector::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
const float SmoothBlendTime = 0.75f;
if(bSwitching)
{
//Locates the player actor
APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0);
if(OurPlayerController)
{
if((OurPlayerController->GetViewTarget() != CameraOne) && (CameraOne != nullptr))
{
//Cut instantly to CameraONe
OurPlayerController->SetViewTargetWithBlend(CameraOne, SmoothBlendTime);
}
else if((OurPlayerController->GetViewTarget() != CameraTwo) && (CameraTwo != nullptr))
{
OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime);
}
}
}
/* All of this commented out is from the original tutorial
const float TimeBetweenCameraChanges = 2.0f;
const float SmoothBlendTime = 0.75f;
TimeToNextCameraChange -= DeltaTime;
if(TimeToNextCameraChange <= 0.0f)
{
TimeToNextCameraChange += TimeBetweenCameraChanges;
//Locates the player actor
APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0);
if(OurPlayerController)
{
if((OurPlayerController->GetViewTarget() != CameraOne) && (CameraOne != nullptr))
{
//Cut instantly to CameraONe
OurPlayerController->SetViewTargetWithBlend(CameraOne, SmoothBlendTime);
}
else if((OurPlayerController->GetViewTarget() != CameraTwo) && (CameraTwo != nullptr))
{
OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime);
}
}
}*/
}
// Called to bind functionality to input
void ACameraDirector::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
// Respond when the "Switch" key is pressed or released
InputComponent->BindAction("Switch", IE_Pressed, this, &ACameraDirector::StartSwitch);
InputComponent->BindAction("Switch", IE_Released, this, &ACameraDirector::StopSwitch);
}
void ACameraDirector::StartSwitch()
{
bSwitching = true;
}
void ACameraDirector::StopSwitch()
{
bSwitching = false;
}