Setup Hearing Sense
回到FPSAIGuard.cpp文件
PawnSensingComp->OnHearNoise.AddDynamic(this,&AFPSAIGuard::OnNoiseHeard);
找到参数列表

复制参数并创建函数
UFUNCTION() void OnNoiseHeard(APawn* NoiseInstigator, const FVector& Location, float Volume);
- 标记为const,函数内容无法进行更改
- 使用矢量位置的引用,无需复制,即可向该函数传输原位置数据,避免复制大量的矢量数据
形参里的 Instigator 在 Actor 中已经使用过了

在函数中将其定义为参数报了错,修改形参名称即可
创建实现
void AFPSAIGuard::OnNoiseHeard(APawn* NoiseInstigator, const FVector& Location, float Volume) { DrawDebugSphere(GetWorld(), Location, 32.0f, 12, FColor::Green, false, 10.0f); }
FPSCharacter.h
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "AI") UPawnNoiseEmitterComponent* NoiseEmitterComponent;
添加前向声明
class UPawnNoiseEmitterComponent;
来到FPSCharacter.cpp
导入头文件
#include "Components/PawnNoiseEmitterComponent.h"
创建实例
NoiseEmitterComponent = CreateDefaultSubobject<UPawnNoiseEmitterComponent>(TEXT("NoiseEmitter"));
编辑BP_Player

运行游戏

当AI看见我们的时候,控制声明的组件就失效了,也不会触发声音事件了

浙公网安备 33010602011771号