Skip to content

Instantly share code, notes, and snippets.

@JoshLmao
Created April 17, 2020 09:22
Show Gist options
  • Save JoshLmao/723bde5dd4b8c650c116e0d2ada66cce to your computer and use it in GitHub Desktop.
Save JoshLmao/723bde5dd4b8c650c116e0d2ada66cce to your computer and use it in GitHub Desktop.
Little guide on how to use UE4's UKismetSystemLibrary::SphereOverlapActors
#include "Kismet\KismetSystemLibrary.h"
// Set what actors to seek out from it's collision channel
TArray<TEnumAsByte<EObjectTypeQuery>> traceObjectTypes;
traceObjectTypes.Add(UEngineTypes::ConvertToObjectType(ECollisionChannel::ECC_Pawn));
// Ignore any specific actors
TArray<AActor*> ignoreActors;
// Ignore self or remove this line to not ignore any
ignoreActors.Init(this, 1);
// Array of actors that are inside the radius of the sphere
TArray<AActor*> outActors;
// Total radius of the sphere
float radius = 750.0f;
// Sphere's spawn loccation within the world
FVector sphereSpawnLocation = GetActorLocation();
// Class that the sphere should hit against and include in the outActors array (Can be null)
UClass* seekClass = AEnemyBase::StaticClass(); // NULL;
UKismetSystemLibrary::SphereOverlapActors(GetWorld(), sphereSpawnLocation, radius, traceObjectTypes, seekClass, ignoreActors, outActors);
// Optional: Use to have a visual representation of the SphereOverlapActors
// DrawDebugSphere(GetWorld(), GetActorLocation(), radius, 12, FColor::Red, true, 10.0f);
// Finally iterate over the outActor array
for (AActor* overlappedActor : outActors) {
UE_LOG(LogTemp, Log, TEXT("OverlappedActor: %s"), *overlappedActor->GetName());
}
@invernomuto1976
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment