Any idea what I’m probably missing this? The bot rotates Ok towards player but destination is always 0.0,0.0,0.0.
AIController.cpp
// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
#include "Nex.h"
#include "NexAIController.h"
ANexAIController::ANexAIController(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
BlackboardComp = PCIP.CreateDefaultSubobject<UBlackboardComponent>(this, TEXT("BlackBoardComp"));
BehaviorComp = PCIP.CreateDefaultSubobject<UBehaviorTreeComponent>(this, TEXT("BehaviorComp"));
bWantsPlayerState = true;
}
void ANexAIController::Possess(APawn* InPawn)
{
Super::Possess(InPawn);
ANexBot* Bot = Cast<ANexBot>(InPawn);
// start behavior
if (Bot && Bot->BotBehavior)
{
BlackboardComp->InitializeBlackboard(Bot->BotBehavior->BlackboardAsset);
EnemyKeyID = BlackboardComp->GetKeyID("Enemy");
NeedAmmoKeyID = BlackboardComp->GetKeyID("NeedAmmo");
BehaviorComp->StartTree(Bot->BotBehavior);
}
}
void ANexAIController::BeginInactiveState()
{
Super::BeginInactiveState();
AGameState* GameState = GetWorld()->GameState;
const float MinRespawnDelay = (GameState && GameState->GameModeClass) ? GetDefault<AGameMode>(GameState->GameModeClass)->MinRespawnDelay : 1.0f;
GetWorldTimerManager().SetTimer(this, &ANexAIController::Respawn, MinRespawnDelay);
}
void ANexAIController::Respawn()
{
GetWorld()->GetAuthGameMode()->RestartPlayer(this);
}
void ANexAIController::FindClosestEnemy()
{
APawn* MyBot = GetPawn();
if (MyBot == NULL)
{
return;
}
const FVector MyLoc = MyBot->GetActorLocation();
float BestDistSq = MAX_FLT;
ANexCharacter* BestPawn = NULL;
for (FConstPawnIterator It = GetWorld()->GetPawnIterator(); It; ++It)
{
ANexCharacter* TestPawn = Cast<ANexCharacter>(*It);
if (TestPawn && TestPawn->IsEnemyFor(this))
{
const float DistSq = (TestPawn->GetActorLocation() - MyLoc).SizeSquared();
if (DistSq < BestDistSq)
{
BestDistSq = DistSq;
BestPawn = TestPawn;
}
}
}
if (BestPawn)
{
SetEnemy(BestPawn);
}
}
void ANexAIController::SetEnemy(class APawn* InPawn)
{
if (BlackboardComp)
{
BlackboardComp->SetValueAsObject(EnemyKeyID, InPawn);
SetFocus(InPawn);
}
}
class ANexCharacter* ANexAIController::GetEnemy() const
{
if (BlackboardComp)
{
return Cast<ANexCharacter>(BlackboardComp->GetValueAsObject(EnemyKeyID));
}
return NULL;
}
void ANexAIController::UpdateControlRotation(float DeltaTime, bool bUpdatePawn)
{
// Look toward focus
FVector FocalPoint = GetFocalPoint();
if (!FocalPoint.IsZero() && GetPawn())
{
FVector Direction = FocalPoint - GetPawn()->GetActorLocation();
FRotator NewControlRotation = Direction.Rotation();
NewControlRotation.Yaw = FRotator::ClampAxis(NewControlRotation.Yaw);
SetControlRotation(NewControlRotation);
APawn* const P = GetPawn();
if (P && bUpdatePawn)
{
P->FaceRotation(NewControlRotation, DeltaTime);
}
}
}