-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathOpenDoorWithLerp.cpp
85 lines (70 loc) · 3.08 KB
/
OpenDoorWithLerp.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Harrison McGuire
// UE4 Version 4.20.2
// https://github.com/Harrison1/unrealcpp
// https://severallevels.io
// https://harrisonmcguire.com
// helpful links
// https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/USceneComponent/index.html
// https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UPrimitiveComponent/OnComponentBeginOverlap/
// https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UPrimitiveComponent/OnComponentEndOverlap/
// https://docs.unrealengine.com/latest/INT/BlueprintAPI/Math/Vector/UnrotateVector/
// https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Kismet/UKismetMathLibrary/LessLess_VectorRotator/index.html
// https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Kismet/UKismetMathLibrary/index.html
#include "OpenDoorWithLerp.h"
#include "Components/BoxComponent.h"
#include "Kismet/KismetMathLibrary.h"
// Sets default values
AOpenDoorWithLerp::AOpenDoorWithLerp()
{
// 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;
Open = false;
MyBoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("My Box Component"));
MyBoxComponent->InitBoxExtent(FVector(50,50,50));
RootComponent = MyBoxComponent;
Door = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("My Mesh"));
Door->SetRelativeLocation(FVector(0.0f, 50.0f, -50.0f));
Door->SetupAttachment(RootComponent);
MyBoxComponent->OnComponentBeginOverlap.AddDynamic(this, &AOpenDoorWithLerp::OnOverlapBegin);
MyBoxComponent->OnComponentEndOverlap.AddDynamic(this, &AOpenDoorWithLerp::OnOverlapEnd);
}
// Called every frame
void AOpenDoorWithLerp::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// maybe in UE5,RelativeRotation is private
DoorRotation = Door->GetRelativeRotation();
if(Open)
{
Door->SetRelativeRotation(FMath::Lerp(FQuat(DoorRotation), FQuat(FRotator(0.0f, RotateValue, 0.0f)), 0.01f));
}
else
{
Door->SetRelativeRotation(FMath::Lerp(FQuat(DoorRotation), FQuat(FRotator(0.0f, 0.0f, 0.0f)), 0.01f));
}
}
void AOpenDoorWithLerp::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor != nullptr && OtherActor != this && OtherComp != nullptr)
{
FVector PawnLocation = OtherActor->GetActorLocation();
FVector Direction = GetActorLocation() - PawnLocation;
Direction = UKismetMathLibrary::LessLess_VectorRotator(Direction, GetActorRotation());
if(Direction.X > 0.0f)
{
RotateValue = 90.0f;
}
else
{
RotateValue = -90.0f;
}
Open = true;
}
}
void AOpenDoorWithLerp::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (OtherActor != nullptr && OtherActor != this && OtherComp != nullptr)
{
Open = false;
}
}