Bootstrap

UE4切换关卡

当设计完成一个场景中的游戏的时候或者角色在一个场景中结束战斗后,需要切换到另外一个场景中,这就需要场景的切换了。
新建一个场景切换的Box,当角色和Box发生碰撞的时候,就会自动加载另外一个场景。
首先定义这个Box类:ALevelTranslateVolume

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "LevelTranslateVolume.generated.h"

UCLASS()
class FIRSTPROJECT_API ALevelTranslateVolume : public AActor {
	GENERATED_BODY()

public:
	// Sets default values for this actor's properties
	ALevelTranslateVolume();

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Translate")
	class UBoxComponent* levelTranslateVolume = nullptr;

	class UBillboardComponent* billboard = nullptr;

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Translate")
	FName newLevelName;

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UFUNCTION()//如果不添加这个宏,函数就不起作用
	virtual void onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
};

// Fill out your copyright notice in the Description page of Project Settings.


#include "LevelTranslateVolume.h"
#include "Components/BoxComponent.h"
#include "Components/BillboardComponent.h"
#include "MainCharacter.h"

// Sets default values
ALevelTranslateVolume::ALevelTranslateVolume() {
	// 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;

	levelTranslateVolume = CreateDefaultSubobject<UBoxComponent>(TEXT("LevelTranslate"));
	RootComponent = levelTranslateVolume;

	billboard = CreateDefaultSubobject<UBillboardComponent>(TEXT("Billboard"));
	billboard->SetupAttachment(GetRootComponent());

	newLevelName = "SunTemple";
}

// Called when the game starts or when spawned
void ALevelTranslateVolume::BeginPlay() {
	Super::BeginPlay();

	levelTranslateVolume->OnComponentBeginOverlap.AddDynamic(this, &ALevelTranslateVolume::onBeginOverlap);

}

// Called every frame
void ALevelTranslateVolume::Tick(float DeltaTime) {
	Super::Tick(DeltaTime);

}

void ALevelTranslateVolume::onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {
	if (OtherActor){
		AMainCharacter* mainCharacter = Cast<AMainCharacter>(OtherActor);
		if (mainCharacter){
			mainCharacter->switchLevel(newLevelName);
		}
	}
}


在角色类中设置切换开关的接口:

//根据名称切换不同的关卡
void switchLevel(FName name);
void AMainCharacter::switchLevel(FName name) {
	UWorld* world = GetWorld();
	if (world){
		FString mapName = world->GetMapName();
		FName currentMapName(*mapName);
		if (currentMapName != name){
			UGameplayStatics::OpenLevel(world, name);
		}
	}
}

创建ALevelTranslateVolume对应的蓝图类,并且把创建好的蓝图类拖动到场景中,并且设置新地图名称
在这里插入图片描述

aaa

;