Add PerceptionComponent to `PlayerCharacter` and `EnemyCharacter`

This commit is contained in:
Louis Hobbs 2023-03-13 23:01:12 +00:00
parent 8a7ee5faee
commit 6127b5cd81
4 changed files with 18 additions and 1 deletions

View File

@ -8,6 +8,15 @@ AEnemyAIController::AEnemyAIController(const FObjectInitializer& object_initiali
{
Blackboard = CreateDefaultSubobject<UBlackboardComponent>(TEXT("Blackboard"));
BehaviorTree = CreateDefaultSubobject<UBehaviorTreeComponent>(TEXT("Behavior Tree"));
PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component"));
AIPerception = PerceptionComponent;
UAISenseConfig_Sight* ConfigSight = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Sense Config"));
ConfigSight->SightRadius = 1000.0f;
ConfigSight->LoseSightRadius = 1000.0f;
ConfigSight->PeripheralVisionAngleDegrees = 30.0f;
AIPerception->ConfigureSense(*ConfigSight);
}
void AEnemyAIController::OnPossess(APawn* InPawn)
@ -98,7 +107,7 @@ void AEnemyAIController::OnPerceptionUpdated(const TArray<AActor*>& actors)
}
FActorPerceptionBlueprintInfo perceptionInfo;
GetPerceptionComponent()->GetActorsPerception(actor, perceptionInfo);
PerceptionComponent->GetActorsPerception(actor, perceptionInfo);
for (auto& stimulus : perceptionInfo.LastSensedStimuli)
{
@ -107,6 +116,8 @@ void AEnemyAIController::OnPerceptionUpdated(const TArray<AActor*>& actors)
continue;
}
Blackboard->SetValueAsObject("TargetActor", actor);
if (stimulus.IsActive())
{
PlayerCharacter = Cast<APlayerCharacter>(actor);

View File

@ -28,6 +28,9 @@ private:
APlayerCharacter* PlayerCharacter;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true"))
UAIPerceptionComponent* AIPerception;
public:
AEnemyAIController(const FObjectInitializer& object_initializer);

View File

@ -20,6 +20,8 @@ class NAKATOMI_API AEnemyCharacter : public ANakatomiCharacter
GENERATED_BODY()
private:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true"))
UAIPerceptionComponent* PerceptionComponent;
UAISenseConfig_Sight* SightConfig;

View File

@ -81,6 +81,7 @@ private:
class UUserWidget* currentPlayerHUD;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true"))
UAIPerceptionStimuliSourceComponent* PerceptionSource;
public: