Implement camera perspective toggle
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -73,10 +73,26 @@ protected:
|
||||
UPROPERTY(EditAnywhere, Category="Input")
|
||||
UInputAction* InteractAction;
|
||||
|
||||
/** Toggle between third-person and first-person camera views. */
|
||||
UPROPERTY(EditAnywhere, Category="Input")
|
||||
UInputAction* ToggleCameraAction;
|
||||
|
||||
/** How far the player can interact with MVP objects. */
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Agrarian|Interaction", meta = (ClampMin = "100"))
|
||||
float InteractionDistance = 450.0f;
|
||||
|
||||
/** Third-person spring arm distance used when returning from first person. */
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Agrarian|Camera", meta = (ClampMin = "0"))
|
||||
float ThirdPersonCameraDistance = 400.0f;
|
||||
|
||||
/** Local first-person camera offset relative to the capsule root. */
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Agrarian|Camera")
|
||||
FVector FirstPersonCameraOffset = FVector(0.0f, 0.0f, 72.0f);
|
||||
|
||||
/** True when this local character is using the optional first-person view. */
|
||||
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Agrarian|Camera", meta = (AllowPrivateAccess = "true"))
|
||||
bool bFirstPersonCamera = false;
|
||||
|
||||
public:
|
||||
|
||||
/** Constructor */
|
||||
@@ -98,6 +114,12 @@ protected:
|
||||
/** Called for interaction input */
|
||||
void Interact();
|
||||
|
||||
/** Called for camera perspective toggle input */
|
||||
void ToggleCameraPerspective();
|
||||
|
||||
/** Applies local camera presentation state. */
|
||||
void SetFirstPersonCamera(bool bEnableFirstPerson);
|
||||
|
||||
public:
|
||||
|
||||
/** Handles move inputs from either controls or UI interfaces */
|
||||
@@ -120,6 +142,10 @@ public:
|
||||
UFUNCTION(BlueprintCallable, Category="Agrarian|Interaction")
|
||||
virtual void TryInteract();
|
||||
|
||||
/** Returns true when this local character is using first-person camera presentation. */
|
||||
UFUNCTION(BlueprintPure, Category="Agrarian|Camera")
|
||||
bool IsFirstPersonCamera() const { return bFirstPersonCamera; }
|
||||
|
||||
/** Server-authoritative interaction entry point. */
|
||||
UFUNCTION(Server, Reliable)
|
||||
void ServerInteract(AActor* TargetActor);
|
||||
@@ -144,4 +170,3 @@ public:
|
||||
/** Returns BuildingPlacementComponent subobject **/
|
||||
FORCEINLINE UAgrarianBuildingPlacementComponent* GetBuildingPlacementComponent() const { return BuildingPlacementComponent; }
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user