Add perception component
This commit is contained in:
@@ -93,4 +93,12 @@ ManualIPAddress=
|
|||||||
|
|
||||||
[/Script/GameplayDebugger.GameplayDebuggerConfig]
|
[/Script/GameplayDebugger.GameplayDebuggerConfig]
|
||||||
CategorySlot5=Five
|
CategorySlot5=Five
|
||||||
|
CategorySlot1=One
|
||||||
|
CategorySlot2=Two
|
||||||
|
CategorySlot3=Three
|
||||||
|
CategorySlot4=Four
|
||||||
|
CategorySlot6=Six
|
||||||
|
CategorySlot7=Seven
|
||||||
|
CategorySlot8=Eight
|
||||||
|
CategorySlot9=Nine
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
BIN
Content/AI/Minions/Minion_BB.uasset
LFS
Normal file
BIN
Content/AI/Minions/Minion_BB.uasset
LFS
Normal file
Binary file not shown.
BIN
Content/AI/Minions/Minion_BT.uasset
LFS
Normal file
BIN
Content/AI/Minions/Minion_BT.uasset
LFS
Normal file
Binary file not shown.
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "AI/CAIController.h"
|
#include "AI/CAIController.h"
|
||||||
|
|
||||||
|
#include "BehaviorTree/BlackboardComponent.h"
|
||||||
#include "Character/CCharacter.h"
|
#include "Character/CCharacter.h"
|
||||||
#include "Perception/AIPerceptionComponent.h"
|
#include "Perception/AIPerceptionComponent.h"
|
||||||
#include "Perception/AISenseConfig_Sight.h"
|
#include "Perception/AISenseConfig_Sight.h"
|
||||||
@@ -15,7 +16,7 @@ ACAIController::ACAIController()
|
|||||||
SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>("Sight config");
|
SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>("Sight config");
|
||||||
|
|
||||||
SightConfig->DetectionByAffiliation.bDetectEnemies = true;
|
SightConfig->DetectionByAffiliation.bDetectEnemies = true;
|
||||||
SightConfig->DetectionByAffiliation.bDetectFriendlies = false;
|
SightConfig->DetectionByAffiliation.bDetectFriendlies = true;
|
||||||
SightConfig->DetectionByAffiliation.bDetectNeutrals = false;
|
SightConfig->DetectionByAffiliation.bDetectNeutrals = false;
|
||||||
|
|
||||||
SightConfig->SightRadius = 1000.f;
|
SightConfig->SightRadius = 1000.f;
|
||||||
@@ -24,6 +25,7 @@ ACAIController::ACAIController()
|
|||||||
SightConfig->PeripheralVisionAngleDegrees = 100.f;
|
SightConfig->PeripheralVisionAngleDegrees = 100.f;
|
||||||
|
|
||||||
AIPerceptionComponent->ConfigureSense(*SightConfig);
|
AIPerceptionComponent->ConfigureSense(*SightConfig);
|
||||||
|
AIPerceptionComponent->OnTargetPerceptionUpdated.AddDynamic(this, &ThisClass::TargetPerceptionUpdated);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ACAIController::OnPossess(APawn* InPawn)
|
void ACAIController::OnPossess(APawn* InPawn)
|
||||||
@@ -40,9 +42,46 @@ void ACAIController::OnPossess(APawn* InPawn)
|
|||||||
void ACAIController::BeginPlay()
|
void ACAIController::BeginPlay()
|
||||||
{
|
{
|
||||||
Super::BeginPlay();
|
Super::BeginPlay();
|
||||||
|
RunBehaviorTree(BehaviorTree);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ACAIController::Tick(float DeltaTime)
|
void ACAIController::Tick(float DeltaTime)
|
||||||
{
|
{
|
||||||
Super::Tick(DeltaTime);
|
Super::Tick(DeltaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ACAIController::TargetPerceptionUpdated(AActor* TargetActor, FAIStimulus Stimulus)
|
||||||
|
{
|
||||||
|
UE_LOG(LogTemp, Warning, TEXT("Saw you"))
|
||||||
|
// GEngine->AddOnScreenDebugMessage(
|
||||||
|
// -1, 12.f, FColor::Blue,
|
||||||
|
// FString::Printf(TEXT("saw you")));
|
||||||
|
|
||||||
|
if (Stimulus.WasSuccessfullySensed())
|
||||||
|
{
|
||||||
|
if (!GetCurrentTarget()) SetCurrentTarget(TargetActor);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (GetCurrentTarget() == TargetActor) SetCurrentTarget(nullptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const UObject* ACAIController::GetCurrentTarget() const
|
||||||
|
{
|
||||||
|
if (const auto BlackboardComponent{GetBlackboardComponent()})
|
||||||
|
{
|
||||||
|
const auto Target{BlackboardComponent->GetValueAsObject(BlackboardTargetName)};
|
||||||
|
return Target;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ACAIController::SetCurrentTarget(AActor* NewTarget)
|
||||||
|
{
|
||||||
|
const auto BlackboardComponent{GetBlackboardComponent()};
|
||||||
|
if (!BlackboardComponent) return;
|
||||||
|
|
||||||
|
if (NewTarget) BlackboardComponent->SetValueAsObject(BlackboardTargetName, NewTarget);
|
||||||
|
else BlackboardComponent->ClearValue(BlackboardTargetName);
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include "CoreMinimal.h"
|
#include "CoreMinimal.h"
|
||||||
#include "AIController.h"
|
#include "AIController.h"
|
||||||
|
#include "Perception/AIPerceptionTypes.h"
|
||||||
#include "CAIController.generated.h"
|
#include "CAIController.generated.h"
|
||||||
|
|
||||||
UCLASS()
|
UCLASS()
|
||||||
@@ -22,9 +23,21 @@ public:
|
|||||||
virtual void Tick(float DeltaTime) override;
|
virtual void Tick(float DeltaTime) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
UPROPERTY(EditDefaultsOnly, Category="AI Behavior")
|
||||||
|
FName BlackboardTargetName {"Target"};
|
||||||
|
UFUNCTION()
|
||||||
|
void TargetPerceptionUpdated(AActor* TargetActor, FAIStimulus Stimulus);
|
||||||
|
|
||||||
|
UPROPERTY(EditDefaultsOnly, Category="AI Behavior")
|
||||||
|
UBehaviorTree* BehaviorTree;
|
||||||
|
|
||||||
UPROPERTY(VisibleDefaultsOnly, Category="Perception")
|
UPROPERTY(VisibleDefaultsOnly, Category="Perception")
|
||||||
UAIPerceptionComponent* AIPerceptionComponent;
|
UAIPerceptionComponent* AIPerceptionComponent;
|
||||||
|
|
||||||
UPROPERTY(VisibleDefaultsOnly, Category="Perception")
|
UPROPERTY(VisibleDefaultsOnly, Category="Perception")
|
||||||
class UAISenseConfig_Sight* SightConfig;
|
class UAISenseConfig_Sight* SightConfig;
|
||||||
|
UFUNCTION()
|
||||||
|
const UObject* GetCurrentTarget() const ;
|
||||||
|
void SetCurrentTarget(AActor* NewTarget);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user