Unreal Engine AI C++ PART -2 Chase Target
In Last Part We see find a target (Player) . In This part our AIEnemy Follow our Player and If The Player in Range Follow the player and not In Range AIEnemy Move to Home Location or Root Location move Randomly.
Start!!
Add Some Sphere Component For Range of AIEnemy to Detect Player;
Open Up EnemyAIController class Files;
#pragma once
#include "CoreMinimal.h"
#include "AIController.h"
#include "BehaviorTree/BehaviorTreeTypes.h"
#include "EnemyAIController.generated.h"
/**
*
*/
UCLASS()
class MASTERCHARATER_API AEnemyAIController : public AAIController
{
GENERATED_BODY()
public:
AEnemyAIController(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());
class UBehaviorTreeComponent* BehaviourTreeComp;
UBlackboardComponent* BlackBoardComp;
/*Hearing Radius */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Targeting Hearing Radius")
float HearingRadius = 300.0f;
virtual void OnPossess(APawn* InPawn) override;
/*Blueprint Event when Target is Change or Out of Radius*/
UFUNCTION(BlueprintImplementableEvent)
void OnTargetChange(class AMasterCharaterCharacter* Target);
/*Set Target or Get Target Character*/
UFUNCTION(BlueprintCallable)
class AMasterCharaterCharacter* GetTarget();
UFUNCTION(BlueprintCallable)
void SetReturnHome();
UFUNCTION(BlueprintCallable)
void OnReturnHome();
/*Components for Detect Players or Target Ranges*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Targeting")
class USphereComponent* HearingSphere;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Targeting")
class USphereComponent* StealthHearingSphere;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Targeting")
class USphereComponent* SightSphere;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Targeting")
float StealthHearingRadius = 140.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Targeting")
float SightRadius = 1800.0f;
/*Sight Angle For Target Identify like Angle of eyes< */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Targeting")
float SightRAngle = 75.0f;
/*Overlap Triggers functions */
UFUNCTION()
void OnHearingOverlap(UPrimitiveComponent* OverlapedComp, AActor* Other, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UFUNCTION()
void OnStealthHearingOverlap(UPrimitiveComponent* OverlapedComp, AActor* Other, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UFUNCTION()
void OnSightOverlap(UPrimitiveComponent* OverlapedComp, AActor* Other, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
/*Current Target Is Player*/
UPROPERTY()
AMasterCharaterCharacter* CurrentTarget = nullptr;
/*Get pawn Nav Agent Location and change in random location in BP_Task*/
UPROPERTY(BlueprintReadOnly)
FVector HomeLocation;
// Not For use But Don't Remove It;
FBlackboard::FKey TargetKeyId;
FBlackboard::FKey MoveToLocation;
FBlackboard::FKey LocationToGo;
TArray<AActor*> TargetPoints;
FORCEINLINE UBlackboardComponent* GetBlackboardComp() const { return BlackBoardComp; }
FORCEINLINE TArray<AActor*> GetAvilableTargetPoints() { return TargetPoints; }
};
Declare Functions in EnemyAIController.cpp
AEnemyAIController::AEnemyAIController(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) {
BehaviourTreeComp = CreateDefaultSubobject<UBehaviorTreeComponent>("Behaviour Tree");
BlackBoardComp = CreateDefaultSubobject<UBlackboardComponent>("Black Board ");
HearingSphere = CreateDefaultSubobject<USphereComponent>(TEXT("Sphe Radius"));
HearingSphere->SetCollisionObjectType(ECC_Pawn);
HearingSphere->SetCollisionProfileName("Trigger");
HearingSphere->OnComponentBeginOverlap.AddDynamic(this, &AEnemyAIController::OnHearingOverlap);
StealthHearingSphere = CreateDefaultSubobject<USphereComponent>("StealthHear Sphering");
StealthHearingSphere->SetCollisionObjectType(ECC_Pawn);
StealthHearingSphere->SetCollisionProfileName("Trigger");
StealthHearingSphere->OnComponentBeginOverlap.AddDynamic(this, &AEnemyAIController::OnStealthHearingOverlap);
SightSphere = CreateDefaultSubobject<USphereComponent>("Sight Radius");
SightSphere->SetCollisionObjectType(ECC_Pawn);
SightSphere->SetCollisionProfileName("Trigger");
SightSphere->OnComponentBeginOverlap.AddDynamic(this, &AEnemyAIController::OnSightOverlap);
bAttachToPawn = true;
SetReturnHome();
}
HearingSphere Component is Now for Important Because this is range of AIEnemy to Find Target;
HearingSphere->SetCollisionObjectType(channelName); // Change The Collision Channel when Object object uses when it moves HearingSphere->SetCollisionProfileName(newProfilename); // This Function Called by constructors Collision Profile name Change and override Profile name Setting; HearingSphere->OnComponentBeginOverlap.AddDynamic(this, &AEnemyAIController::OnHearingOverlap); // Event Called When Component overlaps or Enter Trigger const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer // Crate Component or SubObject
bAtachPawn = true // If true, the controller location will match the possessed Pawn's location
The Next OnPossess();
void AEnemyAIController::OnPossess(APawn* InPawn)
{
Super::OnPossess(InPawn);
AAIEnemy* Charactear = Cast<AAIEnemy>(InPawn);
if (Charactear && Charactear->EnemyBehaviourTree)
{
// Set Black Board;
BlackBoardComp->InitializeBlackboard(*Charactear->EnemyBehaviourTree->BlackboardAsset);
UGameplayStatics::GetAllActorsOfClass(GetWorld(), APatrolTargetPoint::StaticClass(), TargetPoints);
// Set Up Key We Create in BlackBoard Blueprint;
TargetKeyId = BlackBoardComp->GetKeyID("Target");
MoveToLocation = BlackBoardComp->GetKeyID("MoveToLocation");
LocationToGo = BlackBoardComp->GetKeyID("LocationToGo");
//Check Out The Behaviour tree is Now Set;
BehaviourTreeComp->StartTree(*Charactear->EnemyBehaviourTree);
}
/*We First need To Nav Mesh Location For AI Enemy Randomly Patroling;*/
HomeLocation = GetPawn()->GetNavAgentLocation();
/*Attach To Other Scence Component */
HearingSphere->AttachToComponent(GetRootComponent(), FAttachmentTransformRules::SnapToTargetIncludingScale);
StealthHearingSphere->AttachToComponent(GetRootComponent(), FAttachmentTransformRules::SnapToTargetIncludingScale);
SightSphere->AttachToComponent(GetRootComponent(), FAttachmentTransformRules::SnapToTargetIncludingScale);
OnReturnHome();
}
AEnemyAIController::OnPossess(APawn* InPawn);
// Override native Function Controller Possess a Pawn
HomeLocation = GetPawn()->GetNavAgentLocation();
// retrieves the Agents position's
Next GetTarget();
class AMasterCharaterCharacter* AEnemyAIController::GetTarget() {
return CurrentTarget;
}
SetReturnHome();
void AEnemyAIController::SetReturnHome()
{
// Set Return Home AI Going To Own root Location
HearingSphere->SetSphereRadius(0.0f);
StealthHearingSphere->SetSphereRadius(0.0f);
SightSphere->SetSphereRadius(0.0f);
CurrentTarget = nullptr;
}
SetSphereRadius(float Radius);
OnReturnHome();
void AEnemyAIController::OnReturnHome()
{
// Find Random Locations to insert Argument Radius of How long hearing the target;
HearingSphere->SetSphereRadius(HearingRadius);
StealthHearingSphere->SetSphereRadius(StealthHearingRadius);
SightSphere->SetSphereRadius(SightRadius);
}
Last one Is OnHearingOverlap();
void AEnemyAIController::OnHearingOverlap(UPrimitiveComponent* OverlapedComp, AActor* Other, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
AMasterCharaterCharacter* Target = Cast<AMasterCharaterCharacter>(Other);
if (Target != nullptr && CurrentTarget != Target)
{
CurrentTarget = Target;
OnTargetChange(CurrentTarget);
}
}
// Optional for Now
void AEnemyAIController::OnStealthHearingOverlap(UPrimitiveComponent* OverlapedComp, AActor* Other, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
}
void AEnemyAIController::OnSightOverlap(UPrimitiveComponent* OverlapedComp, AActor* Other, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
}
Target Is Overlap the Radius of Sphere we set the Current Target is Our Player(Target) and Call Event OnTargetChagne(CurrentTarget);
EnemyAIController Is Now Done!!
Move Unreal Engine Create First BPTask_MoveToHome Blueprint Class
Create Task Go To Behavior Tree Upper Menu Toolbar Find New Task Click On BPTask_BlueprintTaskBase and Rename it BPTask_MoveToHome
Next Blueprint Task is Move To Target;
ObjEqual Variable is MasterCharacter object Reference;
Set All !
LocationToGo (Actor/Object) Declared in Part 1;
In This Black Board Variables you Don't Need to Declare but if Want to You Do!!
The AIEnemy Don't Work!!
Open AIEnemy Character Blueprint Class check AI Controller is EnemyAIController or Your AI Controller Blueprint Class;
Check Out Behavior Tree In AIEnemy Blueprint class is Right;
Comments
Post a Comment