-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMovingPlatform.cpp
70 lines (51 loc) · 1.54 KB
/
MovingPlatform.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
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
// Fill out your copyright notice in the Description page of Project Settings.
#include "MovingPlatform.h"
// Sets default values
AMovingPlatform::AMovingPlatform()
{
// 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;
}
// Called when the game starts or when spawned
void AMovingPlatform::BeginPlay()
{
Super::BeginPlay();
StartLocation = GetActorLocation();
FString Name = GetName();
UE_LOG(LogTemp, Display, TEXT("Get Name: %s"), *Name);
}
// Called every frame
void AMovingPlatform::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
MovePlatform(DeltaTime);
RotatePlatform(DeltaTime);
}
void AMovingPlatform::MovePlatform(float DeltaTime)
{
if (ShouldPlatformReturn())
{
FVector MoveDirection = PlatformVelocity.GetSafeNormal();
StartLocation = StartLocation + MoveDirection * MoveDistance;
SetActorLocation(StartLocation);
PlatformVelocity = -PlatformVelocity;
}
else
{
FVector CurrentLocation = GetActorLocation();
CurrentLocation = CurrentLocation + PlatformVelocity * DeltaTime;
SetActorLocation(CurrentLocation);
}
}
void AMovingPlatform::RotatePlatform(float DeltaTime)
{
AddActorLocalRotation(RotationVelocity * DeltaTime);
}
float AMovingPlatform::GetDistanceMoved() const
{
return FVector::Dist(StartLocation, GetActorLocation());
}
bool AMovingPlatform::ShouldPlatformReturn() const
{
return GetDistanceMoved() > MoveDistance;
}