feat: 增加延迟生成
This commit is contained in:
@@ -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<USpringArmComponent>(TEXT("SpringArmComponent"));
|
||||
SpringArmComponent->SetupAttachment(RootComponent);
|
||||
SpringArmComponent->TargetArmLength = 300.f;
|
||||
SpringArmComponent->TargetArmLength = 1200.f;
|
||||
SpringArmComponent->bUsePawnControlRotation = true;
|
||||
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
|
||||
CameraComponent->SetupAttachment(SpringArmComponent, USpringArmComponent::SocketName);
|
||||
@@ -37,6 +38,11 @@ void ACameraPawn::BeginPlay()
|
||||
{
|
||||
Super::BeginPlay();
|
||||
|
||||
// 设置摄像头可见性
|
||||
if (CameraComponent)
|
||||
{
|
||||
CameraComponent->SetVisibility(true);
|
||||
}
|
||||
}
|
||||
|
||||
// Called every frame
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<ACameraPawn>(CameraActorClass, SpawnTransform);
|
||||
temp->FollowTarget = this;
|
||||
temp->FinishSpawning(SpawnTransform);
|
||||
}
|
||||
else
|
||||
{
|
||||
FTransform SpawnTransform(GetActorRotation(), GetActorLocation());
|
||||
temp = w->SpawnActorDeferred<ACameraPawn>(ACameraPawn::StaticClass(), SpawnTransform);
|
||||
temp->FollowTarget = this;
|
||||
temp->FinishSpawning(SpawnTransform);
|
||||
}
|
||||
}
|
||||
if (temp)
|
||||
{
|
||||
CameraActor = temp;
|
||||
}
|
||||
}
|
||||
|
||||
// Called every frame
|
||||
|
||||
@@ -22,7 +22,7 @@ public:
|
||||
class UInputAction* CrouchAction;
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Camera")
|
||||
class ACameraPawn* CameraActor;
|
||||
class TSubclassOf<class ACameraPawn> 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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user