Skip to content

Instantly share code, notes, and snippets.

@petru23
Last active February 11, 2018 17:14
Show Gist options
  • Save petru23/109cbde93dbfc5b8aeac9157287be569 to your computer and use it in GitHub Desktop.
Save petru23/109cbde93dbfc5b8aeac9157287be569 to your computer and use it in GitHub Desktop.
Moving a Component's child based on a curve
// Fill out your copyright notice in the Description page of Project Settings.
#include "UCurveComponent.h"
// Sets default values for this component's properties
UUCurveComponent::UUCurveComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UUCurveComponent::BeginPlay()
{
Super::BeginPlay();
ElapsedTime = 0;
CurveMaxTime = 10;
float min;
CurveVector->GetTimeRange(min, CurveMaxTime);
}
// Called every frame
void UUCurveComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
ElapsedTime += DeltaTime * Speed;
if (ElapsedTime > CurveMaxTime)
{
ElapsedTime -= CurveMaxTime;
}
FVector Position = CurveVector->GetVectorValue(ElapsedTime);
SetRelativeLocation(Position);
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/SceneComponent.h"
#include "Runtime/Engine/Classes/Curves/CurveBase.h"
#include "Runtime/Engine/Classes/Curves/CurveVector.h"
#include "UCurveComponent.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class EXCERCISE_TASK1_2_API UUCurveComponent : public USceneComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UUCurveComponent();
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
UPROPERTY(EditAnywhere)
UCurveVector* CurveVector;
UPROPERTY(EditAnywhere)
float Speed;
protected:
// Called when the game starts
virtual void BeginPlay() override;
private:
float ElapsedTime;
float CurveMaxTime;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment