Update - Tanks now able to run patrol routes and once player enters the radius of perception the tanks give chase and try and shoot the player. PROBLEM/QUESTION?: What function do I run in blueprint to be able to set the state back to patrolling when the Sentry AI looses sight of the player?
Please note this is still buggy, will update as I make progress.
Inside of TankAIController.h I added the following:
// Enum for AI perception state
UENUM()
enum class EPerceptionStatus : uint8
{
Patrolling, Seeking
};
public:
UFUNCTION(BlueprintCallable, Category = "State")
EPerceptionStatus GetPerceptionStatus() const;
UFUNCTION(BlueprintCallable, Category = "State")
void SetPerceptionStatus(EPerceptionStatus NextPerceptionStatus);
private:
//set the default value to patrolling
UPROPERTY(VisibleAnywhere, Category = "State")
EPerceptionStatus PerceptionStatus = EPerceptionStatus::Patrolling;
Inside of TankTankAIController.cpp I added this:
void ATankAIController::SetPerceptionStatus(EPerceptionStatus NextPerceptionStatus)
{
PerceptionStatus = NextPerceptionStatus;
}
EPerceptionStatus ATankAIController::GetPerceptionStatus() const
{
return PerceptionStatus;
}
Then I placed the current AI code to move/aim/fire inside of the Tick function inside of an if statement in order to see if the state that was set in blueprint was changed to Seeking:
if (PerceptionStatus == EPerceptionStatus::Seeking) {
UE_LOG(LogTemp, Warning, TEXT("Found Enemy, SEARCH AND DESTROY!"))
// Move towards the player
MoveToActor(PlayerTank, AcceptanceRadius); // TODO Check radius is in cm
// Aim towards the player
auto AimingComponent = ControlledTank->FindComponentByClass<UTankAimingComponent>();
AimingComponent->AimAt(PlayerTank->GetActorLocation());
// If aim & locked Fire towrads the player
if (AimingComponent->GetFiringState() == EFiringStatus::Locked)
{
AimingComponent->Fire(); // TODO limit firing rate
}
}
Then I hooked up the blueprint as follows:
Blueprint imgur link.
I hope this helps someone out looking into something similar. Ideally I would like to do the entire thing in C++ however there is so little documentation available on OnTargetPerceptionUpdated that I felt a bit intimidated to tackle it just using the comments in the base code.
My next step is to get the tanks to stop following the player once the player has exited their perception radius. I’m happy for any suggestions anyone has.