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


    一.添加门头文件 和 声明 

    1. #include "Components/TimelineComponent.h"
    2. #include"Components/BoxComponent.h"
    1. UPROPERTY(EditAnywhere,BlueprintReadWrite,Category = "MyCurve")
    2. UCurveFloat* MyCurveFloat;
    3. UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyCurve")
    4. UTimelineComponent* MyTimeline;
    5. UPROPERTY(EditAnywhere,BlueprintReadWrite,Category = "MyScenceComponent")
    6. USceneComponent* MyScene;
    7. UPROPERTY(EditAnywhere,BlueprintReadWrite,Category = "MyScenceComponent")
    8. UStaticMeshComponent* MyDoorMesh;
    9. UPROPERTY(EditAnywhere,BlueprintReadWrite,Category = "MyScenceComponent")
    10. UBoxComponent* MyBox;
    11. FOnTimelineFloat TimelineDelegate;
    12. FOnTimelineEvent TimelineFinishedDelegate;
    13. UFUNCTION()
    14. void TimelineStart(float value);
    15. UFUNCTION()
    16. void TimelineFinished();
    17. UFUNCTION()
    18. void BeginOverlapFunction( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
    19. UFUNCTION()
    20. void EndOverlapFunction( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

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

    二.添加组件 并进行加载

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

    1. // Sets default values
    2. AMyTimeLineActor::AMyTimeLineActor()
    3. {
    4. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
    5. PrimaryActorTick.bCanEverTick = true;
    6. MyTimeline = CreateDefaultSubobject(TEXT("MyTimeLineComponent"));
    7. MyScene = CreateDefaultSubobject(TEXT("MySenceComponet"));
    8. MyBox = CreateDefaultSubobject(TEXT("MyBoxComponent"));
    9. MyDoorMesh = CreateDefaultSubobject(TEXT("MyStaticMeshComponet"));
    10. static ConstructorHelpers::FObjectFinderTmpStaticMesh(TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Architecture/Wall_400x400.Wall_400x400'"));
    11. if (TmpStaticMesh.Succeeded())
    12. {
    13. MyDoorMesh->SetStaticMesh(TmpStaticMesh.Object);
    14. }
    15. //MyScene->SetupAttachment(RootComponent);
    16. RootComponent = MyScene;
    17. MyDoorMesh->SetupAttachment(MyScene);
    18. MyBox->SetupAttachment(MyScene);
    19. MyBox->SetBoxExtent(FVector(200,100,100));
    20. MyBox->SetRelativeLocation(FVector(200,0,0));
    21. }

    三.设置TimeLine的参数

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

    1. void AMyTimeLineActor::BeginPlay()
    2. {
    3. Super::BeginPlay();
    4. TimelineDelegate.BindUFunction(this,TEXT("TimelineStart")); //dailiyoucanshu
    5. TimelineFinishedDelegate.BindUFunction(this,TEXT("TimelineFinished"));
    6. MyTimeline->AddInterpFloat(MyCurveFloat,TimelineDelegate);
    7. MyTimeline->SetLooping(false);
    8. //MyTimeline->PlayFromStart();
    9. //MyTimeline->Play();
    10. MyTimeline->SetTimelineFinishedFunc(TimelineFinishedDelegate); //
    11. MyBox->OnComponentBeginOverlap.AddDynamic(this, &AMyTimeLineActor::BeginOverlapFunction);
    12. MyBox->OnComponentEndOverlap.AddDynamic(this, &AMyTimeLineActor::EndOverlapFunction);
    13. }

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

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

    1. void AMyTimeLineActor::TimelineStart(float value)
    2. {
    3. GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,FString::Printf(TEXT("Timelineplay %f"),value));
    4. float YawRotation = FMath::Lerp(0,90,value);
    5. MyDoorMesh->SetRelativeRotation(FRotator(0,YawRotation,0));
    6. }
    7. void AMyTimeLineActor::TimelineFinished()
    8. {
    9. GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,TEXT("TimelineFinshed"));
    10. }

    五.在碰撞设置Timeline的播放

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

    1. void AMyTimeLineActor::BeginOverlapFunction(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
    2. {
    3. AMyCharacter* TmpCharacter = Cast(OtherActor);
    4. if (TmpCharacter)
    5. {
    6. MyTimeline->PlayFromStart();
    7. }
    8. }
    9. void AMyTimeLineActor::EndOverlapFunction(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
    10. {
    11. AMyCharacter* TmpCharacter = Cast(OtherActor);
    12. if (TmpCharacter)
    13. {
    14. MyTimeline->ReverseFromEnd();
    15. }
    16. //GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("TimelineFinshed"));
    17. }

  • 相关阅读:
    Flir Blackfly S USB3 工业相机:计数器和定时器的使用方法
    【python中处理日期和时间一】_time及calendar相关函数
    vue路由原理
    四种常见分布式限流算法实现!
    AGV调度台软件QT
    hadoop04--Kafka集群环境搭建
    Timer实现简单计时控制器,毫秒级误差,浅谈守护线程和用户线程
    exFAT文件系统的目录与文件存储
    pycharm远程连接Linux服务器
    TLS及CA证书申请流程
  • 原文地址:https://blog.csdn.net/m0_64217692/article/details/137863466