How to get enemy to face player while away.

I’m trying to make a system where the AI enemy “retreats” from the player when they are low on Health. They will try to move toward their original location when they’re low on Health. Of course, if the player is also in view, they will shoot back.

I’m currently having a hard time trying to implement this and unfortunately, it looks like a lot of other solutions I’ve found online don’t work either. One “solution” I see a lot is to use a Simple Parallel that executes the “Rotate to Face BB Entry” and “MoveTo” functions at the same time. Unfortunately, this only resulted in the AI jittering back and forth between the player and its start location.

Is there a specific box I should edit/tick in the Simple Parallel? How could I replicate the same effect since I see a lot of people claiming that this is the way to make an AI move and face the player at the same time.

I found that using “Rotate to Face BB Entry” is not an efficient way to do this. Instead, it is much better to Set the Gameplay Focus of the AI and call the “MoveTo” function.

Unfortunately, Setting the Gameplay Focus is not available in Blueprint by default. There is a “Set Default Focus,” however a Default Focus is overridden by the MoveTo function, making the AI face the direction they are moving in instead of the player.

I had to create a custom BTTask in C++ that set the AI’s focus on the player and after doing so, the AI finally began to face the player while moving backward as intended. This was the code used:

AActor* PlayerPawn = Cast<AActor>(UGameplayStatics::GetPlayerPawn(GetWorld(), 0));

if (PlayerPawn == nullptr)
{
        return;
}

OwnerComp.GetAIOwner()->SetFocus(PlayerPawn);
1 Like