Download

C++ AI Perception Touch not triggering

Hello,

I am trying to add the Touch Perception in a little project I’m toying with, but with no result. I already have the hearing and sight perception added, and those alongside touch are displayed in the Perception Category in the AI debug.

In my scene I have two AIs, one patrolling and the other running into it.



AAICharacterController::AAICharacterController()
{
SetPerceptionComponent(*CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component")));
Sight = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config"));
Hearing = CreateDefaultSubobject<UAISenseConfig_Hearing>(TEXT("Hearing Config"));
Touch = CreateDefaultSubobject<UAISenseConfig_Touch>(TEXT("Touch Config"));

// Setup the sight parameters
Sight->SightRadius = AISightRadius;
Sight->LoseSightRadius = AISightLoseRadius;
Sight->PeripheralVisionAngleDegrees = AIFieldOfView;
Sight->SetMaxAge(AISightAge);

// What the pawn can detect
Sight->DetectionByAffiliation.bDetectEnemies = true;
Sight->DetectionByAffiliation.bDetectNeutrals = true;
Sight->DetectionByAffiliation.bDetectFriendlies = true;

Hearing->HearingRange = HearingRange;
Hearing->SetMaxAge(HearingAge);

Hearing->DetectionByAffiliation.bDetectEnemies = true;
Hearing->DetectionByAffiliation.bDetectFriendlies = true;
Hearing->DetectionByAffiliation.bDetectNeutrals = true;

Touch->SetMaxAge(1.0f);

// Configure the pawn's sight
GetPerceptionComponent()->SetDominantSense(*Sight->GetSenseImplementation());

// Called when sight is updated. Returns an Array of all the actors which updated the sight.
GetPerceptionComponent()->OnPerceptionUpdated.AddDynamic(this, &AAICharacterController::OnPawnDetected);

//Called when sight is updated. Returns the target that updated it with the information about the sight updated. (Ex. Successfully sensed or not)
GetPerceptionComponent()->OnTargetPerceptionUpdated.AddDynamic(this, &AAICharacterController::OnTargetDetected);

// Configura/Initialize the sight
GetPerceptionComponent()->ConfigureSense(*Sight);
GetAIPerceptionComponent()->ConfigureSense(*Hearing);
GetPerceptionComponent()->ConfigureSense(*Touch);

// Create the AI components
BehaviorComp = CreateDefaultSubobject<UBehaviorTreeComponent>(TEXT("BehaviorComp"));
BlackboardComp = CreateDefaultSubobject<UBlackboardComponent>(TEXT("BlackboardComp"));

}

void AAICharacterController::OnTargetDetected(AActor* Actor, FAIStimulus const stimulus)
{
AAI_PlayCharacter* Bot = Cast<AAI_PlayCharacter>(Actor);
bool isSensed = stimulus.WasSuccessfullySensed();
AAI_PlayCharacter* thisBot = Cast<AAI_PlayCharacter>(GetPawn());

// Seen
if (stimulus.Type.Name.ToString() == "Default__AISense_Sight")
{
// working stuff here
}

// Heared
else if (stimulus.Type.Name.ToString() == "Default__AISense_Hearing")
{
// working stuff here
}

// Touched
else if (stimulus.Type.Name.ToString() == "Default__AISense_Touch")
{
GEngine->AddOnScreenDebugMessage(-123, 3.0f, FColor::Emerald, TEXT("TOUCH???"));
}

// Else
else
{
GEngine->AddOnScreenDebugMessage(344, 3.0f, FColor::Black, stimulus.Type.Name.ToString());
}
}