-获取鼠标位置有很多方法,这里说三种,但只是提供方法,具体并不是很清楚
FHitResult TraceHitResult;//检测结果
GetHitResultUnderCursorByChannel(TraceTypeQuery1, true, TraceHitResult);
FVector pos = TraceHitResult.Location;//位置
bool APlayerController::GetHitResultUnderCursorByChannel(ETraceTypeQuery TraceChannel, bool bTraceComplex, FHitResult& HitResult) const
{
ULocalPlayer* LocalPlayer = Cast(Player);
bool bHit = false;
if (LocalPlayer && LocalPlayer->ViewportClient)
{
FVector2D MousePosition;
if (LocalPlayer->ViewportClient->GetMousePosition(MousePosition))
{
bHit = GetHitResultAtScreenPosition(MousePosition, TraceChannel, bTraceComplex, HitResult);
}
}
if(!bHit) //If there was no hit we reset the results. This is redundant but helps Blueprint users
{
HitResult = FHitResult();
}
return bHit;
}
我推测是根据追踪类型进行定位,不是很清楚
FHitResult bTraceHitResult;
GetHitResultUnderCursor(ECC_Visibility, true, bTraceHitResult);
FVector pos = bTraceHitResult.Location;//位置
bool APlayerController::GetHitResultUnderCursor(ECollisionChannel TraceChannel, bool bTraceComplex, FHitResult& HitResult) const
{
ULocalPlayer* LocalPlayer = Cast(Player);
bool bHit = false;
if (LocalPlayer && LocalPlayer->ViewportClient)
{
FVector2D MousePosition;
if (LocalPlayer->ViewportClient->GetMousePosition(MousePosition))
{
bHit = GetHitResultAtScreenPosition(MousePosition, TraceChannel, bTraceComplex, HitResult);
}
}
if(!bHit) //If there was no hit we reset the results. This is redundant but helps Blueprint users
{
HitResult = FHitResult();
}
return bHit;
}
和第一种很像,只有一个参数是变化的,推测是根据碰撞类型进行定位,因为在碰撞设置的时候也会设置对于Trace的反应。
FHitResult HitResult,Start, Dir, End, ;
DeprojectMousePositionToWorld(Start, Dir);//获取初始位置和方向
End = Start + (Dir*8000.0f);//设置追踪终点
GetWorld()->LineTraceSingleByChannel(HitResult, Start, End, ECC_Visibility);
FVector pos = HitResult.Location;//位置
bool APlayerController::DeprojectMousePositionToWorld(FVector& WorldLocation, FVector& WorldDirection) const
{
ULocalPlayer* const LocalPlayer = GetLocalPlayer();
if (LocalPlayer && LocalPlayer->ViewportClient)
{
FVector2D ScreenPosition;
if (LocalPlayer->ViewportClient->GetMousePosition(ScreenPosition))
{
return UGameplayStatics::DeprojectScreenToWorld(this, ScreenPosition, WorldLocation, WorldDirection);
}
}
return false;
}
//被调用的方法
bool UGameplayStatics::DeprojectScreenToWorld(APlayerController const* Player, const FVector2D& ScreenPosition, FVector& WorldPosition, FVector& WorldDirection)
{
ULocalPlayer* const LP = Player ? Player->GetLocalPlayer() : nullptr;
if (LP && LP->ViewportClient)
{
// get the projection data
FSceneViewProjectionData ProjectionData;
if (LP->GetProjectionData(LP->ViewportClient->Viewport, eSSP_FULL, /*out*/ ProjectionData))
{
FMatrix const InvViewProjMatrix = ProjectionData.ComputeViewProjectionMatrix().InverseFast();
FSceneView::DeprojectScreenToWorld(ScreenPosition, ProjectionData.GetConstrainedViewRect(), InvViewProjMatrix, /*out*/ WorldPosition, /*out*/ WorldDirection);
return true;
}
}
// something went wrong, zero things and return false
WorldPosition = FVector::ZeroVector;
WorldDirection = FVector::ZeroVector;
return false;
}