創(chuàng)建定時(shí)器來定時(shí)生成電池到場景中
最終 SpawnVolume.h
// Fill out your copyright notice in the Description page of Project Settings.
pragma once
include "GameFramework/Actor.h"
include "SpawnVolume.generated.h"
UCLASS()
class BATTERYCOLLECTOR_API ASpawnVolume : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ASpawnVolume();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
FORCEINLINE UBoxComponent* GetWhereToSpawn()const { return WhereToSpawn; }
UFUNCTION(BlueprintPure, Category = "Spawning")
FVector GetRandomPointInVolume();
protected:
UPROPERTY(EditAnyWhere, Category = "Spawning")
TSubclassOf<class APickUp> WhatToSpawn;
FTimerHandle SpawnTimer; //定時(shí)器
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
float SpawnDelayRangeLow;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
float SpawnDelayRangeHigh;
private:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Spawning", meta = (AllowPrivateAccess = "true"))
class UBoxComponent* WhereToSpawn;
void SpawnPickup();
float SpawnDelay;
};
最終SpawnVolume.cpp
// Fill out your copyright notice in the Description page of Project Settings.
include "BatteryCollector.h"
include "SpawnVolume.h"
include "Kismet/KismetMathLibrary.h"
include "PickUp.h"
// Sets default values
ASpawnVolume::ASpawnVolume()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
WhereToSpawn = CreateDefaultSubobject<UBoxComponent>(TEXT("WhereToSpawn"));
RootComponent = WhereToSpawn;
SpawnDelayRangeLow = 1.0f;
SpawnDelayRangeHigh = 4.5f;
}
// Called when the game starts or when spawned
void ASpawnVolume::BeginPlay()
{
Super::BeginPlay();
SpawnDelay = FMath::FRandRange(SpawnDelayRangeLow, SpawnDelayRangeHigh);
GetWorldTimerManager().SetTimer(SpawnTimer, this, &ASpawnVolume::SpawnPickup, SpawnDelay, false); // 添加定時(shí)器 回調(diào)函數(shù)為SpawnPickup 時(shí)間間隔為SpawnDelay 是否循環(huán)false
}
// Called every frame
void ASpawnVolume::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
FVector ASpawnVolume::GetRandomPointInVolume()
{
FVector SpawnOrigin = WhereToSpawn->Bounds.Origin;
FVector SpawnExtent = WhereToSpawn->Bounds.BoxExtent;
return UKismetMathLibrary::RandomPointInBoundingBox(SpawnOrigin, SpawnExtent);
}
void ASpawnVolume::SpawnPickup()
{
if (WhatToSpawn != NULL)
{
UWorld* const World = GetWorld();
if (World)
{
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;
FVector SpawnLocation = GetRandomPointInVolume();
FRotator SpawnRotation;
SpawnRotation.Yaw = FMath::FRand()*360.0f;
SpawnRotation.Pitch = FMath::FRand()*360.0f;
SpawnRotation.Roll = FMath::FRand()*360.0f;
APickUp* const SpawnedPickup = World->SpawnActor<APickUp>(WhatToSpawn, SpawnLocation, SpawnRotation, SpawnParams);
GetWorldTimerManager().SetTimer(SpawnTimer, this, &ASpawnVolume::SpawnPickup, SpawnDelay, false);
}
}
}
在UE4 Editor中 間SpawnVolume拖拽到場景中 調(diào)整大小位置及參數(shù) 運(yùn)行 電池會(huì)動(dòng)態(tài)生成