Hello everyone,
I’m facing a trouble with my timeline. I have a platform and I’m using a timeline to move that platform.
I’ve set a variable to control the time (TravelTimeInSeconds) that my platform will take to move from the initial point to the end point in seconds. Whatever is the value I set, the platform always take less time than I set.
I’m using FMath::Lerp(), and the alpha value is the float value of my curve, and the time from 0 to 1 takes exactly the time I set, but the actor is moving more faster than that.
Example: if I set 5 seconds, the float value will take 5 seconds from 0 to 1.
I made a gif to show what I’m saying.
Here it is the video link if you want to see with more resolution
And this is my C++ code
.H
#pragma once
#include "GameFramework/Actor.h"
#include "MovablePlatform.generated.h"
UCLASS()
class PROJECTHEC_API AMovablePlatform : public AActor
{
GENERATED_BODY()
virtual void PostInitializeComponents() override;
UPROPERTY()
class USceneComponent* Root;
UPROPERTY(EditAnywhere)
class UPaperSpriteComponent* SpriteMovablePlatform;
UPROPERTY()
class UPaperSprite* SourceMovablePlatform;
// The end location where is the platform will go
UPROPERTY(EditAnywhere, Category = PlatformControls)
FVector EndLocation;
// Storing the initial location of the actor
FVector InitialLocation;
// The time that the platform will take to get to the end location
UPROPERTY(EditAnywhere, Category = PlatformControls)
float TravelTimeInSeconds;
// The time the platform will stay stoped
UPROPERTY(EditAnywhere, Category = PlatformControls)
float StopDelay = 0.0f;
// Just to test the end location of the platform
UPROPERTY(EditAnywhere, Category = PlatformControls)
bool bTestEndLocation = false;
UPROPERTY(EditAnywhere, Category = PlatformControls)
bool bOrientationAxisX;
UPROPERTY(EditAnywhere, Category = PlatformControls)
bool bOrientationAxisZ;
//Timeline
UFUNCTION()
void MovePlatform(float Value);
class UCurveFloat* TimelineCurve;
FTimeline PlatformTimeline;
.Cpp
#include "ProjectHEC.h"
#include "MovablePlatform.h"
#include "PaperSpriteComponent.h"
#include "Runtime/Engine/Classes/Components/TimelineComponent.h"
// Sets default values
MovablePlatform::AMovablePlatform()
{
// 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<UPaperSprite> MovablePlatformAsset(TEXT("PaperSprite'/Game/Sprites/Ledge.Ledge'"));
SourceMovablePlatform = MovablePlatformAsset.Get();
Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
RootComponent = Root;
SpriteMovablePlatform = CreateDefaultSubobject<UPaperSpriteComponent>(TEXT("MovablePlatform"));
SpriteMovablePlatform->SetSprite(SourceMovablePlatform);
SpriteMovablePlatform->AttachTo(RootComponent);
//Timeline - Try later
ConstructorHelpers::FObjectFinder<UCurveFloat> Curve(TEXT("CurveFloat'/Game/Data/TimelineCurve.TimelineCurve'"));
TimelineCurve = Curve.Object;
PlatformTimeline = FTimeline{};
FOnTimelineFloat InterpFunc{};
InterpFunc.BindUFunction(this, FName("MovePlatform"));
PlatformTimeline.AddInterpFloat(TimelineCurve, InterpFunc, FName("PlatformTimeline"));
}
void AMovablePlatform::PostInitializeComponents()
{
Super::PostInitializeComponents();
InitialLocation = GetActorLocation();
EndLocation += InitialLocation;
}
// Called when the game starts or when spawned
void AMovablePlatform::BeginPlay()
{
Super::BeginPlay();
PlatformTimeline.PlayFromStart();
PlatformTimeline.SetPlayRate(1/TravelTimeInSeconds);
}
// Called every frame
void AMovablePlatform::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
PlatformTimeline.TickTimeline(DeltaTime);
}
void AMovablePlatform::MovePlatform(float Value)
{
EndLocation = FMath::Lerp(EndLocation, InitialLocation, TimelineCurve->GetFloatValue(Value));
SetActorRelativeLocation(EndLocation);
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::, FString::SanitizeFloat(Value));
}
If anyone could help me with this issue I’ll be grateful
PS.: Sorry for my English or if I wrote anything wrong.
Thanks.