UGameplayStatics::FindNearestActor
#include "Kismet/GameplayStatics.h"
Access: public
Specifiers: staticBlueprintPure
Description
Returns the actor from ActorsToCheck whose GetActorLocation() is closest to Origin, and outputs the distance to it.
Caveats & Gotchas
- • Returns nullptr and sets Distance to 0 when ActorsToCheck is empty — always null-check the result before using it.
- • Distances are compared using squared distance internally for performance, but the output Distance is the actual Euclidean distance (not squared). Do not compare it directly against a squared threshold.
- • Uses actor pivot location (GetActorLocation), not the closest point on the actor's bounds; a large actor with an off-centre pivot may not be the visually nearest one.
Signature
static ENGINE_API AActor* FindNearestActor(FVector Origin, const TArray<AActor*>& ActorsToCheck, float& Distance); Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| Origin | FVector | World-space location from which distances are measured. | — |
| ActorsToCheck | const TArray<AActor*>& | Candidate actors to check. Null entries in the array are skipped. | — |
| Distance | float& | Output: the straight-line distance from Origin to the returned actor's location. | — |
Return Type
AActor* Example
Pick up the nearest item within grab range C++
TArray<AActor*> Items;
UGameplayStatics::GetAllActorsOfClass(this, APickupItem::StaticClass(), Items);
float Distance;
AActor* Nearest = UGameplayStatics::FindNearestActor(GetActorLocation(), Items, Distance);
if (Nearest && Distance <= GrabRadius)
{
Cast<APickupItem>(Nearest)->Pickup(this);
} Tags
Version History
Introduced in: 4.15
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?