在场景中可以随便产生一些Actor
- 新建C++类:SpawnVolume
- 打开VS2019编辑类
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SpwanVolume.generated.h"
UCLASS()
class FIRSTPROJECT_API ASpwanVolume : public AActor {
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ASpwanVolume();
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category ="Spawning")
class UBoxComponent* spawningBox = nullptr;
UFUNCTION(BlueprintPure, Category = "Spawning")
FVector getSpawnPoint();
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Spawning")
TSubclassOf<class AAnimal> pawnToSpawn;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "SpwanVolume.h"
#include "Components/BoxComponent.h"
#include "Kismet/KismetMathLibrary.h"
// Sets default values
ASpwanVolume::ASpwanVolume() {
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
spawningBox = CreateDefaultSubobject<UBoxComponent>(TEXT("SpawningBox"));
RootComponent = spawningBox;
}
FVector ASpwanVolume::getSpawnPoint() {
FVector boxExtent = spawningBox->GetScaledBoxExtent();
FVector boxOrigin = spawningBox->GetComponentLocation();
FVector randPt = UKismetMathLibrary::RandomPointInBoundingBox(boxOrigin, boxExtent);
return randPt;
}
// Called when the game starts or when spawned
void ASpwanVolume::BeginPlay() {
Super::BeginPlay();
}
// Called every frame
void ASpwanVolume::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
}
- 创建蓝图类
- 把创建的蓝图类拖动到场景中,点击【运行】即可
aaa