Sphere Trace returns hit furthest from trace, need closest hit.

Hello,

I have a problem with the hit output from a sphere trace, I need to get the closest block from the player but the recorded hit is always the furthest one. Please see code and image result.

code: (actual trace is )


FHitResult LHitOut(ForceInit);
UKismetSystemLibrary::SphereTraceSingle(this, LStart, LEnd, 20.0f, UEngineTypes::ConvertToTraceType(ECC_GameTraceChannel2), true, toignore, EDrawDebugTrace::ForOneFrame, LHitOut, true);

full rutine:

void AGMaxCharacter::SeekLedge()
{
ACharacter* myCharacter = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);

//WALL AND LEDGE TRACE PARAMS
const FVector WStart = myCharacter->GetActorLocation();
FRotator FR = myCharacter->GetActorRotation();
FVector FV = UKismetMathLibrary::GetForwardVector(FR);
FVector WL = FV * 70.0f;
const  FVector WEnd = FVector(FV.X * 150.0f, FV.Y * 150.0f, FV.Z) + WStart;

const FVector LStart = FVector(WL.X, WL.Y, WL.Z) + FVector(WStart.X, WStart.Y, WStart.Z + 500.0f);
const  FVector LEnd = FVector(LStart.X, LStart.Y, (LStart.Z - 500.0f));

//Trace for Wall
FHitResult WHitOut(ForceInit);
TArray<AActor*> toignore;
UKismetSystemLibrary::SphereTraceSingle(this, WStart, WEnd, 20.0f, UEngineTypes::ConvertToTraceType(ECC_GameTraceChannel2), false, toignore, EDrawDebugTrace::ForOneFrame, WHitOut, true);

// Trace for Ledge
FHitResult LHitOut(ForceInit);
UKismetSystemLibrary::SphereTraceSingle(this, LStart, LEnd, 20.0f, UEngineTypes::ConvertToTraceType(ECC_GameTraceChannel2),false, toignore, EDrawDebugTrace::ForOneFrame, LHitOut, true);


 WallLocation = WHitOut.Location;
 WallNormal = WHitOut.Normal;
 LedgeLocation = LHitOut.Location;
FVector SocketLocation = GetMesh()->GetSocketLocation("PelvisSocket");
LocalDistanceToLedge = (SocketLocation.Z - LedgeLocation.Z);

if (((SocketLocation.Z - LedgeLocation.Z) > -71.0f && (SocketLocation.Z - LedgeLocation.Z) < 1.0f) && LocalIsClimbingLedge == false)
{
    LocalCanGrabLedge = true;
    GrabLedge();
}else{
    LocalCanGrabLedge = false;
}

}

Results:

Any help is greatly appreciated!!

Thanks!

This is kind of expected behavior, the single trace for volumes will result you the far object that being hit.
WTF Is? Box Trace For Objects in Unreal Engine 4 ( UE4 ) - YouTube It’s been demonstrated here, for reference.

You can experiment with the multi trace version of the similar method that will give you all hits, which you can use to select the desired hit information (eg closest to your pawn).

can you invert the start and end points of trace

Apparently you are right, changing the order of the inputs will indeed flip the direction of the results, althought i’d still recommend OP to go with multi traces, so it makes easier to choose the most favored result between hits. For instance, it would be counter productive to get a hit beneath the block that hits the ground, since it is likely the character wants to grab the first elevated point which the single trace will never been able to request. It only will be available in the set of the multiple hit results, and by measuring distances (and normalized directions maybe) it is possible to choose the first ledge to grab onto.