unleashedcode

Lerp With Timeline and Curvefloat

Jun 8th, 2023
1,177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 KB | Source Code | 0 0
  1. #header .h
  2.  
  3. #pragma once
  4.  
  5. #include "CoreMinimal.h"
  6. #include "GameFramework/Actor.h"
  7. #include "Components/TimeLineComponent.h"
  8. #include "Planet.generated.h"
  9.  
  10.  
  11. class UStaticMeshComponent;
  12. class UTimelineComponent;
  13. class UCurveFloat;
  14.  
  15.  
  16. UCLASS()
  17. class STARTRANSPORT_API APlanet : public AActor
  18. {
  19.     GENERATED_BODY()
  20.    
  21. public:
  22.     // Sets default values for this actor's properties
  23.     APlanet();
  24.  
  25.  
  26.    
  27.  
  28. protected:
  29.     // Called when the game starts or when spawned
  30.     virtual void BeginPlay() override;
  31.    
  32.     /* Mesh */
  33.     UPROPERTY(EditDefaultsOnly, Category = "Planet")
  34.     TObjectPtr<UStaticMeshComponent> PlanetMesh;
  35.  
  36.     /* Timeline */
  37.     UPROPERTY(EditAnywhere,BlueprintReadWrite, Category = "Planet")
  38.     TObjectPtr<UTimelineComponent> MainTimeline;
  39.  
  40.     /* Curve */
  41.     UPROPERTY(EditAnywhere,BlueprintReadWrite, Category = "Planet")
  42.     TObjectPtr<UCurveFloat> PlanetRotationCurve;
  43.  
  44.  
  45.     UFUNCTION()
  46.     void RotatePlanet(float Value);
  47.  
  48.     UFUNCTION()
  49.     void TimelineEnd();
  50.  
  51.  
  52.  
  53. private:   
  54.        
  55.     FRotator PlanetCurrentRotation = FRotator(0.f);
  56.     FRotator PlanetTargetRotation = FRotator(0.f);
  57.  
  58.     float RotationAmount = 360.0f;
  59.     float LengthOfTimeline = 30.0f;
  60.    
  61. };
  62.  
  63. #cpp
  64.  
  65. #include "Actors/Planet/Planet.h"
  66. #include "Components/StaticMeshComponent.h"
  67. #include "Components/TimeLineComponent.h"
  68. #include "Curves/CurveFloat.h"
  69. #include "Kismet/GameplayStatics.h"
  70.  
  71.  
  72. // Sets default values
  73. APlanet::APlanet()
  74. {
  75.     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  76.     PrimaryActorTick.bStartWithTickEnabled = true;
  77.     PrimaryActorTick.bCanEverTick = true;
  78.     bNetLoadOnClient = false;
  79.     SetCanBeDamaged(false);
  80.  
  81.     //Mesh
  82.     PlanetMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PlanetMesh"));
  83.     PlanetMesh->SetEnableGravity(false);
  84.     PlanetMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);   
  85.    
  86.     //Root Component
  87.     SetRootComponent(PlanetMesh);
  88.    
  89.     //Curve to use
  90.     PlanetRotationCurve = CreateDefaultSubobject<UCurveFloat>(TEXT("CurveFloat"));
  91.    
  92.     //Timeline
  93.     MainTimeline = CreateDefaultSubobject<UTimelineComponent>(TEXT("TimeLineComp"));
  94. }
  95.  
  96.  
  97.  
  98. // Called when the game starts or when spawned
  99. void APlanet::BeginPlay()
  100. {
  101.     Super::BeginPlay();
  102.  
  103.     //Bind the timelineupdate to the rotateplanet function
  104.     FOnTimelineFloat TimelineMovementValue;
  105.     FOnTimelineEvent TimelineFinishEvent;
  106.  
  107.     //Bind functions to timeline
  108.     TimelineMovementValue.BindUFunction(this, FName("RotatePlanet"));
  109.     TimelineFinishEvent.BindUFunction(this, FName("TimelineEnd"));
  110.  
  111.     if (PlanetRotationCurve)
  112.     {      
  113.         //This will allow the curve to feed the data to TimelineMovementValue, which in turn will give RotatePlanet() its value param.
  114.         MainTimeline->AddInterpFloat(PlanetRotationCurve, TimelineMovementValue);
  115.  
  116.         //This will now allow the timline, when finished, to call the TimlineFinish event of StartRotating()
  117.         MainTimeline->SetTimelineFinishedFunc(TimelineFinishEvent);
  118.     }
  119.        
  120.  
  121.     //Initialize the Starting Rotation and Ending Rotation
  122.     PlanetCurrentRotation = GetActorRotation();
  123.     PlanetTargetRotation = PlanetCurrentRotation;
  124.  
  125.     //Add the 360 degrees on the Yaw
  126.     PlanetTargetRotation.Yaw += RotationAmount;
  127.    
  128.     //Start the timline
  129.     MainTimeline->PlayFromStart();
  130.  
  131. }
  132.  
  133.  
  134.  
  135. void APlanet::RotatePlanet(float Value)
  136. {
  137.     //Create the new rotation
  138.     FRotator NewRotation = FMath::Lerp(PlanetCurrentRotation, PlanetTargetRotation, Value);
  139.     SetActorRotation(NewRotation);
  140.    
  141.        
  142.     UE_LOG(LogTemp, Warning, TEXT("VALUE: %f "), Value);
  143.     UE_LOG(LogTemp, Warning, TEXT("CurrentRotation %s"), *GetActorRotation().ToString());
  144.     UE_LOG(LogTemp, Warning, TEXT("PlanetTargetRot: %s"),  *PlanetTargetRotation.ToString());
  145.    
  146.  
  147.  
  148.  
  149. }
  150.  
  151. //Rotate the planet
  152. void APlanet::TimelineEnd()
  153. {
  154.     UE_LOG(LogTemp, Warning, TEXT("TimelineEndCalled"));
  155. }
  156.  
Advertisement
Add Comment
Please, Sign In to add comment