天天看点

UE4通过蓝图使网格体上升

上一篇文章中创建了一个网格体蓝图,里面有触发盒子,所以可以通过触发盒子,是网格体的位置发生变化。

一、使用蓝图使网格体移动

  1. 在C++类中定义可以被蓝图调用的事件
UFUNCTION(BlueprintImplementableEvent, Category="FloorSwitch")
	void riseDoor();
	UFUNCTION(BlueprintImplementableEvent, Category = "FloorSwitch")
	void lowerDoor();
           
  1. 在触发事件中调用
void AFloorSwitch::onBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {
	UE_LOG(LogTemp, Warning, TEXT("AFloorSwitch::onBeginOverlap"));
	riseDoor();
}

void AFloorSwitch::onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) {
	UE_LOG(LogTemp, Warning, TEXT("AFloorSwitch::onEndOverlap"));
	lowerDoor();
}
           
  1. 在蓝图中使用这些事件
UE4通过蓝图使网格体上升

上面蓝图的意思是:riseDoos事件设置Door的场景位置,在原来位置的基础上Z轴添加450,然后设置新的场景。

然后【运行】看一看到,当角色移动到触发盒子的时候,前面的网格体就会上升450个单位

二、同时使用C++和蓝图使网格体移动

  1. 在蓝图中添加两个时间轴,分别用来控制网格体移动的平滑过程

    一个时间轴用来控制上升的门,一个用来下降的地板

    UE4通过蓝图使网格体上升
    记得一定要勾选上【使用最后一个关键帧】,目的是:时间轴在两个关键帧中间运行,而不会超过关键帧的范围
    UE4通过蓝图使网格体上升
  2. 绘制蓝图,并且可以调用C++中的事件和函数
    UE4通过蓝图使网格体上升
  3. C++定义的事件和函数
    UE4通过蓝图使网格体上升
// 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);

	UFUNCTION(BlueprintImplementableEvent, Category="FloorSwitch")
	void riseDoor();
	UFUNCTION(BlueprintImplementableEvent, Category = "FloorSwitch")
	void lowerDoor();

	//门的初始位置
	UPROPERTY(BlueprintReadWrite, Category = "FloorSwitch")
	FVector initDoorLocation;

	//地板开关的初始位置
	UPROPERTY(BlueprintReadWrite, Category = "FloorSwitch")
	FVector initFloorSwitchLocation;

	UFUNCTION(BlueprintImplementableEvent, Category = "FloorSwitch")
	void riseFloorSwitch();
	UFUNCTION(BlueprintImplementableEvent, Category = "FloorSwitch")
	void lowerFloorSwitch();

	UFUNCTION(BlueprintCallable, Category="FloorSwitch")
	void updateDoorLocation(float z);

	UFUNCTION(BlueprintCallable, Category = "FloorSwitch")
	void updateFloorSwitchLocation(float z);
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(50.0, 50.0, 50.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"));
	riseDoor();
	lowerFloorSwitch();
}

void AFloorSwitch::onEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) {
	UE_LOG(LogTemp, Warning, TEXT("AFloorSwitch::onEndOverlap"));
	lowerDoor();
	riseFloorSwitch();
}

void AFloorSwitch::updateDoorLocation(float z) {
	FVector newLocation = initDoorLocation;
	newLocation.Z += z;
	door->SetWorldLocation(newLocation);
}

void AFloorSwitch::updateFloorSwitchLocation(float z) {
	FVector newLocation = initFloorSwitchLocation;
	newLocation.Z += z;
	floorSwitch->SetWorldLocation(newLocation);
}

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

	initDoorLocation = door->GetComponentLocation();
	initFloorSwitchLocation = floorSwitch->GetComponentLocation();
}

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

}

           

aaa

继续阅读