그냥 게임개발자
ProjectA - ChangeCamera 에러 수정(2023/12/26) 본문
현재 카메라 시점을 바꾸는 코드는 이렇다.
void APACharacterPlayer::SetChangeCamera()
{
ensure(FpsCamera);
ensure(TpsCamera);
if (IsValid(TpsCamera) && IsValid(FpsCamera))
{
switch (CameraType)
{
case ECAMERA::TPS:
TpsCamera->Activate();
FpsCamera->Deactivate();
MyCamera = TpsCamera;
break;
case ECAMERA::FPS:
TpsCamera->Deactivate();
FpsCamera->Activate();
MyCamera = FpsCamera;
break;
}
}
else
{
UE_LOG(LogTemp, Error, TEXT("ERROR"));
}
}
여기서 TpsCamera가 nullptr로 잡히는 버그가 발생했다.
그렇기에 여러가지 시도를 해보았지만 너무 허탈했다..
// ThirdCamera
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, Meta = (AllowPrivateAccess = "true"))
TObjectPtr<class UCameraComponent> TpsFollowCamera;
// FirstCamera
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, Meta = (AllowPrivateAccess = "true"))
TObjectPtr<class UCameraComponent> FpsFollowCamera;
헤더파일에서 변수 이름을 바꿔줘야한다...
TpsCamera라는 변수이름이 언리얼엔진 어디에서 사용하고 있나보다....
진짜... 이것을 몇시간 째 삽질을 했는지.....일단 해결했으니 다행이다...
그래서 다시 좀 수정해보았다.
PACharacterPlayer.h
UENUM()
enum class ECAMERA : uint8
{
TPS,
FPS,
COUNT,
};
UCLASS()
class PROJECTA_API APACharacterPlayer : public APACharacterBase
{
...
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Camera, Meta = (AllowPrivateAccess = "true"))
ECAMERA CameraType;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, Meta = (AllowPrivateAccess = "true"))
TMap<ECAMERA, TObjectPtr<class UCameraComponent>> CameraMap;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, Meta = (AllowPrivateAccess = "true"))
TObjectPtr<class UCameraComponent> MyCamera;
...
}
카메라 변수를 수정 TMap으로 담는다.
CameraType에 따라 바꾸도록하자.
PACharacterPlayer.cpp
APACharacterPlayer::APACharacterPlayer()
{
// Camera
UCameraComponent* TpsFollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("TpsFollowCamera"));
TpsFollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
TpsFollowCamera->bUsePawnControlRotation = false;
UCameraComponent* FpsFollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FpsFollowCamera"));
FpsFollowCamera->SetupAttachment(GetMesh(), TEXT("FX_Head"));
FpsFollowCamera->bUsePawnControlRotation = true;
FpsFollowCamera->SetRelativeRotation(FRotator(180.f, 0.f, 0.f));
FpsFollowCamera->SetAutoActivate(false);
CameraMap.Emplace(ECAMERA::TPS, TpsFollowCamera);
CameraMap.Emplace(ECAMERA::FPS, FpsFollowCamera);
CameraType = ECAMERA::TPS;
MyCamera = CameraMap[CameraType];
}
void APACharacterPlayer::BeginPlay()
{
Super::BeginPlay();
...
SetChangeCamera();
}
void APACharacterPlayer::SetChangeCamera()
{
CameraType = CameraType == ECAMERA::TPS ? ECAMERA::FPS : ECAMERA::TPS;
MyCamera->Deactivate();
MyCamera = CameraMap[CameraType];
MyCamera->Activate();
}
1인칭 카메라는 흔들리지 않도록 bUsePawnControlRotation = true로 바꿔주자

잘된다.
'ProjectA' 카테고리의 다른 글
ProjectA - 애니메이션 Retarget, AnimBlueprint (0) | 2023.12.27 |
---|---|
ProjectA - 임시적 1인칭 Yaw회전 적용(2023/12/27) (0) | 2023.12.27 |
ProjectA - EnhancedInput(Move, Look)(2023/12/26) (0) | 2023.12.26 |
ProjectA - GameMode, Character 클래스 생성 2023/12/26 (0) | 2023.12.26 |
프로젝트명 : ProjectA(2023/12/26) (0) | 2023.12.26 |