How to make AI look at an object?

Hi all. I want to make a task for a Pawn which has its own and custom AIController to look at an specific object in the world (an actor). What I’ve tried so far is to create a task with an AActor UPROPERTY which will be the object I want the AI to look at, and then in the “ExecuteTask” function I get the rotator with “Find look at rotation” between the pawn and the aactor. Finally, I just set the pawn rotation.

EBTNodeResult::Type UTK_TaskLookAtObject::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
	ATK_AIController *AIController = Cast<ATK_AIController>(OwnerComp.GetAIOwner());
	if (AIController)
	{
		UBlackboardComponent* BlackboardComp = AIController->GetBlackboardComponent();
		AActor* Enemy = AIController->GetPawn();
		if (Enemy && ObjectToLookAt)
		{
			FVector TargetLocation(ObjectToLookAt->GetActorLocation().X, ObjectToLookAt->GetActorLocation().Y, Enemy->GetActorLocation().Z);
			FRotator EnemyRotation = UKismetMathLibrary::FindLookAtRotation(Enemy->GetActorLocation(), TargetLocation);
			Enemy->SetActorRotation(EnemyRotation);
			BlackboardComp->SetValueAsObject(AIController->Target, ObjectToLookAt);
			return EBTNodeResult::Succeeded;
		}
	}
	return EBTNodeResult::Failed;
}

The problem is that I can’t select the actor in the task properties in the editor, and I think it is because it is not possible to choose a scene actor directly because the default properties of the task are not tied to any world.

I can’t think about another way of doing this, so please can anybody help me? Thank you very much in advance!

1 Like

I think the correct way to do this will be setting an ObjectToLookAt on your Blackboard, then accessing that in this Task