UE5 C++ 使用TimeLine时间轴实现开关门

一.添加门头文件 和 声明

cpp 复制代码
#include "Components/TimelineComponent.h"
#include"Components/BoxComponent.h"
cpp 复制代码
	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category = "MyCurve")
	UCurveFloat* MyCurveFloat;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyCurve")
	UTimelineComponent* MyTimeline;
	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category = "MyScenceComponent")
	USceneComponent* MyScene;
	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category = "MyScenceComponent")
	UStaticMeshComponent* MyDoorMesh;
	UPROPERTY(EditAnywhere,BlueprintReadWrite,Category = "MyScenceComponent")
	UBoxComponent* MyBox;

	FOnTimelineFloat TimelineDelegate;
	FOnTimelineEvent TimelineFinishedDelegate;
	UFUNCTION()
	void TimelineStart(float value);
	UFUNCTION()
	void TimelineFinished();
	UFUNCTION()
	void BeginOverlapFunction( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
	UFUNCTION()
	void EndOverlapFunction( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

再上篇的基础上,添加了StaticMeshComponent, UBoxComponent。和碰撞的三个函数。

二.添加组件 并进行加载

构造函数中将组件的父子级关系设置好,静态加载Drremesh,再设置碰撞大小。

cpp 复制代码
// Sets default values
AMyTimeLineActor::AMyTimeLineActor()
{
 	// 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;
	MyTimeline = CreateDefaultSubobject<UTimelineComponent>(TEXT("MyTimeLineComponent")); 
	MyScene = CreateDefaultSubobject<USceneComponent>(TEXT("MySenceComponet"));
	MyBox = CreateDefaultSubobject<UBoxComponent>(TEXT("MyBoxComponent"));
	MyDoorMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMeshComponet"));
	static ConstructorHelpers::FObjectFinder<UStaticMesh>TmpStaticMesh(TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Architecture/Wall_400x400.Wall_400x400'"));
	if (TmpStaticMesh.Succeeded())
	{
		MyDoorMesh->SetStaticMesh(TmpStaticMesh.Object);
	}
	//MyScene->SetupAttachment(RootComponent);
	RootComponent = MyScene;
	MyDoorMesh->SetupAttachment(MyScene);

	MyBox->SetupAttachment(MyScene);
	MyBox->SetBoxExtent(FVector(200,100,100));
	MyBox->SetRelativeLocation(FVector(200,0,0));
}

三.设置TimeLine的参数

将TimeLine进行代理绑定,将碰撞进行代理绑定。

cpp 复制代码
void AMyTimeLineActor::BeginPlay()
{
	Super::BeginPlay();
	TimelineDelegate.BindUFunction(this,TEXT("TimelineStart"));   //dailiyoucanshu
	TimelineFinishedDelegate.BindUFunction(this,TEXT("TimelineFinished"));
	MyTimeline->AddInterpFloat(MyCurveFloat,TimelineDelegate);
	MyTimeline->SetLooping(false);
	//MyTimeline->PlayFromStart();
	//MyTimeline->Play();
	MyTimeline->SetTimelineFinishedFunc(TimelineFinishedDelegate);   //
	 
	MyBox->OnComponentBeginOverlap.AddDynamic(this, &AMyTimeLineActor::BeginOverlapFunction);
	MyBox->OnComponentEndOverlap.AddDynamic(this, &AMyTimeLineActor::EndOverlapFunction);
}

四.在时间轴设置对应逻辑

设置时间轴持续调用的,TimelineStart(float value)。其中value是 对应Time的曲线value。这里持续开门。开到90°

cpp 复制代码
void AMyTimeLineActor::TimelineStart(float value)
{
	GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,FString::Printf(TEXT("Timelineplay %f"),value));
	float YawRotation = FMath::Lerp(0,90,value);
	MyDoorMesh->SetRelativeRotation(FRotator(0,YawRotation,0));
}

void AMyTimeLineActor::TimelineFinished()
{
	GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,TEXT("TimelineFinshed"));

}

五.在碰撞设置Timeline的播放

判断是否是MyCharacter,再进行开门动画播放。离开时,倒过来播放

cpp 复制代码
void AMyTimeLineActor::BeginOverlapFunction(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	AMyCharacter* TmpCharacter = Cast<AMyCharacter>(OtherActor);
	if (TmpCharacter)
	{
		MyTimeline->PlayFromStart();
	}
}

void AMyTimeLineActor::EndOverlapFunction(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	AMyCharacter* TmpCharacter = Cast<AMyCharacter>(OtherActor);
	if (TmpCharacter)
	{
		MyTimeline->ReverseFromEnd();
	}
	//GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("TimelineFinshed"));
}

相关推荐
一点媛艺2 小时前
Kotlin函数由易到难
开发语言·python·kotlin
姑苏风2 小时前
《Kotlin实战》-附录
android·开发语言·kotlin
奋斗的小花生3 小时前
c++ 多态性
开发语言·c++
魔道不误砍柴功3 小时前
Java 中如何巧妙应用 Function 让方法复用性更强
java·开发语言·python
闲晨3 小时前
C++ 继承:代码传承的魔法棒,开启奇幻编程之旅
java·c语言·开发语言·c++·经验分享
老猿讲编程3 小时前
一个例子来说明Ada语言的实时性支持
开发语言·ada
UestcXiye4 小时前
《TCP/IP网络编程》学习笔记 | Chapter 3:地址族与数据序列
c++·计算机网络·ip·tcp
Chrikk4 小时前
Go-性能调优实战案例
开发语言·后端·golang
幼儿园老大*4 小时前
Go的环境搭建以及GoLand安装教程
开发语言·经验分享·后端·golang·go
canyuemanyue4 小时前
go语言连续监控事件并回调处理
开发语言·后端·golang