World.h 库里的 Trace API

  • Trace模式

    • TraceSingle 单个结果
    • TraceMulti 多个结果
  • Trace 的检测依据
    • ByChanne
    • ByObjectType
    • ByProfile

LineTraceSingleByChannel

由于一般不使用 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());
    }

UKismetSystemLibrary 库里的 Trace API

  • 与蓝图使用的 Trace 节点对应,

  • 实际调用的是 World.h 里的 Trace 函数

LineTrace

LineTraceSingle

根据 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 说明

    • 默认 TraceTypeQuery1 —— Visibility
    • 默认 TraceTypeQuery2 —— Camera
    • 可在 ProjectSettings->Engine->Collision->Trace Channels 添加自定义
  • 代码实现

    // 起始点和检测结果
    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());
    }

LineTraceSingleForObjects

根据 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

    • 默认 ObjectTypeQuery1 —— WorldStatic
    • 默认 ObjectTypeQuery2 —— WorldDynamic
    • 默认 ObjectTypeQuery3 —— Oawn
    • 默认 ObjectTypeQuery4 —— PhysicasBody
    • 默认 ObjectTypeQuery5 —— Vehicle
    • 默认 ObjectTypeQuery6 —— Destructible
    • 可以再 ProjectSettings->Engine->Collision->Object Channels 添加自定义
  • 代码实现

    // 设置要检测的 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());
    }

LineTraceSingleByProfile

根据 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());
    }

LineTraceMulti

根据 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
    );

LineTraceMultiForObjects

根据 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
    );

LineTraceMultiByProfile

根据 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
    );

BoxTrace

  • 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
    )

SphereTrace

  • 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
    )

CapsuleTrace

  • 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
    )

参考

【UE4 C++】 射线检测 LineTrace 及 BoxTrace、SphereTrace、CapsuleTrace API的更多相关文章

  1. 【Unity3D】枪战游戏—发射子弹、射线检测

    一.子弹的碰撞检测: 因为子弹的移动速度非常的快,那么如果为子弹添加一个collider,就有可能检测不到了. 因为collider是每一帧在执行,第一帧子弹可能在100米处,那么下一帧就在900米处 ...

  2. Unity3D 之射线检测

    这里来记录下射线检测的相关内容: 射线检测故名就是通过射线去检测是否和碰撞器产生了交集,和碰撞器与碰撞器发生交集一样,会返回一个真. 射线的用法很多:比如检测是否跳跃,通过向地面投射射线控制在地面时候 ...

  3. Unity3D 5.0简单的射线检测实现跳跃功能

    这里是一个简单的跳跃,5.0和其他版本貌似不一样,并且,再起跳功能做的不完全. 不过一个基本的思路在这里. 1.首先,射线检测,这里是利用一个空对象,放到主角对象的下面 2.然后调节射线的位置,在主角 ...

  4. UGUI射线检测

    1.Graphic Raycaster 主要用于UI上的射线检测,挂有这个组件的物体,必须要挂上Canvas这个组件(当挂上Graphic Raycaster时Canvas也会自动挂上). Ignor ...

  5. Unity射线检测的用法总结

    RayCast 射线检测 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享.心 ...

  6. U3D学习10——关节和射线检测

    1.弹簧  2.铰链  3.固定关节  4.角色关节  5.自定义关节  6.raycast和raycasthit 射线有位移参数,可以设定只触发某一层的. 7.射线检测用于高速和精确 update是 ...

  7. Layabox 3D游戏开发学习笔记---射线检测,鼠标控制物体运动

    核心要点:3D物体碰撞是靠射线检测,射线与碰撞器相撞获取对应的碰撞点信息. class RayPicking03 { private ray: Laya.Ray; private point: Lay ...

  8. 【转】 [UnityUI]UGUI射线检测

    http://blog.csdn.net/lyh916/article/details/50947026 1.Graphic Raycaster 主要用于UI上的射线检测,挂有这个组件的物体,必须要挂 ...

  9. Unity - Raycast 射线检测

    本文简要分析了Unity中射线检测的基本原理及用法,包括: Ray 射线 RaycastHit 光线投射碰撞信息 Raycast 光线投射 SphereCast 球体投射 OverlapSphere ...

随机推荐

  1. 一、Rabbitmq的简单介绍

    以下只是本人从零学习过程的整理 部分内容参考地址:https://www.cnblogs.com/ysocean/p/9240877.html 1.RabbitMQ的概念 RabbitMQ是实现了高级 ...

  2. Kubernetes-Pod介绍(-)

    前言 本篇是Kubernetes第四篇,大家一定要把环境搭建起来,看是解决不了问题的,必须实战.从现在开始都是重要的核心概念,此篇偏一些Pod的概念介绍,后续每篇都会有实战. Kubernetes系列 ...

  3. Django——ORM打印SQL

    如果想打印ORM转换过程中的SQL,需要在settings.py中进行如下配置: LOGGING = { 'version': 1, 'disable_existing_loggers': False ...

  4. C# 爬虫框架实现 概述

    目录: C# 爬虫框架实现 概述 C# 爬虫框架实现 流程_爬虫结构/原理 C# 爬虫框架实现 流程_各个类开发 C# 爬虫框架实现 流程_遇到的问题 C# 爬虫框架实现 后记 C#爬虫框架实现 源代 ...

  5. adb 常用命令大全(6)- 模拟按键输入

    语法格式 input [<source>] <command> [<arg>...] 物理键 # 电源键 adb shell input keyevent 26 # ...

  6. golang 判断平台是32位还是64位

    在strconv包中有个常量 const intSize  =  32  <<  (  ^uint(0)  >>  63  ) const IntSize = intSize ...

  7. Docker 安装 MySQL5.6

    方法一.docker pull mysql查找Docker Hub上的mysql镜像 #docker search mysql 这里我们拉取官方的镜像,标签为5.6 #docker pull mysq ...

  8. 搭建GIT仓库

  9. Java大数操作

    Java的Math包中提供了两个类用于对大数进行操作: BigInteger类,用于大整数的操作 BigDecimal类,用于大的小数操作 BigInteger类 Java中的基本类型中,表示整数的有 ...

  10. 概述 .NET 6 ThreadPool 实现

    目录 前言 任务的调度 基本调度单元 IThreadPoolWorkItem 实现类的实例. Task 全局队列 本地队列 偷窃机制 Worker Thread 的生命周期管理 线程生命注入实验 .N ...