Thanks for the answer. I am doing most of this in C++. Only the “MoveTo” is used within the Behavior Tree.
http://puu.sh/gQn45/fe089e7277.png
If i use “EnemyPosition” which is the Actor Location of Enemy, it get really weird. He moves to the point where he found me and stays there instead of following me.
The C++ part is here:
/// Behavior Functions ///
void ABaseAIController::SearchEnemy()
{
ABaseAIClass* MyPawn = Cast<ABaseAIClass>(GetPawn());
if (MyPawn == NULL)
{
return;
}
const FVector BotLocation = MyPawn->GetActorLocation();
float BestDistance = MAX_FLT;
ASmallCoopTDCharacter* EnemyPawn = NULL;
for (FConstPawnIterator It = GetWorld()->GetPawnIterator(); It; ++It)
{
ASmallCoopTDCharacter* TempEnemyPawn = Cast<ASmallCoopTDCharacter>(*It);
if (TempEnemyPawn)
{
const float Distance = FVector::Dist(TempEnemyPawn->GetActorLocation(), BotLocation);
if (Distance < BestDistance)
{
BestDistance = Distance;
EnemyPawn = TempEnemyPawn;
}
}
}
if (EnemyPawn && BestDistance <= MyPawn->GetBotConfig().SenseRange)
{
SetNewEnemy(EnemyPawn);
}
}
void ABaseAIController::SetNewEnemy(class APawn* NewEnemy)
{
BlackboardComp->SetValueAsObject(EnemyKeyID, NewEnemy);
BlackboardComp->SetValueAsVector(EnemyLocationKeyID, NewEnemy->GetActorLocation());
}
That is just a simple “If we find an enemy withing range and he is the nearest, then set the BlackBoard values” algorithm. And in my Service, i just call the SearchEnemy() function.