六一的部落格


关关难过关关过,前路漫漫亦灿灿。



AI Sense Damage


说明

当前, NPC只能感知处于视角180度范围内的敌人

添加伤害感知: 即对NPC造成伤害, 其能锁定伤害来源(身体转向伤害源)


为AIController添加伤害感知

BP_STUAIController

  1. 设置调试信息颜色
  2. 设置调试信息生存期

    Max Age



API导航


UAISense_Damage::ReportDamageEvent

上报伤害事件

1/** EventLocation will be reported as Instigator's location at the moment of event happening*/
2UFUNCTION(BlueprintCallable, Category = "AI|Perception", meta = (WorldContext="WorldContextObject", AdvancedDisplay="HitLocation"))
3static void ReportDamageEvent(UObject* WorldContextObject, AActor* DamagedActor, AActor* Instigator, float DamageAmount, FVector EventLocation, FVector HitLocation, FName Tag = NAME_None);

感知伤害源

  1. 获取感知对象

    如果视角没有敌人, 则在伤害源中寻找最近敌人

    ShootThemUp: Components/STUAIPerceptionComponent.cpp

     1// GetClosetEnemy
     2
     3if (!PercieveActors.Num())
     4{
     5    return GetDamageEnemy();GetCurrentlyPerceivedActors(UAISense_Damage::StaticClass(), PercieveActors);
     6    if (!PercieveActors.Num()) return nullptr;
     7
     8    UE_LOG(LogTemp, Display, TEXT("Actor name: %s"), *PercieveActors[0]->GetName());
     9}
    10
    11// ...
    12
    13if (BestPawn)
    14{
    15    UE_LOG(LogTemp, Display, TEXT("Actor name: %s"), *BestPawn->GetName());
    16}

    当前只有敌人可以造成伤害

  2. 受到伤害时上报伤害事件

    private

    ShootThemUp: Components/STUHealthComponent.h

    1void ReportDamageEvent(float Damage, AController *InstigatedBy);

    ShootThemUp: Components/STUHealthComponent.cpp

    1// ApplyDamage
    2ReportDamageEvent(Damage, InstigatedBy);

    ShootThemUp: Components/STUHealthComponent.cpp

     1#include "Perception/AISense_Damage.h"
     2
     3void USTUHealthComponent::ReportDamageEvent(float Damage, AController *InstigatedBy)
     4{
     5    if (!InstigatedBy ||            //
     6        !InstigatedBy->GetPawn() || //
     7        !GetOwner())
     8        return;
     9
    10    UAISense_Damage::ReportDamageEvent(GetWorld(), GetOwner(), InstigatedBy->GetPawn(), Damage, InstigatedBy->GetPawn()->GetActorLocation(),
    11                                       GetOwner()->GetActorLocation());
    12}

查看

  1. 设置NPC行为树

    为Selector添加Find Enemy服务


  2. 调试信息: 可见粉色调试球, 以及伤害源名称

    按下 ' 打开调试界面, 按下小写键盘上的 4 显示感知信息



附: NPC感知声音

  1. 为AIController添加声音感知

    BP_STUAIController


  2. 上报声音事件

    • 头文件

      1#include "Perception/AISense_Hearing.h"
    • 接口

       1/**
       2​ * Report a noise event.
       3​ * 
       4​ * @param NoiseLocation Location of the noise.
       5​ * @param Loudness Loudness of the noise. If MaxRange is non-zero, modifies MaxRange, otherwise modifies the squared distance of the sensor's range.
       6​ * @param Instigator Actor that triggered the noise.
       7​ * @param MaxRange Max range at which the sound can be heard, multiplied by Loudness. Values <= 0 mean no limit (still limited by listener's range however).
       8​ * @param Tag Identifier for the event.
       9 */
      10UFUNCTION(BlueprintCallable, Category = "AI|Perception", meta = (WorldContext="WorldContextObject"))
      11static void ReportNoiseEvent(UObject* WorldContextObject, FVector NoiseLocation, float Loudness = 1.f, AActor* Instigator = nullptr, float MaxRange = 0.f, FName Tag = NAME_None);

NPC感知伤害


AI Sense Damage


说明

当前, NPC只能感知处于视角180度范围内的敌人

添加伤害感知: 即对NPC造成伤害, 其能锁定伤害来源(身体转向伤害源)


为AIController添加伤害感知

BP_STUAIController

  1. 设置调试信息颜色
  2. 设置调试信息生存期

    Max Age



API导航


UAISense_Damage::ReportDamageEvent

上报伤害事件

1/** EventLocation will be reported as Instigator's location at the moment of event happening*/
2UFUNCTION(BlueprintCallable, Category = "AI|Perception", meta = (WorldContext="WorldContextObject", AdvancedDisplay="HitLocation"))
3static void ReportDamageEvent(UObject* WorldContextObject, AActor* DamagedActor, AActor* Instigator, float DamageAmount, FVector EventLocation, FVector HitLocation, FName Tag = NAME_None);

感知伤害源

  1. 获取感知对象

    如果视角没有敌人, 则在伤害源中寻找最近敌人

    ShootThemUp: Components/STUAIPerceptionComponent.cpp

     1// GetClosetEnemy
     2
     3if (!PercieveActors.Num())
     4{
     5    return GetDamageEnemy();GetCurrentlyPerceivedActors(UAISense_Damage::StaticClass(), PercieveActors);
     6    if (!PercieveActors.Num()) return nullptr;
     7
     8    UE_LOG(LogTemp, Display, TEXT("Actor name: %s"), *PercieveActors[0]->GetName());
     9}
    10
    11// ...
    12
    13if (BestPawn)
    14{
    15    UE_LOG(LogTemp, Display, TEXT("Actor name: %s"), *BestPawn->GetName());
    16}

    当前只有敌人可以造成伤害

  2. 受到伤害时上报伤害事件

    private

    ShootThemUp: Components/STUHealthComponent.h

    1void ReportDamageEvent(float Damage, AController *InstigatedBy);

    ShootThemUp: Components/STUHealthComponent.cpp

    1// ApplyDamage
    2ReportDamageEvent(Damage, InstigatedBy);

    ShootThemUp: Components/STUHealthComponent.cpp

     1#include "Perception/AISense_Damage.h"
     2
     3void USTUHealthComponent::ReportDamageEvent(float Damage, AController *InstigatedBy)
     4{
     5    if (!InstigatedBy ||            //
     6        !InstigatedBy->GetPawn() || //
     7        !GetOwner())
     8        return;
     9
    10    UAISense_Damage::ReportDamageEvent(GetWorld(), GetOwner(), InstigatedBy->GetPawn(), Damage, InstigatedBy->GetPawn()->GetActorLocation(),
    11                                       GetOwner()->GetActorLocation());
    12}

查看

  1. 设置NPC行为树

    为Selector添加Find Enemy服务


  2. 调试信息: 可见粉色调试球, 以及伤害源名称

    按下 ' 打开调试界面, 按下小写键盘上的 4 显示感知信息



附: NPC感知声音

  1. 为AIController添加声音感知

    BP_STUAIController


  2. 上报声音事件

    • 头文件

      1#include "Perception/AISense_Hearing.h"
    • 接口

       1/**
       2​ * Report a noise event.
       3​ * 
       4​ * @param NoiseLocation Location of the noise.
       5​ * @param Loudness Loudness of the noise. If MaxRange is non-zero, modifies MaxRange, otherwise modifies the squared distance of the sensor's range.
       6​ * @param Instigator Actor that triggered the noise.
       7​ * @param MaxRange Max range at which the sound can be heard, multiplied by Loudness. Values <= 0 mean no limit (still limited by listener's range however).
       8​ * @param Tag Identifier for the event.
       9 */
      10UFUNCTION(BlueprintCallable, Category = "AI|Perception", meta = (WorldContext="WorldContextObject"))
      11static void ReportNoiseEvent(UObject* WorldContextObject, FVector NoiseLocation, float Loudness = 1.f, AActor* Instigator = nullptr, float MaxRange = 0.f, FName Tag = NAME_None);