AI Perception Component not Rotating with Character

Hello, I’m facing a rather strange issue: The AI Perception Component on my character is not rotating as the character rotates. Here is a screenshot describing the issue:

While the AI Character (Center) is facing the player (Lower left), the AI Perception component points straight and not in the directon the AI Character is looking.

I’ve initialized these values in C++ and created blueprints to place in the world.
Here is the C++ AIPatrol Character Constructor Code

AAIPatrol::AAIPatrol()
{
	bUseControllerRotationPitch = false;
	bUseControllerRotationYaw = false;
	bUseControllerRotationRoll = false;

	GetCharacterMovement()->bOrientRotationToMovement = true; 
}

and here is the C++ AI Perception Config code in the AIPatrolController Constructor:

AAI_Patrol_Controller::AAI_Patrol_Controller()
{
	PrimaryActorTick.bCanEverTick = false;

	//Sight config
	SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config"));
	SetPerceptionComponent(*CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("AI_Perception_Component")));

	if ((GetPerceptionComponent() != nullptr) && (GEngine != nullptr))
	{
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "AI Perception be set!");
		UE_LOG(LogTemp, Warning, TEXT("AI Perception Set"));
	}

	GetPerceptionComponent()->ConfigureSense(*SightConfig);
	GetPerceptionComponent()->SetDominantSense(SightConfig->GetSenseImplementation());
	GetPerceptionComponent()->OnPerceptionUpdated.AddDynamic(this, &AAI_Patrol_Controller::SetPlayerCaught);

	SightConfig->SightRadius = 3000.0;
	SightConfig->LoseSightRadius = 3000.0 + 20.0f;
	SightConfig->PeripheralVisionAngleDegrees = 50.0f;
	SightConfig->DetectionByAffiliation.bDetectEnemies = true;
	SightConfig->DetectionByAffiliation.bDetectNeutrals = true;
	SightConfig->DetectionByAffiliation.bDetectFriendlies = true;

	GetPerceptionComponent()->ConfigureSense(*SightConfig);



	BrainComponent = BehaviorTreeComponent = CreateDefaultSubobject<UBehaviorTreeComponent>(TEXT("BehaviorTreeComponent"));
	BlackboardComponent = CreateDefaultSubobject<UBlackboardComponent>(TEXT("BlackboardComponent"));
}

I’ve spent the last 2 days trying to find out what’s going wrong without finding any solutions whatsoever.
Any help to resolve this issue would be greatly appreciated.
Thank You!

I think what you’re missing is tying the perception component to your pawn. Within your controller after possessing the pawn you’ll need to call:

 UAIPerceptionSystem::RegisterPerceptionStimuliSource(this, SightConfig->GetSenseImplementation(), GetControlledPawn());

You can probably just override the Posses method:

void AIPatrolController::Possess(APawn* InPawn) {
    Super::Possess(InPawn);
    UAIPerceptionSystem::RegisterPerceptionStimuliSource(this, SightConfig->GetSenseImplementation(), InPawn);

}

Calling RegisterPerceptionStimuliSource is unnecessary if any of the senses used is configured to register always Pawns as sources (and sight is configured like that by default). Also RegisterPerceptionStimuliSource has nothing to do with the described issue.

It’s a strange one. It seems that the relevant AI perception code is being called, since both location and rotation of perception listeners are being updated in one function call. This suggests that your AI’s Rotation is different from its skel mesh’s facing. Have you tried removing bit by bit the changes to the default behavior your code introduces, like the stuff in your AAIPatrol constructor?

Cheers,

–mieszko

I tried this approach. It didn’t work. But thanks anyway. :slight_smile:

Yes, I’ve tried several changes:

  1. Created a new third person template project and used code from the ThirdPersonCharacter C++ code already provided in the project.

  2. Derived a default AIPatrolCharacter class from the ACharacter Base class

  3. Emptied all the contents from the current AIPatrol constructor

All to no effect. :frowning:
I really have no clue what more to do.

I faced a similar issue while programming my custom controller a while back.

It took a bit of finding, but I was able to rectify the issue by overriding the AController::GetControlRotation() function.

In your .h class you would put

virtual FRotator GetControlRotation() const override;

And in your .cpp file you’d have something like so

FRotator AIPatrolController::GetControlRotation() const
{
	if (GetPawn() == nullptr)
	{
		return FRotator();
	}

	return FRotator(0.0f, GetPawn()->GetActorRotation().Yaw, 0.0f);
}

Hope this helps.

1 Like

@MieszkoZ Does this issue occur due to a bug in the engine?

Hey,

The perception component calls the GetActorEyesViewPoint() to get the rotation and the view of the eyes/head, if you don’t override this function in your AICharacter it will use the default one.
In this default one it will call GetControlRotation() I believe, like XanderWraik mentioned.

Try to override this function and set the rotation to the rotation of the mesh or something.
In MyAICharacter.h:

virtual void GetActorEyesViewPoint(FVector& OutLocation, FRotator& OutRotation) const override;

Hope this helps and good luck with your project :slight_smile:
Elias