I am using C++ to fire a ray downward from a Pawn to hit a cube underneath it. Both HitResult.HitItem
and HitResult.FaceIndex
return -1
.
Can someone explain why these values are -1
? I think there is something fundamental I do not understand about LineTraceSingleByChannel
or about the cube properties and would welcome expert input. I have provided code and a screenshot below which shows the values in the log.
bool AShip::OrientOnSurface(float DeltaSeconds)
{
const int TraceStartFactor = 1;
const int TraceLength = 1000;
const float HoverHeight = 150.0f;
FQuat ActorQuat = GetActorQuat();
FVector ActorDownVector = ActorQuat.RotateVector(FVector(0, 0, -1));
FVector ActorUpVector = ActorQuat.RotateVector(FVector(0, 0, 1));
FVector ActorRightVector = ActorQuat.RotateVector(FVector(0, 1, 0));
FVector ActorForwardVector = ActorQuat.RotateVector(FVector(1, 0, 0));
FVector ActorLocation = GetActorLocation();
FVector Start = ActorLocation + ActorDownVector * TraceStartFactor;
FVector End = ActorLocation + ActorDownVector * TraceLength;
FCollisionQueryParams QueryParams;
QueryParams.AddIgnoredActor(this);
QueryParams.bReturnFaceIndex = true;
FCollisionResponseParams ResponseParams;
FHitResult HitResult;
GetWorld()->LineTraceSingleByChannel
(
HitResult,
Start,
End,
ECC_Visibility,
QueryParams,
ResponseParams
);
// If ray cast downward hits a surface
if (HitResult.bBlockingHit)
{
FVector ImpactNormal = HitResult.ImpactNormal;
FVector ImpactPoint = HitResult.ImpactPoint;
// Get component that was hit
UPrimitiveComponent* HitComponent = HitResult.GetComponent();
int32 HitItem = HitResult.Item;
AActor* HitActor = HitResult.GetActor();
// See if a static mesh and/or a spline mesh component were hit
UStaticMeshComponent* StaticMeshComp = Cast<UStaticMeshComponent>(HitResult.GetComponent());
USplineMeshComponent* SplineMeshComp = Cast<USplineMeshComponent>(HitResult.GetComponent());
// If hit a spline mesh component then make sure that it's also a static mesh component
if (!StaticMeshComp)
return false;
// Get static mesh
UStaticMesh* StaticMesh = StaticMeshComp->GetStaticMesh();
// Return if the static mesh isn't set (should not happen)
if (!StaticMesh)
return false;
// Flag neede for cooked builds or will crash when accessing mesh data
if (!StaticMesh->bAllowCPUAccess)
return false;
// Return if RenderData is invalid
if (!StaticMesh->GetRenderData())
return false;
// No valid mesh data on lod 0 (shouldn't happen)
if (!StaticMesh->GetRenderData()->LODResources.IsValidIndex(0))
return false;
int FaceIndex = HitResult.FaceIndex;
FTransform ComponentTransform = StaticMeshComp->GetComponentTransform();
FStaticMeshVertexBuffers* VertexBuffers = &StaticMesh->GetRenderData()->LODResources[0].VertexBuffers;
FStaticMeshVertexBuffer* StaticMeshVertexBuffer = &VertexBuffers->StaticMeshVertexBuffer;
FPositionVertexBuffer* PositionVertexBuffer = &VertexBuffers->PositionVertexBuffer;
FIndexArrayView IndexBuffer = StaticMesh->GetRenderData()->LODResources[0].IndexBuffer.GetArrayView();
// Why do both values equal -1?
UE_LOG(LogClass, Log, TEXT("HitItem: %d"), HitItem);
UE_LOG(LogClass, Log, TEXT("IndexBuffer: %d"), FaceIndex);
}
return true;
}