Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #header .h
- #pragma once
- #include "CoreMinimal.h"
- #include "GameFramework/Actor.h"
- #include "Components/TimeLineComponent.h"
- #include "Planet.generated.h"
- class UStaticMeshComponent;
- class UTimelineComponent;
- class UCurveFloat;
- UCLASS()
- class STARTRANSPORT_API APlanet : public AActor
- {
- GENERATED_BODY()
- public:
- // Sets default values for this actor's properties
- APlanet();
- protected:
- // Called when the game starts or when spawned
- virtual void BeginPlay() override;
- /* Mesh */
- UPROPERTY(EditDefaultsOnly, Category = "Planet")
- TObjectPtr<UStaticMeshComponent> PlanetMesh;
- /* Timeline */
- UPROPERTY(EditAnywhere,BlueprintReadWrite, Category = "Planet")
- TObjectPtr<UTimelineComponent> MainTimeline;
- /* Curve */
- UPROPERTY(EditAnywhere,BlueprintReadWrite, Category = "Planet")
- TObjectPtr<UCurveFloat> PlanetRotationCurve;
- UFUNCTION()
- void RotatePlanet(float Value);
- UFUNCTION()
- void TimelineEnd();
- private:
- FRotator PlanetCurrentRotation = FRotator(0.f);
- FRotator PlanetTargetRotation = FRotator(0.f);
- float RotationAmount = 360.0f;
- float LengthOfTimeline = 30.0f;
- };
- #cpp
- #include "Actors/Planet/Planet.h"
- #include "Components/StaticMeshComponent.h"
- #include "Components/TimeLineComponent.h"
- #include "Curves/CurveFloat.h"
- #include "Kismet/GameplayStatics.h"
- // Sets default values
- APlanet::APlanet()
- {
- // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
- PrimaryActorTick.bStartWithTickEnabled = true;
- PrimaryActorTick.bCanEverTick = true;
- bNetLoadOnClient = false;
- SetCanBeDamaged(false);
- //Mesh
- PlanetMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PlanetMesh"));
- PlanetMesh->SetEnableGravity(false);
- PlanetMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
- //Root Component
- SetRootComponent(PlanetMesh);
- //Curve to use
- PlanetRotationCurve = CreateDefaultSubobject<UCurveFloat>(TEXT("CurveFloat"));
- //Timeline
- MainTimeline = CreateDefaultSubobject<UTimelineComponent>(TEXT("TimeLineComp"));
- }
- // Called when the game starts or when spawned
- void APlanet::BeginPlay()
- {
- Super::BeginPlay();
- //Bind the timelineupdate to the rotateplanet function
- FOnTimelineFloat TimelineMovementValue;
- FOnTimelineEvent TimelineFinishEvent;
- //Bind functions to timeline
- TimelineMovementValue.BindUFunction(this, FName("RotatePlanet"));
- TimelineFinishEvent.BindUFunction(this, FName("TimelineEnd"));
- if (PlanetRotationCurve)
- {
- //This will allow the curve to feed the data to TimelineMovementValue, which in turn will give RotatePlanet() its value param.
- MainTimeline->AddInterpFloat(PlanetRotationCurve, TimelineMovementValue);
- //This will now allow the timline, when finished, to call the TimlineFinish event of StartRotating()
- MainTimeline->SetTimelineFinishedFunc(TimelineFinishEvent);
- }
- //Initialize the Starting Rotation and Ending Rotation
- PlanetCurrentRotation = GetActorRotation();
- PlanetTargetRotation = PlanetCurrentRotation;
- //Add the 360 degrees on the Yaw
- PlanetTargetRotation.Yaw += RotationAmount;
- //Start the timline
- MainTimeline->PlayFromStart();
- }
- void APlanet::RotatePlanet(float Value)
- {
- //Create the new rotation
- FRotator NewRotation = FMath::Lerp(PlanetCurrentRotation, PlanetTargetRotation, Value);
- SetActorRotation(NewRotation);
- UE_LOG(LogTemp, Warning, TEXT("VALUE: %f "), Value);
- UE_LOG(LogTemp, Warning, TEXT("CurrentRotation %s"), *GetActorRotation().ToString());
- UE_LOG(LogTemp, Warning, TEXT("PlanetTargetRot: %s"), *PlanetTargetRotation.ToString());
- }
- //Rotate the planet
- void APlanet::TimelineEnd()
- {
- UE_LOG(LogTemp, Warning, TEXT("TimelineEndCalled"));
- }
Advertisement
Add Comment
Please, Sign In to add comment