06 - Creating the Spawning Volume

生成用来创建电池的volume

添加新的C++ actor类 SpawnVolume 为Actor类添加一个UBoxComponent类 用来获得一个随机的矩形区域

最终。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();

private:

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Spawning", meta = (AllowPrivateAccess = "true"))

class UBoxComponent* WhereToSpawn;

};

最终。cpp

#include "BatteryCollector.h"

#include "SpawnVolume.h"

#include "Kismet/KismetMathLibrary.h" //为了使用下面的UKismetMathLibrary::RandomPointInBoundingBox

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(TEXT("WhereToSpawn"));

RootComponent = WhereToSpawn;

}

// Called when the game starts or when spawned

void ASpawnVolume::BeginPlay()

{

Super::BeginPlay();

}

// 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);

}

你可能感兴趣的:(06 - Creating the Spawning Volume)