-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mover.cpp
34 lines (34 loc) · 1.15 KB
/
Mover.cpp
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
#include "Mover.h"
#include "Math/UnrealMathUtility.h"
// Sets default values for this component's properties
UMover::UMover()
{
// 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 UMover::BeginPlay()
{
Super::BeginPlay();
OrignalLocation = GetOwner()->GetActorLocation();
}
// Called every frame
void UMover::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
FVector TargetLocation = OrignalLocation;
if (ShouldMove)
{
TargetLocation = OrignalLocation + MoveOffset;
}
FVector CurrentLocation = GetOwner()->GetActorLocation();
float Speed = MoveOffset.Length() / MoveTime;
FVector NewLocation = FMath::VInterpConstantTo(CurrentLocation, TargetLocation, DeltaTime, Speed);
GetOwner()->SetActorLocation(NewLocation);
}
void UMover::SetShouldMove(bool NewShouldMove)
{
ShouldMove = NewShouldMove;
}