碰撞会生成重叠事件,因此理论上可以使用OnComponentBeginOverlap
并获取SweepResult
以获得重叠事件。但是SweepResult
不太可靠,因此建议在重叠事件内进行球形扫描
。
void Pawn::OnComponentBeginOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor && (OtherActor != this))
{
TArray<FHitResult> Results;
auto ActorLoc = GetActorLocation();
auto OtherLoc = OtherComp->GetComponentLocation();
auto CollisionRadius = FVector::Dist(Start, End) * 1.2f;
// spherical sweep
GetWorld()->SweepMultiByObjectType(Results, ActorLoc, OtherLoc,
FQuat::Identity, 0,
FCollisionShape::MakeSphere(CollisionRadius),
// use custom params to reduce the search space
FCollisionQueryParams::FCollisionQueryParams(false)
);
for (auto HitResult : Results)
{
if (OtherComp->GetUniqueID() == HitResult.GetComponent()->GetUniqueID()) {
// insert your code
break;
}
}
}
}
你可以尝试使用FCollisionQueryParams来加快这个过程,但球形扫描会在几帧碰撞后被绘制,所以也许你可以暂停/停止演员以获得精确的结果。
UPrimitiveComponent::UpdateOverlapsImpl
和UPrimitiveComponent::ComponentOverlapMulti
。 - Rotem