分Trace通道检测和Object Type物体类型检测两种。
-
Trace
-
1.1 UWord::LineTraceSingleByChannel——返回单个碰撞
-
bool LineTraceSingleByChannel( struct FHitResult& OutHit, const FVector& Start, const FVector& End, ECollisionChannel TraceChannel, const FCollisionQueryParams& Params = FCollisionQueryParams::DefaultQueryParam, const FCollisionResponseParams& ResponseParam = FCollisionResponseParams::DefaultResponseParam) const; /** * Trace a ray against the world using a specific channel and return the first blocking hit * @param OutHit First blocking hit found * @param Start Start location of the ray * @param End End location of the ray * @param TraceChannel The 'channel' that this ray is in, used to determine which components to hit * @param Params Additional parameters used for the trace * @param ResponseParam ResponseContainer to be used for this trace * @return TRUE if a blocking hit is found */
代码示例:
-
FVector MouseLineStart; //start 开始位置 FVector MouseLineDirection; //方向 //将当前鼠标 2D 位置转换为世界空间 3D 位置和方向。如果无法确定值,则返回 false。 MyController->DeprojectMousePositionToWorld(MouseLineStart, MouseLineDirection); FVector MouseLineEnd = MouseLineStart + (MouseLineDirection * 3000);//结束位置 FHitResult MouseHit; FCollisionQueryParams QParams; bool bMouseHit = GetWorld()->LineTraceSingleByChannel(MouseHit, MouseLineStart, MouseLineEnd, ECC_Visibility, QParams); if(bMouseHit) { return MouseHit.GetActor(); //返回鼠标点击的物体 }
若想要忽略某些物体类型,防止误选,可以函数Params参数中添加忽略响应的Actor数组,代码如下:
-
TArray<AActor*> IgnoreActors; //通过Tag标签找出所有忽略响应的Actor UGameplayStatics::GetAllActorsWithTag(this, IgnoreActors_Tag,IgnoreActors); QParams.AddIgnoredActors(IgnoreActors);
-
Object
-
2.1 Uword::LineTraceSingleByObjectType——返回单个指定Object Type碰撞
bool LineTraceSingleByObjectType(struct FHitResult& OutHit,const FVector& Start,const FVector& End,const FCollisionObjectQueryParams& ObjectQueryParams, const FCollisionQueryParams& Params = FCollisionQueryParams::DefaultQueryParam) const; /** * Trace a ray against the world using object types and return the first blocking hit * @param OutHit First blocking hit found * @param Start Start location of the ray * @param End End location of the ray * @param ObjectQueryParams List of object types it's looking for * @param Params Additional parameters used for the trace * @return TRUE if any hit is found */
(代码示例待补充,等用到了再加上)
-
2.2 UKismetSystemLibrary::LineTraceMultiForObjects——返回该路径上所有指定Object Type碰撞
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);
/**
* Does a collision trace along the given line and returns all hits encountered.
* This only finds objects that are of a type specified by ObjectTypes.
*
* @param WorldContext World context
* @param Start Start of line segment.
* @param End End of line segment.
* @param ObjectTypes Array of Object Types to trace
* @param bTraceComplex True to test against complex collision, false to test against simplified collision.
* @param OutHit Properties of the trace hit.
* @return True if there was a hit, false otherwise.
*/
代码示例:
FVector StartLocation=... //开始位置
FVector EndLocation=... //结束位置
TArray<TEnumAsByte<EObjectTypeQuery>> ObjectTypes;
ObjectTypes.Emplace(EObjectTypeQuery::ObjectTypeQuery1); // WorldStatic
ObjectTypes.Emplace(EObjectTypeQuery::ObjectTypeQuery2); // WorldDynamic
ObjectTypes.Emplace(EObjectTypeQuery::ObjectTypeQuery3); // Pawn
ObjectTypes.Emplace(EObjectTypeQuery::ObjectTypeQuery4); // PhysicsBody
ObjectTypes.Emplace(EObjectTypeQuery::ObjectTypeQuery5); // Vehicle
ObjectTypes.Emplace(EObjectTypeQuery::ObjectTypeQuery6); // Destructible
TArray<AActor*> IgnoreActors=...
TEnumAsByte<EDrawDebugTrace::Type> DrawDebugTrace = EDrawDebugTrace::Type::ForDuration;
TArray<FHitResult> TraceOutHits=...
FLinearColor TraceColor = FLinearColor::Red;
FLinearColor TraceHitColor = FLinearColor::Green;
float TraceDrawTime = 3.0f;
UKismetSystemLibrary::LineTraceMultiForObjects(
this,
StartLocation,
EndLocation,
ObjectTypes,
false,
IgnoreActors, //忽略Actors数组
DrawDebugTrace,
TraceOutHits,
true,
TraceColor,
TraceHitColor,
TraceDrawTime);
if (TraceOutHits.Num() > 0)
{
for(FHitResult result : TraceOutHits)
{
AActor*ReAcot = result.GetActor();
...
}
}