In the AIController theres this bit in the UpdateControlRotation
// Don't pitch view unless looking at another pawn
if (NewControlRotation.Pitch != 0 && Cast<APawn>(GetFocusActor()) == nullptr)
{
NewControlRotation.Pitch = 0.f;
}
This is wrong. What is intended is the character not to pitch while they are moving while not focused on an actor. What this does is override pitch regardless of you focus priorities. Here’s how you fix this:
EAIFocusPriority::Type AAIController::GetFocalPointAndPriority(FVector& OutVector) const
{
OutVector = FAISystem::InvalidLocation;
EAIFocusPriority::Type Result;
// find focus with highest priority
for (int32 Index = FocusInformation.Priorities.Num() - 1; Index >= 0; --Index)
{
const FFocusKnowledge::FFocusItem& FocusItem = FocusInformation.Priorities[Index];
AActor* FocusActor = FocusItem.Actor.Get();
if (FocusActor)
{
OutVector = GetFocalPointOnActor(FocusActor);
Result = Index;
break;
}
else if (FAISystem::IsValidLocation(FocusItem.Position))
{
OutVector = FocusItem.Position;
Result = Index;
break;
}
}
return Result;
}
This function returns the focal point along with the priority with this you can update UpdateControlRotation like so
void AAIController::UpdateControlRotation(float DeltaTime, bool bUpdatePawn)
{
APawn* const MyPawn = GetPawn();
if (MyPawn)
{
//FocusInformation.Priorities
const FRotator InitialControlRotation = GetControlRotation();
FRotator NewControlRotation = InitialControlRotation;
// Look toward focus
FVector FocalPoint;
EAIFocusPriority::Type PointPriority = GetFocalPointAndPriority(FocalPoint); // Thanks Frank!
if (FAISystem::IsValidLocation(FocalPoint))
{
NewControlRotation = (FocalPoint - MyPawn->GetPawnViewLocation()).Rotation();
}
else if (bSetControlRotationFromPawnOrientation)
{
NewControlRotation = MyPawn->GetActorRotation();
}
// Don't pitch view unless looking at another pawn
if (PointPriority == EAIFocusPriority::Move && Cast<APawn>(GetFocusActor()) == nullptr)
{
NewControlRotation.Pitch = 0.f;
}
if (InitialControlRotation.Equals(NewControlRotation, 1e-3f) == false)
{
SetControlRotation(NewControlRotation);
if (bUpdatePawn)
{
MyPawn->FaceRotation(NewControlRotation, DeltaTime);
}
}
}
}
And now it works as intended!