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

一.添加门头文件 和 声明 

#include "Components/TimelineComponent.h"
#include"Components/BoxComponent.h"
	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,再设置碰撞大小。

// 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进行代理绑定,将碰撞进行代理绑定。

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°

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,再进行开门动画播放。离开时,倒过来播放

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"));
}

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3 Timeline 时间线实现会拐弯的时间轴相对于普通的时间线来说更加有趣和独特。在实现这种时间轴时,我们可以使用 Vue3 的组件化开发方式来创建一个可重复使用的时间线组件。 首先,我们需要创建一个 TimeNode(时间节点)组件,用来表示每个时间点。我们可以为每个节点设置一个时间和一个事件描述。时间节点可以通过计算属性或者接收传递的属性来设定其位置。 接下来,在时间线组件中,我们需要创建一个数组来存储时间节点。我们可以使用 v-for 指令来遍历这个数组,并将每个时间节点渲染到视图中。 然后,我们可以使用 CSS 来为时间节点添加样式,使其能够正确地显示出拐弯的效果。我们可以为时间节点设置不同的样式类,然后在 CSS 中定义这些样式类的样式规则,来实现时间轴的拐弯效果。 最后,我们可以根据时间节点的数量和位置,计算出时间轴的长度和位置,然后通过 CSS 来设置时间轴的样式,使其正确地显示出拐弯的时间轴效果。 通过以上的步骤,我们就可以实现一个拐弯的时间轴组件。这个组件可以根据需要接收传递的属性,可以根据时间节点的数量和位置来自动计算出时间轴的样式,使其能够正确地显示出拐弯的效果。 总之,Vue3 Timeline 时间线实现会拐弯的时间轴,通过使用 Vue3 的组件化开发方式,结合计算属性、遍历数组、添加样式和计算位置等技巧,可以比较容易地实现出一个有趣和独特的时间轴组件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值