UE4製作自動門----藍圖 or C++

1.藍圖製作

1)創建藍圖項目

在這裏插入圖片描述

在這裏插入圖片描述
在這裏插入圖片描述
項目打開後會打開默認場景,可以使用默認的,也可以自己新建場景。

2)找到初學者資源包打開Content/StartConent/Props文件夾找到我們需要的門SM_Door

在這裏插入圖片描述

3) 添加碰撞體

作用是阻擋角色直接闖過門
在這裏插入圖片描述
最新版本的引擎自帶的有碰撞,所以如果使用的是最新版此步跳過即可

4) 添加盒體觸發器(Sphere Trigger)

在這裏插入圖片描述
在這裏插入圖片描述
調整適當大小

5) 設置移動性

選擇門,把細節面板(Details)----變換(Transform)----移動性(Mobility)----改爲,可移動(Moveable)
在這裏插入圖片描述

6)打開關卡藍圖

選擇觸發盒子,選擇 藍圖(Blueprints)----打開關卡藍圖(Open Level Blueprint)
在這裏插入圖片描述

7)編寫觸發事件

打開關卡藍圖,寫觸發事件。
添加兩個事件:
右鍵----觸發盒子添加事件(Add Event for Trigger Box)----碰撞(Collision)----添加角色開始觸發事件(Add On Actor Begin Overlap)、添加角色結束觸發事件(Add On Actor End Overlap)
在這裏插入圖片描述

8) 給門添加旋轉

到場景選中門,然後到關卡藍圖中給門添加旋轉引用(Create a Reference to SM_Door)
在這裏插入圖片描述
在這裏插入圖片描述

9) 引出旋轉節點(SetActorRotation)

通過這個旋轉引用,引出旋轉節點(SetActorRotation)。門的旋轉是Z軸旋轉,所以需要把旋轉節點的Z軸數值修改爲需要的數值,如:90度。

10) 添加時間軸(Timeline)

時間軸是讓開門的過程更加自然。
雙擊打開時間軸,新建一個浮點型的軌跡(float)----右鍵添加兩個關鍵幀(Add Key to CurveFloat)----分別設置關鍵幀的 時間、值,如:(0,0),(2,90)。
適當以縮放適合水平和豎直方向尺寸,來適應我們的視覺窗口。
點擊第一個關鍵幀,添加一個自動曲線(Auto),作用是讓開門過程由慢而快。
把時間軸的長度,設置爲最後一個關鍵幀的時間長度。
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

11) 最後回到關卡藍圖把對應節點連接好即可

在這裏插入圖片描述

2.C++製作

1)首先創建一個C++項目,創建步驟類似藍圖,只是在選擇的時候選擇C++即可
2)代碼

TE.H

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/TimelineComponent.h"
#include "AutoDoor.generated.h"

UCLASS()
class PROC_API AAutoDoor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AAutoDoor();

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		class UBoxComponent * HitBoxComp; 

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		class UStaticMeshComponent * DoorMeshComp;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		class UCurveFloat * TimelineCurve; 

	FTimeline MyTimeline; 

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		FVector CurrentDoorLocation;  

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
		FVector EndDoorLocation;  

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

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

	UFUNCTION()
		void UpdateDoorLocation(float Volume);  

	UFUNCTION()
		void HitBegin( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);
};

TE.cpp

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


#include "AutoDoor.h"
#include "Components/BoxComponent.h"
#include "Components/StaticMeshComponent.h"



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

	//初始化
	HitBoxComp = CreateDefaultSubobject<UBoxComponent>(TEXT("HitBox"));
	DoorMeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("DoorMesh"));
	DoorMeshComp->SetupAttachment(HitBoxComp);

	CurrentDoorLocation = DoorMeshComp->GetRelativeTransform().GetLocation();  
	HitBoxComp->OnComponentBeginOverlap.AddDynamic(this, &AAutoDoor::HitBegin);  //對HitBegin函數的綁定
}

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

	FOnTimelineFloatStatic TimelineCallBack;   //讓UpdateDoorLocation函數對timeline進行綁定
	TimelineCallBack.BindUFunction(this, TEXT("UpdateDoorLocation"));  
	MyTimeline.AddInterpFloat(TimelineCurve, TimelineCallBack);  
	
}

// Called every frame
void AAutoDoor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	MyTimeline.TickTimeline(DeltaTime);  //對timeline類進行綁定

}

void AAutoDoor::UpdateDoorLocation(float Volume)
{
	FVector NewLocation(0,0,0);
	NewLocation.X = FMath::Lerp(CurrentDoorLocation.X, EndDoorLocation.X, Volume);  
	DoorMeshComp->AddRelativeLocation(NewLocation);
}

void AAutoDoor::HitBegin(UPrimitiveComponent * OverlappedComponent, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	//檢查碰撞到的是不是指定的Actor
	if (OtherActor)
	{
		MyTimeline.PlayFromStart();
		//MyTimeline.ReverseFromEnd();  
}

3)繼承C++類

新建一個藍圖類,繼承自定義的C++類
在這裏插入圖片描述

4)新建曲線圖

右鍵,新建一個曲線圖,並雙擊打開,繼承浮點型曲線(CurveFloat)
在這裏插入圖片描述
在這裏插入圖片描述
打開曲線圖後,添加兩個關鍵幀,方法和藍圖時間軸添加關鍵幀一樣。
注意:曲線平滑的選擇,需要同時選中兩個關鍵幀,右鍵選擇Auto方可生效。
在這裏插入圖片描述

5)設置曲線

藍圖中,選中藍圖,設置指定的曲線(即自定義的曲線)
在這裏插入圖片描述

6)指定模型

選中靜態模型門,給門指定一個模型,這裏就依照自己喜好自由發揮
在這裏插入圖片描述

7)修改碰撞盒子

修改碰撞盒子的大小,一定要比門大。碰撞盒子的Collsion Presets指定爲OverlapAll

8)最後把藍圖拖到場景測試運行即可。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章