Implement camera perspective toggle

This commit is contained in:
2026-05-15 10:44:02 -07:00
parent c5e795ef26
commit cd8de0f906
8 changed files with 206 additions and 4 deletions
+46 -1
View File
@@ -43,7 +43,7 @@ AAgrarianGameCharacter::AAgrarianGameCharacter()
// Create a camera boom (pulls in towards the player if there is a collision)
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->TargetArmLength = 400.0f;
CameraBoom->TargetArmLength = ThirdPersonCameraDistance;
CameraBoom->bUsePawnControlRotation = true;
// Create a follow camera
@@ -80,6 +80,11 @@ void AAgrarianGameCharacter::SetupPlayerInputComponent(UInputComponent* PlayerIn
{
EnhancedInputComponent->BindAction(InteractAction, ETriggerEvent::Started, this, &AAgrarianGameCharacter::Interact);
}
if (ToggleCameraAction)
{
EnhancedInputComponent->BindAction(ToggleCameraAction, ETriggerEvent::Started, this, &AAgrarianGameCharacter::ToggleCameraPerspective);
}
}
else
{
@@ -110,6 +115,46 @@ void AAgrarianGameCharacter::Interact()
TryInteract();
}
void AAgrarianGameCharacter::ToggleCameraPerspective()
{
SetFirstPersonCamera(!bFirstPersonCamera);
}
void AAgrarianGameCharacter::SetFirstPersonCamera(bool bEnableFirstPerson)
{
bFirstPersonCamera = bEnableFirstPerson;
if (!CameraBoom || !FollowCamera)
{
return;
}
if (bFirstPersonCamera)
{
CameraBoom->TargetArmLength = 0.0f;
CameraBoom->SocketOffset = FirstPersonCameraOffset;
CameraBoom->bDoCollisionTest = false;
FollowCamera->bUsePawnControlRotation = false;
if (GetMesh())
{
GetMesh()->SetOwnerNoSee(true);
}
}
else
{
CameraBoom->TargetArmLength = ThirdPersonCameraDistance;
CameraBoom->SocketOffset = FVector::ZeroVector;
CameraBoom->bDoCollisionTest = true;
FollowCamera->bUsePawnControlRotation = false;
if (GetMesh())
{
GetMesh()->SetOwnerNoSee(false);
}
}
}
void AAgrarianGameCharacter::DoMove(float Right, float Forward)
{
if (GetController() != nullptr)