feat: 增加延迟生成
This commit is contained in:
BIN
Content/Blueprints/BP_CameraPawn.uasset
LFS
Normal file
BIN
Content/Blueprints/BP_CameraPawn.uasset
LFS
Normal file
Binary file not shown.
Binary file not shown.
@@ -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.
|
// 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;
|
PrimaryActorTick.bCanEverTick = true;
|
||||||
|
|
||||||
|
// 初始化 SpringArmComponent 和 CameraComponent
|
||||||
SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
|
SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
|
||||||
SpringArmComponent->SetupAttachment(RootComponent);
|
SpringArmComponent->SetupAttachment(RootComponent);
|
||||||
SpringArmComponent->TargetArmLength = 300.f;
|
SpringArmComponent->TargetArmLength = 1200.f;
|
||||||
SpringArmComponent->bUsePawnControlRotation = true;
|
SpringArmComponent->bUsePawnControlRotation = true;
|
||||||
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
|
CameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
|
||||||
CameraComponent->SetupAttachment(SpringArmComponent, USpringArmComponent::SocketName);
|
CameraComponent->SetupAttachment(SpringArmComponent, USpringArmComponent::SocketName);
|
||||||
@@ -37,6 +38,11 @@ void ACameraPawn::BeginPlay()
|
|||||||
{
|
{
|
||||||
Super::BeginPlay();
|
Super::BeginPlay();
|
||||||
|
|
||||||
|
// 设置摄像头可见性
|
||||||
|
if (CameraComponent)
|
||||||
|
{
|
||||||
|
CameraComponent->SetVisibility(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Called every frame
|
// Called every frame
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ public:
|
|||||||
class USpringArmComponent* SpringArmComponent;
|
class USpringArmComponent* SpringArmComponent;
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Camera")
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Camera")
|
||||||
class UCameraComponent* CameraComponent;
|
class UCameraComponent* CameraComponent;
|
||||||
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Camera")
|
||||||
|
APawn* FollowTarget;
|
||||||
|
|
||||||
UFUNCTION(BlueprintCallable, Category = "Camera")
|
UFUNCTION(BlueprintCallable, Category = "Camera")
|
||||||
void CameraZoom(const FInputActionValue& Value);
|
void CameraZoom(const FInputActionValue& Value);
|
||||||
|
|||||||
@@ -59,6 +59,29 @@ void AMyCharacter::BeginPlay()
|
|||||||
{
|
{
|
||||||
Super::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
|
// Called every frame
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ public:
|
|||||||
class UInputAction* CrouchAction;
|
class UInputAction* CrouchAction;
|
||||||
|
|
||||||
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Camera")
|
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Camera")
|
||||||
class ACameraPawn* CameraActor;
|
class TSubclassOf<class ACameraPawn> CameraActorClass;
|
||||||
|
|
||||||
UFUNCTION(BlueprintCallable, Category = "Move")
|
UFUNCTION(BlueprintCallable, Category = "Move")
|
||||||
void Move(const FInputActionValue& Value);
|
void Move(const FInputActionValue& Value);
|
||||||
@@ -55,4 +55,7 @@ protected:
|
|||||||
private:
|
private:
|
||||||
bool bIsRunning = false;
|
bool bIsRunning = false;
|
||||||
bool bIsCrouching = false;
|
bool bIsCrouching = false;
|
||||||
|
|
||||||
|
UPROPERTY(VisibleInstanceOnly, Category = "Camera")
|
||||||
|
class ACameraPawn* CameraActor;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user