当前位置:   article > 正文

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

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<UTimelineComponent>(TEXT("MyTimeLineComponent"));
  7. MyScene = CreateDefaultSubobject<USceneComponent>(TEXT("MySenceComponet"));
  8. MyBox = CreateDefaultSubobject<UBoxComponent>(TEXT("MyBoxComponent"));
  9. MyDoorMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMeshComponet"));
  10. static ConstructorHelpers::FObjectFinder<UStaticMesh>TmpStaticMesh(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<AMyCharacter>(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<AMyCharacter>(OtherActor);
  12. if (TmpCharacter)
  13. {
  14. MyTimeline->ReverseFromEnd();
  15. }
  16. //GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("TimelineFinshed"));
  17. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/484619
推荐阅读
相关标签
  

闽ICP备14008679号