天天看點

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

繼續閱讀