如何配置AI的视觉和听觉感知

解读

国内 UE4 面试中,这道题考察的是“AI 感知系统(AI Perception System)”的落地能力。面试官想确认三件事:

  1. 你是否真的用过引擎内置的 AIPerceptionComponent,而不是自己写一套;
  2. 能否把视觉与听觉两种刺激源(Stimulus)同时跑通,并正确区分;
  3. 能否把感知结果高效同步到行为树(BT)或 EQS,做出“看得见、听得见、记得住”的 AI。

回答时切忌只讲蓝图节点,必须体现“C++ 可扩展 + 蓝图可调试”的双轨思路,并给出性能与同步的权衡。

知识点

  1. AIPerceptionComponent:官方感知入口,支持 Sight、Hearing、Damage、Touch、Team、Prediction 六类刺激。
  2. AIStimulus:一次刺激的数据包,包含位置、年龄、强度、Tag、宿主对象。
  3. SenseConfig:在组件里为每种感官新建配置对象(UAISenseConfig_Sight / Hearing),决定半径、角度、监听通道、痕迹保留时长。
  4. PerceptionListener:AI 本身作为 Listener,每帧收集刺激;刺激源(AActor)需挂载 AIPerceptionStimuliSourceComponent 并注册对应 Sense。
  5. OnPerceptionUpdated 与 OnTargetPerceptionForgotten:蓝图事件与 C++ Delegate,用于把结果写进黑板或自定义记忆结构。
  6. 团队感知:通过 GenericTeamAgentInterface 实现 AITeam,解决“同阵营不报警”需求。
  7. 网络同步:感知结果只运行在服务器,AI 行为 replicate 到客户端;若做 ListenServer,需把 Stimulus Location 设为可复制变量,防止客户端透视。
  8. 性能优化:LOD 系统里按距离降低 Sense 的 TickInterval;用 EQS 的 SkipUnreachable 减少射线检测;对大量 AI 使用 SharedPerception 模式。

答案

我分四步落地视觉与听觉感知,全部在引擎原生框架内完成,C++ 做底层、蓝图做调试,兼顾策划可调。

第一步:建立 AI 角色

  1. 新建 AAICharacter 派生类,添加 UAIPerceptionComponent,命名为 PerComp。

  2. 在构造函数里创建并绑定两个配置对象:
    UAISenseConfig_Sight* SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("SightConfig"));
    SightConfig->SightRadius = 3000.f;
    SightConfig->LoseSightRadius = 3500.f;
    SightConfig->PeripheralVisionAngleDegrees = 90.f;
    SightConfig->DetectionByAffiliation.bDetectEnemies = true;
    SightConfig->DetectionByAffiliation.bDetectNeutrals = true;
    SightConfig->SetMaxAge(5.f); // 视觉记忆 5 秒

    UAISenseConfig_Hearing* HearConfig = CreateDefaultSubobject<UAISenseConfig_Hearing>(TEXT("HearConfig"));
    HearConfig->HearingRange = 2000.f;
    HearConfig->SetMaxAge(3.f);
    HearConfig->DetectionByAffiliation = SightConfig->DetectionByAffiliation; // 复用阵营规则

    PerComp->ConfigureSense(*SightConfig);
    PerComp->ConfigureSense(*HearConfig);
    PerComp->SetDominantSense(*SightConfig); // 视觉优先
    PerComp->OnPerceptionUpdated.AddDynamic(this, &AMyAICharacter::OnPerceptionUpdate);

第二步:注册刺激源
对玩家或任何可能发出声音的 Actor,在 BeginPlay 里:
UAIPerceptionSystem::RegisterPerceptionStimuliSource(this, UAISense_Sight::StaticClass(), this);
UAIPerceptionSystem::RegisterPerceptionStimuliSource(this, UAISense_Hearing::StaticClass(), this);

若要在蓝图中“手动”发出脚步声,调用:
UGameplayStatics::PlaySoundAtLocation 之后,立即执行:
UAISense_Hearing::ReportNoiseEvent(GetWorld(), Location, 1.f, Instigator, 0.f, FName("FootStep"));
其中 1.f 是 Loudness,可在 AI 侧用 HearConfig->HearingRange * Loudness 做距离衰减。

第三步:处理感知结果
在 C++ 的 OnPerceptionUpdate(TArray<AActor*> UpdatedActors) 里:
for (AActor* Actor : UpdatedActors)
{
FActorPerceptionBlueprintInfo Info;
PerComp->GetActorsPerception(Actor, Info);
for (const FAIStimulus& Stim : Info.LastSensedStimuli)
{
if (Stim.Type == UAISense_Sight::StaticClass())
Blackboard->SetValueAsObject(TargetKey, Actor);
else if (Stim.Type == UAISense_Hearing::StaticClass())
Blackboard->SetValueAsVector(NoiseKey, Stim.StimulusLocation);
}
}
若 Stim.WasSuccessfullySensed() == false,说明已丢失,可调用 Blackboard->ClearValue。

第四步:行为树消费
行为树里用 Blackboard 的 TargetKey 做 MoveTo,NoiseKey 做 MoveTo 并设置 Investigate 任务;
EQS 用 Context_NoiseLocation 做 Query,确保 AI 能跑到“最后听到声音的位置”而不是玩家实时坐标。
服务器端运行整套逻辑,客户端仅复制 AI 的 Pawn 移动与 Montage,避免透视作弊。

性能与同步:

  • 把 PerComp 的 TickInterval 与距离挂钩,30 m 内每 0.2 s,30-60 m 每 0.5 s,60 m 外暂停。
  • 对同屏 >50 个 AI 的关卡,启用 AAIController::SetSharedPerceptionFilter(true),让同一小队共享感知结果,减少冗余检测。
  • 若项目为 ListenServer,把 NoiseLocation 设为 RepNotify 变量,客户端收到后再本地播放疑问语音,保证表现统一。

拓展思考

  1. 记忆系统:官方 MaxAge 只能做“时间遗忘”,如需“位置遗忘”或“视线遮挡后仍记得 3 秒”,可在 AIController 里自建 TMap<AActor, FMyMemory>,在 OnPerceptionUpdate 里更新 LastSeenLocation、Timestamp,并在 Tick 中做射线遮挡检查,超时或遮挡累计 3 秒后清除。
  2. 感官冲突:当 AI 同时看到玩家又听到脚步声时,行为树可能来回切换目标。可在黑板加 EAIAlertLevel 枚举,视觉刺激直接设为 High,听觉设为 Medium,行为树用 Priority Selector 保证 High 级任务打断 Medium。
  3. 异步查询:对开放世界,可用 EQS 的 Async 模式,把感知结果作为 Query 的 Context,避免主线程卡顿;同时结合 HLOD 系统,在远距离把 AI 降频到 1 Hz,近距恢复 10 Hz。
  4. 机器学习扩展:若项目使用 ML 决策,可把感知结果(Stimulus Location、Age、Type)作为 Observation 输入到 ONNX Runtime 插件,让 AI 自己判断“追、包抄、报警”策略,而不仅靠行为树硬编码。