I’ve taken another extra step to allow the designer more editability when it comes to the door.
void UOpenDoor::BeginPlay()
{
Super::BeginPlay();
ActorThatOpens = GetWorld()->GetFirstPlayerController()->GetPawn();
InitialYaw = GetOwner()->GetActorRotation().Yaw;
CurrentYaw = InitialYaw;
OpenAngle += InitialYaw; // TargetYaw = TargetYaw + InitialYaw;
if (!PressurePlate)
{
UE_LOG(LogTemp, Warning, TEXT("\"%s\" has DoorComponent, but no pressure plate set."), *GetOwner()->GetName())
}
}
// Called every frame
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (PressurePlate && PressurePlate->IsOverlappingActor(ActorThatOpens))
{
OpenCloseDoor(DeltaTime, OpenAngle, DoorOpenSpeed);
DoorLastOpened = GetWorld()->GetTimeSeconds();
}
else if (DoorLastOpened <= GetWorld()->GetTimeSeconds() - DoorCloseDelay)
{
OpenCloseDoor(DeltaTime, InitialYaw, DoorCloseSpeed);
}
}
void UOpenDoor::OpenCloseDoor(float DeltaTime, float Yaw, float DoorSpeed)
{
CurrentYaw = FMath::FInterpTo(CurrentYaw, Yaw, DeltaTime, DoorSpeed); // smooth transition door open
FRotator DoorRotation = GetOwner()->GetActorRotation(); // create editable variable that has the current door rotation
DoorRotation.Yaw = CurrentYaw; // set the yaw to whatever the interp function has it set to currently
GetOwner()->SetActorRotation(DoorRotation); // after all the calculations above, set the door to the current rotation that the lerp is calculating
return;
}
I’ve allowed the ability for the designer to set both open and close speeds all the while using one single function to open and close the door.
If someone could tell me if this is a good idea and if it makes the code more efficient please let me know. I’d like to know if I done a good job re-factoring the code to make it more efficient than it was