2025-02-17 14:46:22 +01:00
|
|
|
#include "Player/InteractionComponent.h"
|
|
|
|
|
|
|
|
|
|
UInteractionComponent::UInteractionComponent()
|
|
|
|
|
{
|
|
|
|
|
PrimaryComponentTick.bCanEverTick = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void UInteractionComponent::BeginPlay()
|
|
|
|
|
{
|
|
|
|
|
Super::BeginPlay();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void UInteractionComponent::CheckForInteractable()
|
|
|
|
|
{
|
|
|
|
|
AActor* Owner = GetOwner();
|
|
|
|
|
|
|
|
|
|
FVector LineStart = Owner->GetActorLocation();
|
|
|
|
|
FVector ForwardVector = Owner->GetActorForwardVector();
|
|
|
|
|
FVector LineEnd = LineStart + (ForwardVector * InteractionDistance);
|
|
|
|
|
|
|
|
|
|
FHitResult HitResult;
|
|
|
|
|
FCollisionQueryParams QueryParams;
|
|
|
|
|
QueryParams.AddIgnoredActor(Owner);
|
|
|
|
|
|
|
|
|
|
bool bHit = GetWorld()->LineTraceSingleByChannel(HitResult, LineStart, LineEnd, ECC_Visibility, QueryParams);
|
|
|
|
|
|
2025-02-17 15:28:18 +01:00
|
|
|
if (bHit && HitResult.GetActor() && HitResult.GetActor()->Implements<UInteractable>())
|
2025-02-17 14:46:22 +01:00
|
|
|
{
|
2025-02-17 15:28:18 +01:00
|
|
|
InteractedActor = HitResult.GetActor();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
InteractedActor = nullptr;
|
2025-02-17 14:46:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bShowDebugLine)
|
|
|
|
|
DrawDebugLine(GetWorld(), LineStart, LineEnd, FColor::Red, false, 1.0f, 0, 1.0f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void UInteractionComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
|
|
|
|
|
{
|
|
|
|
|
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
|
|
|
|
|
CheckForInteractable();
|
|
|
|
|
}
|
|
|
|
|
|