UE4 C++ 时间线(TimeLine)

使用时间线让物体缓慢移动
在头文件中声明移动的初始位置和目标位置

UPROPERTY(EditAnyWhere, Category = "PlatformControls")
        FVector EndLocation;
    UPROPERTY(EditAnyWhere, Category = "PlatformControls")
    FVector InitialLcation;

声明执行时间线的函数

UFUNCTION()
        void MovePlatform(float Value)

申明Curvefloat对象和timeline对象

class UCurveFloat *TimeLineCurve;
    FTimeline PlatformTimeline;

在CPP文件中首先加载UCurveFloat

    ConstructorHelpers::FObjectFinderCurve(TEXT("/Game/NewCurveBase.NewCurveBase"));

    if (Curve.Succeeded())
        {
            TimeLineCurve = Curve.Object;
        }

CPP文件如下

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

#include "TimeLine.h"
#include "MyActor.h"
//#include "PaperSpriteComponent.h"
#include "Runtime/Engine/Classes/Components/TimelineComponent.h"
#include"Engine/GameEngine.h"


//void AMyActor::PostInitializeComponents()
//{
//  Super::PostInitializeComponents();
//
//  InitialLcation = GetActorLocation();
//  EndLocation += InitialLcation;
//}

void AMyActor::MovePlatform(float Value)


{
    //EndLocation += InitialLcation;
    InitialLcation = FMath::Lerp(InitialLcation, EndLocation, TimeLineCurve->GetFloatValue(Value));
    SetActorRelativeLocation(InitialLcation);
    GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,FString::SanitizeFloat(Value));
}

// Sets default values
AMyActor::AMyActor()
{
    // 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;
    //ConstructorHelpers::FObjectFinderOptional(TEXT("/Game/Lege.Lege"));
    //SoureMoveblePlatform=
    RootStatic = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("root"));
    static ConstructorHelpers::FObjectFinder<UStaticMesh>RootAsset(TEXT("/Game/Floor_Mesh.Floor_Mesh")); 
    ConstructorHelpers::FObjectFinder<UCurveFloat>Curve(TEXT("/Game/NewCurveBase.NewCurveBase"));

    //RootComponent = Root;
    if (RootAsset.Succeeded())
    {
        RootStatic->SetStaticMesh(RootAsset.Object);
    }
    RootComponent = RootStatic;

        if (Curve.Succeeded())
        {
            TimeLineCurve = Curve.Object;
        }

        //RootComponent = Root;
    PlatformTimeline = FTimeline{};
    FOnTimelineFloat InterpFunc{};
    InterpFunc.BindUFunction(this,FName("MovePlatform"));
    PlatformTimeline.AddInterpFloat(TimeLineCurve,InterpFunc,FName("PlatformTimeline"));


}

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
    Super::BeginPlay();
    //UUserWidget
    //WidgetInstance->AddToViewport();

    InitialLcation = GetActorLocation();
    PlatformTimeline.PlayFromStart();
    //PlatformTimeline.ReverseFromEnd();
    //float time= GetWorld()->GetTimerManager().
    //PlatformTimeline.SetTimelineFinishedFunc(void(AMyActor::BeginPlay()));
    //UGameplayStatics::GetPlayerController(this,0)->

    PlatformTimeline.SetPlayRate(1);

}

// Called every frame
void AMyActor::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
    PlatformTimeline.TickTimeline(DeltaTime);


}

切记要加头文件
源码下载
https://github.com/amazelucc/TimeLine

你可能感兴趣的:(ue4,c++)