Trace模式
Trace 的检测依据
由于一般不使用 World 里的Trace API,故本小结只举 LineTraceSingleByChannel 一个例子
Syntax
bool LineTraceSingleByChannel(
struct FHitResult& OutHit,
const FVector& Start,
const FVector& End,
ECollisionChannel TraceChannel,
const FCollisionQueryParams& Params = FCollisionQueryParams::DefaultQueryParam,
const FCollisionResponseParams& ResponseParam = FCollisionResponseParams::DefaultResponseParam
) const;
代码实现
// 碰撞参数
FCollisionQueryParams CollisonQueryParams(TEXT("QueryParams"),true,NULL);
CollisonQueryParams.bTraceComplex = true;
CollisonQueryParams.bReturnPhysicalMaterial = false;
CollisonQueryParams.AddIgnoredActor(this);
// 起始点和检测结果
FVector BeginLoc = GetActorLocation();
FVector EndLoc = BeginLoc + GetActorForwardVector() * 1000;
FHitResult HitResult;
// 射线检测
GetWorld()->LineTraceSingleByChannel(HitResult, BeginLoc, EndLoc, ECollisionChannel::ECC_Visibility, CollisonQueryParams);
// 绘制射线
DrawDebugLine(GetWorld(), BeginLoc, HitResult.GetActor() ? HitResult.Location : EndLoc, FColor::Red, false, 1.0f);
if (HitResult.GetActor())
{
UKismetSystemLibrary::PrintString(GetWorld(), HitResult.GetActor()->GetName());
}
与蓝图使用的 Trace 节点对应,
实际调用的是 World.h 里的 Trace 函数
根据 Channel 检测单个物体
Syntax
static bool LineTraceSingle(
const UObject* WorldContextObject,
const FVector Start,
const FVector End,
ETraceTypeQuery TraceChannel,
bool bTraceComplex,
const TArray<AActor*>& ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
FHitResult& OutHit,
bool bIgnoreSelf,
FLinearColor TraceColor = FLinearColor::Red,
FLinearColor TraceHitColor = FLinearColor::Green,
float DrawTime = 5.0f
);
ETraceTypeQuery 说明
代码实现
// 起始点和检测结果
FVector BeginLoc = GetActorLocation();
FVector EndLoc = BeginLoc + GetActorForwardVector() * 1000;
FHitResult HitResult;
// 要忽略的物体
TArray<AActor*> IgnoreActors; // 射线检测
bool bIsHit = UKismetSystemLibrary::LineTraceSingle(GetWorld(), BeginLoc, EndLoc, TraceTypeQuery1, false, IgnoreActors, EDrawDebugTrace::ForDuration, HitResult, true);
if (bIsHit)
{
UKismetSystemLibrary::PrintString(GetWorld(), HitResult.GetActor()->GetName());
}
根据 Object Type 检测单个物体
Syntax
static bool LineTraceSingleForObjects(
const UObject* WorldContextObject,
const FVector Start,
const FVector End,
const TArray<TEnumAsByte<EObjectTypeQuery> > & ObjectTypes,
bool bTraceComplex,
const TArray<AActor*>& ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
FHitResult& OutHit,
bool bIgnoreSelf,
FLinearColor TraceColor = FLinearColor::Red,
FLinearColor TraceHitColor = FLinearColor::Green,
float DrawTime = 5.0f
);
EObjectTypeQuery 对应 ObjectType
代码实现
// 设置要检测的 Object Type
TArray<TEnumAsByte<EObjectTypeQuery> > ObjectTypes;
ObjectTypes.Add(EObjectTypeQuery::ObjectTypeQuery1);
//开始检测
bool bIsHit = UKismetSystemLibrary::LineTraceSingleForObjects(GetWorld(), BeginLoc, EndLoc, ObjectTypes, false, IgnoreActors, EDrawDebugTrace::ForDuration, HitResult, true);
if (bIsHit)
{
UKismetSystemLibrary::PrintString(GetWorld(), HitResult.GetActor()->GetName());
}
根据 Collision Preset 检测单个物体
Syntax
static bool LineTraceSingleByProfile(
const UObject* WorldContextObject,
const FVector Start,
const FVector End,
FName ProfileName,
bool bTraceComplex,
const TArray<AActor*>& ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
FHitResult& OutHit,
bool bIgnoreSelf,
FLinearColor TraceColor = FLinearColor::Red,
FLinearColor TraceHitColor = FLinearColor::Green,
float DrawTime = 5.0f
);
ProfileName 对应 Collision Preset 的名称
代码实现
bool bIsHit = UKismetSystemLibrary::LineTraceSingleByProfile(GetWorld(), BeginLoc, EndLoc,TEXT("BlockAll"), false, IgnoreActors, EDrawDebugTrace::ForDuration, HitResult, true);
if (bIsHit)
{
UKismetSystemLibrary::PrintString(GetWorld(), HitResult.GetActor()->GetName());
}
根据 Channel 检测多个物体,输出 TArray&
Syntax
static bool LineTraceMulti(
const UObject* WorldContextObject,
const FVector Start,
const FVector End,
ETraceTypeQuery TraceChannel,
bool bTraceComplex,
const TArray<AActor*>& ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
TArray<FHitResult>& OutHits,
bool bIgnoreSelf,
FLinearColor TraceColor = FLinearColor::Red,
FLinearColor TraceHitColor = FLinearColor::Green,
float DrawTime = 5.0f
);
根据 Object Type 检测多个物体,输出 TArray&
Syntax
static bool LineTraceMultiForObjects(
const UObject* WorldContextObject,
const FVector Start,
const FVector End,
const TArray<TEnumAsByte<EObjectTypeQuery> > & ObjectTypes,
bool bTraceComplex,
const TArray<AActor*>& ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
TArray<FHitResult>& OutHits,
bool bIgnoreSelf, FLinearColor TraceColor = FLinearColor::Red,
FLinearColor TraceHitColor = FLinearColor::Green,
float DrawTime = 5.0f
);
根据 Collision Preset 检测多个物体,输出 TArray&
Syntax
static bool LineTraceMultiByProfile
(
const UObject* WorldContextObject,
const FVector Start,
const FVector End,
FName ProfileName,
bool bTraceComplex,
const TArray<AActor*>& ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
TArray<FHitResult>& OutHits,
bool bIgnoreSelf,
FLinearColor TraceColor = FLinearColor::Red,
FLinearColor TraceHitColor = FLinearColor::Green,
float DrawTime = 5.0f
);
BoxTraceSingle
static bool BoxTraceSingle
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
const FVector HalfSize,
const FRotator Orientation,
ETraceTypeQuery TraceChannel,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
FHitResult & OutHit,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
BoxTraceMulti
static bool BoxTraceMulti
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
FVector HalfSize,
const FRotator Orientation,
ETraceTypeQuery TraceChannel,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
TArray < FHitResult > & OutHits,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
BoxTraceSingleForObjects
static bool BoxTraceSingleForObjects
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
const FVector HalfSize,
const FRotator Orientation,
const TArray < TEnumAsByte < EObjectTypeQuery > > & ObjectTypes,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
FHitResult & OutHit,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
BoxTraceMultiForObjects
static bool BoxTraceMultiForObjects
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
const FVector HalfSize,
const FRotator Orientation,
const TArray < TEnumAsByte < EObjectTypeQuery > > & ObjectTypes,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
TArray < FHitResult > & OutHits,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
BoxTraceSingleByProfile
static bool BoxTraceSingleByProfile
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
const FVector HalfSize,
const FRotator Orientation,
FName ProfileName,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
FHitResult & OutHit,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
BoxTraceMultiByProfile
static bool BoxTraceMultiByProfile
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
FVector HalfSize,
const FRotator Orientation,
FName ProfileName,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
TArray < FHitResult > & OutHits,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
SphereTraceSingle
static bool SphereTraceSingle
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
float Radius,
ETraceTypeQuery TraceChannel,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
FHitResult & OutHit,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
SphereTraceMulti
static bool SphereTraceMulti
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
float Radius,
ETraceTypeQuery TraceChannel,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
TArray < FHitResult > & OutHits,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
SphereTraceSingleForObjects
static bool SphereTraceSingleForObjects
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
float Radius,
const TArray < TEnumAsByte < EObjectTypeQuery > > & ObjectTypes,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
FHitResult & OutHit,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
SphereTraceMultiForObjects
static bool SphereTraceMultiForObjects
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
float Radius,
const TArray < TEnumAsByte < EObjectTypeQuery > > & ObjectTypes,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
TArray < FHitResult > & OutHits,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
SphereTraceSingleByProfile
static bool SphereTraceSingleByProfile
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
float Radius,
FName ProfileName,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
FHitResult & OutHit,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
SphereTraceMultiByProfile
static bool SphereTraceMultiByProfile
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
float Radius,
FName ProfileName,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
TArray < FHitResult > & OutHits,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
CapsuleTraceSingle
static bool CapsuleTraceSingle
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
float Radius,
float HalfHeight,
ETraceTypeQuery TraceChannel,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
FHitResult & OutHit,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
CapsuleTraceMulti
static bool CapsuleTraceMulti
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
float Radius,
float HalfHeight,
ETraceTypeQuery TraceChannel,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
TArray < FHitResult > & OutHits,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
CapsuleTraceSingleForObjects
static bool CapsuleTraceSingleForObjects
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
float Radius,
float HalfHeight,
const TArray < TEnumAsByte < EObjectTypeQuery > > & ObjectTypes,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
FHitResult & OutHit,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
CapsuleTraceMultiForObjects
static bool CapsuleTraceMultiForObjects
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
float Radius,
float HalfHeight,
const TArray < TEnumAsByte < EObjectTypeQuery > > & ObjectTypes,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
TArray < FHitResult > & OutHits,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
CapsuleTraceSingleByProfile
static bool CapsuleTraceSingleByProfile
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
float Radius,
float HalfHeight,
FName ProfileName,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
FHitResult & OutHit,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
CapsuleTraceMultiByProfile
static bool CapsuleTraceMultiByProfile
(
const UObject * WorldContextObject,
const FVector Start,
const FVector End,
float Radius,
float HalfHeight,
FName ProfileName,
bool bTraceComplex,
const TArray < AActor * > & ActorsToIgnore,
EDrawDebugTrace::Type DrawDebugType,
TArray < FHitResult > & OutHits,
bool bIgnoreSelf,
FLinearColor TraceColor,
FLinearColor TraceHitColor,
float DrawTime
)
手机扫一扫
移动阅读更方便
你可能感兴趣的文章