UE随机产生角色

在场景中可以随便产生一些Actor

  1. 新建C++类:SpawnVolume
    在这里插入图片描述
  2. 打开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);

}
  1. 创建蓝图类
    在这里插入图片描述
  2. 把创建的蓝图类拖动到场景中,点击【运行】即可
    aaa
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wb175208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值