class ACTIONROGUELIKE_API USInteractionComponent : public UActorComponent { GENERATED_BODY()
...
public: void PrimaryInteract(); };
void USInteractionComponent::PrimaryInteract() { FCollisionObjectQueryParams ObjectQueryParams; ObjectQueryParams.AddObjectTypesToQuery(ECC_WorldDynamic);
AActor* MyOwner = GetOwner();
FVector EyeLocation; FRotator EyeRotation; MyOwner->GetActorEyesViewPoint(EyeLocation, EyeRotation); FVector End = EyeLocation + (EyeRotation.Vector() * 1000);
#if 0 FHitResult HitResult; GetWorld()->LineTraceSingleByObjectType(HitResult, EyeLocation, End, ObjectQueryParams);
AActor* HitActor = HitResult.GetActor(); if (HitActor) { if (HitActor->Implements<USGameplayInterface>()) { APawn* MyPawn = Cast<APawn>(MyOwner); ISGameplayInterface::Execute_Interact(HitActor, MyPawn); } } DrawDebugLine(GetWorld(), EyeLocation, End, FColor::Red, false, 2.0f, 0, 2.0f); #elif 1 TArray<FHitResult> HitResults; float Radius = 30.f; FCollisionShape CollisionShape; CollisionShape.SetSphere(Radius); bool bBlockingHit = GetWorld()->SweepMultiByObjectType(HitResults, EyeLocation, End, FQuat::Identity, ObjectQueryParams, CollisionShape); FColor LineColor = bBlockingHit ? FColor::Green : FColor::Red;
for (FHitResult Hit : HitResults) { AActor* HitActor = Hit.GetActor(); if (HitActor) { if (HitActor->Implements<USGameplayInterface>()) { APawn* MyPawn = Cast<APawn>(MyOwner);
ISGameplayInterface::Execute_Interact(HitActor, MyPawn); DrawDebugSphere(GetWorld(), Hit.ImpactPoint, Radius, 32, LineColor, false, 2.0f); break; } } DrawDebugSphere(GetWorld(), Hit.ImpactPoint, Radius, 32, LineColor, false, 2.0f); }
DrawDebugLine(GetWorld(), EyeLocation, End, LineColor, false, 2.0f, 0, 2.0f); #else TArray<FOverlapResult> Overlaps; FCollisionQueryParams Params; Params.AddIgnoredActor(MyOwner); bool bBlockingHit = GetWorld()->OverlapMultiByObjectType(Overlaps, MyOwner->GetActorLocation(), FQuat::Identity, ObjectQueryParams, FCollisionShape::MakeSphere(200.f), Params); FColor LineColor = bBlockingHit ? FColor::Green : FColor::Red; for (FOverlapResult& Overlap : Overlaps) { AActor* HitActor = Overlap.GetActor(); if (HitActor) { if (HitActor->Implements<USGameplayInterface>()) { APawn* MyPawn = Cast<APawn>(MyOwner);
ISGameplayInterface::Execute_Interact(HitActor, MyPawn); DrawDebugSphere(GetWorld(), HitActor->GetActorLocation(), 30.f, 32, LineColor, false, 2.0f); break; } } DrawDebugSphere(GetWorld(), HitActor->GetActorLocation(), 30.f, 32, LineColor, false, 2.0f); } DrawDebugLine(GetWorld(), EyeLocation, End, LineColor, false, 2.0f, 0, 2.0f); #endif }
|