UE 使用射线实现选择功能

分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();
    ...
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值