当角色移动到某一块地板的时候,前面的门就会被自动的升起来。
- 编写一个类
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "FloorSwitch.generated.h"
UCLASS()
class FIRSTPROJECT_API AFloorSwitch : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AFloorSwitch();
//触发盒子
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category="FloorSwitch")
class UBoxComponent* traggerBox = nullptr;
//地板开关
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "FloorSwitch")
class UStaticMeshComponent* floorSwitch = nullptr;
//门
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "FloorSwitch")
class UStaticMeshComponent* door = nullptr;
UFUNCTION()
void onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult&SweepResult);
UFUNCTION()
void onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent*OtherComp, int32 OtherBodyIndex);
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 "FloorSwitch.h"
#include "Components/BoxComponent.h"
// Sets default values
AFloorSwitch::AFloorSwitch() {
// 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;
traggerBox = CreateDefaultSubobject<UBoxComponent>(TEXT("TriggerBox"));
RootComponent = traggerBox;
traggerBox->SetCollisionEnabled(ECollisionEnabled::QueryOnly);//只检测碰撞事件
traggerBox->SetCollisionObjectType(ECC_WorldStatic);//设置对象类型
traggerBox->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);//忽略所有通道
traggerBox->SetCollisionResponseToChannel(ECC_Pawn, ECR_Overlap);//对Pawn类型开放事件重叠事件
traggerBox->SetBoxExtent(FVector(62.0, 62.0, 62.0));
floorSwitch = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("FloorSwitch"));
floorSwitch->SetupAttachment(GetRootComponent());
door = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Door"));
door->SetupAttachment(GetRootComponent());
}
void AFloorSwitch::onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {
UE_LOG(LogTemp, Warning, TEXT("AFloorSwitch::onBeginOverlap"));
}
void AFloorSwitch::onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) {
UE_LOG(LogTemp, Warning, TEXT("AFloorSwitch::onEndOverlap"));
}
// Called when the game starts or when spawned
void AFloorSwitch::BeginPlay() {
Super::BeginPlay();
//绑定事件
traggerBox->OnComponentBeginOverlap.AddDynamic(this, &AFloorSwitch::onBeginOverlap);
traggerBox->OnComponentEndOverlap.AddDynamic(this, &AFloorSwitch::onEndOverlap);
}
// Called every frame
void AFloorSwitch::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
}
- 创建蓝图,并且保存在文件夹中:GamePlayBP
- 把蓝图拖动到场景中,当角色才在方块的时候,就会打出日志
aaa