diff --git a/Content/Blueprints/BP_CameraPawn.uasset b/Content/Blueprints/BP_CameraPawn.uasset new file mode 100644 index 0000000..4a01661 --- /dev/null +++ b/Content/Blueprints/BP_CameraPawn.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8007fb767b9832c2c06cb7d1048bd18090056da875e10ca63285a8f99da60289 +size 22559 diff --git a/Content/Blueprints/BP_TestChar.uasset b/Content/Blueprints/BP_TestChar.uasset index b9bd80c..896d1f9 100644 --- a/Content/Blueprints/BP_TestChar.uasset +++ b/Content/Blueprints/BP_TestChar.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:54b1206bf269b93f8120e4018329827390628affde74d5ddd8d0cf3e1b7e141d -size 37281 +oid sha256:cf0ae167d96eafdb6585a27e502eadeac22982f55d61237edead927034100a23 +size 35290 diff --git a/Source/lonese/CameraPawn.cpp b/Source/lonese/CameraPawn.cpp index 1326103..4bf0916 100644 --- a/Source/lonese/CameraPawn.cpp +++ b/Source/lonese/CameraPawn.cpp @@ -24,9 +24,10 @@ ACameraPawn::ACameraPawn() // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true; + // 初始化 SpringArmComponent 和 CameraComponent SpringArmComponent = CreateDefaultSubobject(TEXT("SpringArmComponent")); SpringArmComponent->SetupAttachment(RootComponent); - SpringArmComponent->TargetArmLength = 300.f; + SpringArmComponent->TargetArmLength = 1200.f; SpringArmComponent->bUsePawnControlRotation = true; CameraComponent = CreateDefaultSubobject(TEXT("CameraComponent")); CameraComponent->SetupAttachment(SpringArmComponent, USpringArmComponent::SocketName); @@ -37,6 +38,11 @@ void ACameraPawn::BeginPlay() { Super::BeginPlay(); + // 设置摄像头可见性 + if (CameraComponent) + { + CameraComponent->SetVisibility(true); + } } // Called every frame diff --git a/Source/lonese/CameraPawn.h b/Source/lonese/CameraPawn.h index 330d854..9007742 100644 --- a/Source/lonese/CameraPawn.h +++ b/Source/lonese/CameraPawn.h @@ -16,6 +16,8 @@ public: class USpringArmComponent* SpringArmComponent; UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Camera") class UCameraComponent* CameraComponent; + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Camera") + APawn* FollowTarget; UFUNCTION(BlueprintCallable, Category = "Camera") void CameraZoom(const FInputActionValue& Value); diff --git a/Source/lonese/MyCharacter.cpp b/Source/lonese/MyCharacter.cpp index 422cf9a..24941ec 100644 --- a/Source/lonese/MyCharacter.cpp +++ b/Source/lonese/MyCharacter.cpp @@ -59,6 +59,29 @@ void AMyCharacter::BeginPlay() { Super::BeginPlay(); + auto w = GetWorld(); + ACameraPawn* temp = nullptr; + if (w) + { + if (CameraActorClass) + { + FTransform SpawnTransform(GetActorRotation(), GetActorLocation()); + temp = w->SpawnActorDeferred(CameraActorClass, SpawnTransform); + temp->FollowTarget = this; + temp->FinishSpawning(SpawnTransform); + } + else + { + FTransform SpawnTransform(GetActorRotation(), GetActorLocation()); + temp = w->SpawnActorDeferred(ACameraPawn::StaticClass(), SpawnTransform); + temp->FollowTarget = this; + temp->FinishSpawning(SpawnTransform); + } + } + if (temp) + { + CameraActor = temp; + } } // Called every frame diff --git a/Source/lonese/MyCharacter.h b/Source/lonese/MyCharacter.h index d9799a5..15cbbfd 100644 --- a/Source/lonese/MyCharacter.h +++ b/Source/lonese/MyCharacter.h @@ -22,7 +22,7 @@ public: class UInputAction* CrouchAction; UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Camera") - class ACameraPawn* CameraActor; + class TSubclassOf CameraActorClass; UFUNCTION(BlueprintCallable, Category = "Move") void Move(const FInputActionValue& Value); @@ -55,4 +55,7 @@ protected: private: bool bIsRunning = false; bool bIsCrouching = false; + + UPROPERTY(VisibleInstanceOnly, Category = "Camera") + class ACameraPawn* CameraActor; };