-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBobbingCube.cpp
More file actions
99 lines (81 loc) · 2.49 KB
/
Copy pathBobbingCube.cpp
File metadata and controls
99 lines (81 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Fill out your copyright notice in the Description page of Project Settings.
#include "BobbingCube.h"
#include "Components/TimelineComponent.h"
// Sets default values
ABobbingCube::ABobbingCube()
{
// 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;
RotatableMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("RotatableMesh"));
AnimTrigger = CreateDefaultSubobject<UBoxComponent>(TEXT("AnimationTrigger"));
AnimTrigger->SetGenerateOverlapEvents(true);
AnimTrigger->OnComponentBeginOverlap.AddDynamic(this, &ABobbingCube::OnAnimTriggered);
AnimTrigger->SetupAttachment(RotatableMesh); //FAttachmentTransformRules::SnapToTargetNotIncludingScale);
}
// Called when the game starts or when spawned
void ABobbingCube::BeginPlay()
{
Super::BeginPlay();
/*
if (AnimCurve)
{
FOnTimelineFloat ProgressFunction;
ProgressFunction.BindUFunction(this, FName("HandleProgress"));
MyTimeline.AddInterpFloat(AnimCurve, ProgressFunction);
MyTimeline.SetLooping(false);
InitialRotation = TargetRotation = GetActorRotation();
if (RotationAxis == "Yaw")
{
TargetRotation.Yaw += MaxRotation;
}
else if (RotationAxis == "Pitch")
{
TargetRotation.Pitch += MaxRotation;
}
else if (RotationAxis == "Roll")
{
TargetRotation.Roll += MaxRotation;
}
}
*/
if (AnimCurve)
{
FOnTimelineFloat ProgressFunction;
ProgressFunction.BindUFunction(this, FName("HandleProgress"));
MyTimeline.AddInterpFloat(AnimCurve, ProgressFunction);
MyTimeline.SetLooping(false);
InitialPosition = TargetPosition = GetActorLocation();
if (TransformationAxis == "Z")
{
TargetPosition.Z += MaxSpeed;
}
if (TransformationAxis == "X")
{
TargetPosition.X += MaxSpeed;
}
if (TransformationAxis == "Y")
{
TargetPosition.Y += MaxSpeed;
}
}
}
// Called every frame
void ABobbingCube::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
MyTimeline.TickTimeline(DeltaTime);
}
void ABobbingCube::HandleProgress(float Value)
{
FVector NewPosition = FMath::Lerp(InitialPosition, TargetPosition, Value);
SetActorLocation(NewPosition);
}
void ABobbingCube::OnAnimTriggered(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex,
bool bFromSweep, const FHitResult& SweepResult)
{
if (!MyTimeline.IsPlaying())
{
MyTimeline.PlayFromStart();
}
}