# Using AI perception sight to target head in UE5.3

**URL:** <https://forums.unrealengine.com/t/using-ai-perception-sight-to-target-head-in-ue5-3/1685932>\
**Category:** AI\
**Tags:** Perception, AI-Perception, perception-component, unreal-engine, CPP, question\
**Created:** [February 13, 2024, 1:02pm UTC](https://forums.unrealengine.com/t/using-ai-perception-sight-to-target-head-in-ue5-3/1685932 "2024-02-13T13:02:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Slavian2003](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Slavian2003](https://forums.unrealengine.com/u/Slavian2003)\
**Post date:** [February 13, 2024, 1:02pm UTC](https://forums.unrealengine.com/t/using-ai-perception-sight-to-target-head-in-ue5-3/1685932/1 "2024-02-13T13:02:48Z")

</div>

Hello. I’m making third person game and have a problem with AI Perception in UE5.3. AI Perception Sight targets the center of the body of my character and I need it to use head of my character as the target.

So, I made research and found out that the only way to implement this is to use C++. My project was made with the help of blueprint only and I never used C++ before, so don’t judge me too harshly. I also find following instructions [IAISightTargetInterface::CanBeSeenFrom | Unreal Engine 5.2 Documentation](https://docs.unrealengine.com/5.3/en-US/API/Runtime/AIModule/Perception/IAISightTargetInterface/CanBeSeenFrom/1/) and used it to make a code.

First, I added to my BP\_NPC\_Base (this is base for all NPC character in my game) a AIPerception and set Sight as following.

 ![BP_NPC_Base](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/d/0/cd05c1f2971b2e3517b81dacead373c453655541.jpeg)

 ![Perception](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/f/9/6f9f36126db797a89a43c9f0cb0468757758df14.jpeg)

Then I added to the BP\_NPC\_Base a parent character class in C++ named C\_Character and used Visual Studio 2022 to implemented following code based on instructions mentioned above.  
C\_Character.h code is as follows

 ![C_Character.h](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/6/0/660208171801193149a64300fc28befecd18af68.jpeg)

C\_Character.cpp code is as follows

 ![C_Character.cpp](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/5/e/05e34dbc432fb562d842578908c7bbad05e160d2.jpeg)

So, after that it seems that AISIghtTargetInterface was implemented as Inherent in Class Settings.

 ![Parent_Class](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/4/e/04e30484c4f0c0913e8db52724d40ac858bc2878.jpeg)

But nothing happens. The AIPerseption sight green sphere is still in the center of the torso of my character.

Did I miss something? Do you have any suggestions? I would be glad to have a piece of advice.

---

<div class="post-metadata">

**Author:** ![Slavian2003](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Slavian2003](https://forums.unrealengine.com/u/Slavian2003)\
**Post date:** [February 17, 2024, 7:13am UTC](https://forums.unrealengine.com/t/using-ai-perception-sight-to-target-head-in-ue5-3/1685932/2 "2024-02-17T07:13:15Z")

</div>

Here is the code for cpp file.

```auto
    // Fill out your copyright notice in the Description page of Project Settings.

#include "C_Character.h"
#include "Perception/AIPerceptionComponent.h"
#include "Perception/AISense_Sight.h"
#include "Kismet/GameplayStatics.h"
#include "DrawDebugHelpers.h"

// Sets default values
AC_Character::AC_Character()
{
	// Set this character to call Tick() every frame. 
	PrimaryActorTick.bCanEverTick = true;
}

// Called when the game starts or when spawned
void AC_Character::BeginPlay()
{
	Super::BeginPlay();
}

// Called every frame
void AC_Character::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

// Called to bind functionality to input
void AC_Character::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
}

// Implement the CanBeSeenFrom method
UAISense_Sight::EVisibilityResult AC_Character::CanBeSeenFrom(const FCanBeSeenFromContext& Context, FVector& OutSeenLocation, int32& OutNumberOfLoSChecksPerformed, int32& OutNumberOfAsyncLosCheckRequested, float& OutSightStrength, int32* UserData, const FOnPendingVisibilityQueryProcessedDelegate* Delegate)
{
    // Get the player character
    ACharacter* PlayerCharacter = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);

    // Check if the player character is valid
    if (!IsValid(PlayerCharacter))
    {
        return UAISense_Sight::EVisibilityResult::NotVisible; // Player character is not valid, consider not visible
    }

    // Get the head socket location of the player character
    FVector PlayerHeadLocation = FVector::ZeroVector;
    if (USkeletalMeshComponent* PlayerMesh = PlayerCharacter->GetMesh())
    {
        // Get the head bone name
        FName HeadBoneName = PlayerMesh->GetBoneName(PlayerMesh->GetBoneIndex("head"));
        // Get the head bone location
        PlayerHeadLocation = PlayerMesh->GetBoneLocation(HeadBoneName);
    }
    else
    {
        // Fallback to the middle of the player character if mesh or socket is not found
        PlayerHeadLocation = PlayerCharacter->GetActorLocation();
    }

    // Perform a line trace from the NPC's head to the player's head
    FHitResult HitResult;
    FCollisionQueryParams CollisionParams;
    CollisionParams.AddIgnoredActor(Context.IgnoreActor);

    bool bHit = GetWorld()->LineTraceSingleByChannel(HitResult, Context.ObserverLocation, PlayerHeadLocation, ECC_Visibility, CollisionParams);

    // Optional: Visualize the line trace for debugging purposes
    if (bHit)
    {
        DrawDebugLine(GetWorld(), Context.ObserverLocation, PlayerHeadLocation, FColor::Green, false, 0.1f, 0, 1.0f);
    }
    else
    {
        DrawDebugLine(GetWorld(), Context.ObserverLocation, PlayerHeadLocation, FColor::Red, false, 0.1f, 0, 1.0f);
    }

    // If the line trace hits the player's head, consider the player's head visible
    if (bHit && HitResult.GetActor() == PlayerCharacter)
    {
        OutSeenLocation = PlayerHeadLocation;
        OutNumberOfLoSChecksPerformed = 1;
        OutNumberOfAsyncLosCheckRequested = 0;
        OutSightStrength = 1.0f; // Max visibility strength
        return UAISense_Sight::EVisibilityResult::Visible; // Player head is visible
    }
    else
    {
        OutNumberOfLoSChecksPerformed = 1;
        OutNumberOfAsyncLosCheckRequested = 0;
        OutSightStrength = 0.0f; // No visibility
        return UAISense_Sight::EVisibilityResult::NotVisible; // Player head is not visible
    }

    // This line should be outside the if-else block
    return UAISense_Sight::EVisibilityResult::NotVisible; // Default to not visible 
}

```

---

<div class="post-metadata">

**Author:** ![Slavian2003](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Slavian2003](https://forums.unrealengine.com/u/Slavian2003)\
**Post date:** [February 17, 2024, 7:14am UTC](https://forums.unrealengine.com/t/using-ai-perception-sight-to-target-head-in-ue5-3/1685932/3 "2024-02-17T07:14:16Z")

</div>

Here is the code for h file

```auto
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Perception/AISightTargetInterface.h" // Include the AISightTargetInterface header
#include "C_Character.generated.h"

UCLASS()
class TRIDEVIATOE6_API AC_Character : public ACharacter, public IAISightTargetInterface // Add IAISightTargetInterface
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	AC_Character();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

	// Implement the CanBeSeenFrom method from IAISightTargetInterface
	virtual UAISense_Sight::EVisibilityResult CanBeSeenFrom(const FCanBeSeenFromContext& Context, FVector& OutSeenLocation, int32& OutNumberOfLoSChecksPerformed, int32& OutNumberOfAsyncLosCheckRequested, float& OutSightStrength, int32* UserData = nullptr, const FOnPendingVisibilityQueryProcessedDelegate* Delegate = nullptr) override;
};

```

---

<div class="post-metadata">

**Author:** ![Skreewd](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Skreewd](https://forums.unrealengine.com/u/Skreewd)\
**Post date:** [May 6, 2024, 5:59pm UTC](https://forums.unrealengine.com/t/using-ai-perception-sight-to-target-head-in-ue5-3/1685932/4 "2024-05-06T17:59:01Z")

</div>

You are supposed to override that function on your character, not the ai.

```auto
...
FVector SightTargetLocation = GetMesh()->GetSocketLocation("neck_01");

bool hit = GetWorld()->LineTraceSingleByChannel(HitResult, Context.ObserverLocation, SightTargetLocation, ECC_Visibility, FCollisionQueryParams(Name_AILineOfSight, false, Context.IgnoreActor));

AActor* actor = Cast<AActor>(HitResult.GetActor());

if (!hit || (IsValid(actor) && actor->IsOwnedBy(this))) {
	OutNumberOfLoSChecksPerformed = 1;
	OutNumberOfAsyncLosCheckRequested = 0;
	OutSightStrength = 1;
	//We hit the player
	return UAISense_Sight::EVisibilityResult::Visible;
}

OutNumberOfLoSChecksPerformed = 1;
OutNumberOfAsyncLosCheckRequested = 0;
OutSightStrength = 0;

return UAISense_Sight::EVisibilityResult::NotVisible;

```

---

<div class="post-metadata">

**Author:** ![Deathglaze](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Deathglaze](https://forums.unrealengine.com/u/Deathglaze)\
**Post date:** [December 21, 2024, 6:31am UTC](https://forums.unrealengine.com/t/using-ai-perception-sight-to-target-head-in-ue5-3/1685932/5 "2024-12-21T06:31:34Z")

</div>

HI! I’m stuck for a few days I am getting error on #include “Perception/AISightTargetInterface.h”  
I’m sorry I’m new to C++

 ![Screenshot 2024-12-21 023115](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/e/3/be3eccce073f793a4fa513f1d2b3368adcffba7e.png)

---

<div class="post-metadata">

**Author:** ![JadenKorr](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JadenKorr](https://forums.unrealengine.com/u/JadenKorr)\
**Post date:** [January 3, 2025, 10:24pm UTC](https://forums.unrealengine.com/t/using-ai-perception-sight-to-target-head-in-ue5-3/1685932/6 "2025-01-03T22:24:39Z")

</div>

Hi, I had the same problem you did and got to this thread after searching.  
From your part and the previous answer I put together my code and it now effectively tracks the head:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/3/9/43978999cb6cbf81557dde87cfba0d4367bc972d.jpeg)

How I did it is implementing it in the character you want to trace, so not in the AI that looks for the character, but your character who you want to be visible, in my case this is called CharacterMaster.

Here is the implementation in the .cpp file

```auto
UAISense_Sight::EVisibilityResult ACPP_CharacterMaster::CanBeSeenFrom(
	const FCanBeSeenFromContext& Context,
	FVector& OutSeenLocation,
	int32& OutNumberOfLoSChecksPerformed,
	int32& OutNumberOfAsyncLosCheckRequested,
	float& OutSightStrength,
	int32* UserData,
	const FOnPendingVisibilityQueryProcessedDelegate* Delegate
)
{
	FVector SightTargetLocation = GetMesh()->GetSocketLocation("head");
	FHitResult HitResult;

	bool HadBlockingHit = GetWorld()->LineTraceSingleByChannel(
		HitResult,
		Context.ObserverLocation,
		SightTargetLocation,
		ECC_Visibility,
		FCollisionQueryParams(FName("Name_AILineOfSight"), false, Context.IgnoreActor)
	);

	AActor* HitActor = Cast<AActor>(HitResult.GetActor());

	if (!HadBlockingHit || (IsValid(HitActor) && HitActor->IsOwnedBy(this)))
	{
		OutSeenLocation = HadBlockingHit ? HitResult.ImpactPoint : SightTargetLocation;
		OutNumberOfLoSChecksPerformed = 1;
		OutNumberOfAsyncLosCheckRequested = 0;
		OutSightStrength = 1;
		return UAISense_Sight::EVisibilityResult::Visible;
	}

	OutNumberOfLoSChecksPerformed = 1;
	OutNumberOfAsyncLosCheckRequested = 0;
	OutSightStrength = 0;
	return UAISense_Sight::EVisibilityResult::NotVisible;
}

```

And this is my class definition and includes you need, I think:  
.h

```auto
#include "Perception/AISense_Sight.h"
#include "Perception/AISightTargetInterface.h"
#include "CPP_CharacterMaster.generated.h"

UCLASS()
class MYSTUFF_API ACPP_CharacterMaster : public ACharacter , public IAISightTargetInterface
...
virtual UAISense_Sight::EVisibilityResult CanBeSeenFrom(
	const FCanBeSeenFromContext& Context,
	FVector& OutSeenLocation,
	int32& OutNumberOfLoSChecksPerformed,
	int32& OutNumberOfAsyncLosCheckRequested,
	float& OutSightStrength,
	int32* UserData = nullptr,
	const FOnPendingVisibilityQueryProcessedDelegate* Delegate = nullptr
) override;

```

Hope this helps you out!

---

<div class="post-metadata">

**Author:** ![rennaked](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@rennaked](https://forums.unrealengine.com/u/rennaked)\
**Post date:** [February 12, 2025, 4:59pm UTC](https://forums.unrealengine.com/t/using-ai-perception-sight-to-target-head-in-ue5-3/1685932/7 "2025-02-12T16:59:39Z")

</div>

Its been a few months since you commented but for the help of others in the future.

The reason you can’t find the “Perception” module is because it needs to be included as a dependency in your build.cs file. It should look something like this

```auto
	PrivateDependencyModuleNames.AddRange(new string[] { "AIModule" });	

```

Also for some reason i had to include the entire path not just “Perception/AISightTargetinterface.h” like this "#include “Runtime/AIModule/Classes/Perception/AISightTargetInterface.h”

---

<div class="post-metadata">

**Author:** ![dreedmonroe](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@dreedmonroe](https://forums.unrealengine.com/u/dreedmonroe)\
**Post date:** [April 4, 2025, 1:01am UTC](https://forums.unrealengine.com/t/using-ai-perception-sight-to-target-head-in-ue5-3/1685932/8 "2025-04-04T01:01:39Z")

</div>

I’m having the same problem as @Deathglaze. When I compile my custom character in Visual Studios, I get the error "cannot open source file “Perception/AISightTargetInterface.h”

I took the advice of @rennaked and tried changing the “AI Module” location in both: PublicDependencyModuleNames AND PrivateDependencyModuleNames. I also tried using the full include path (“Runtime/AIModule/Classes/Perception/AISightTargetInterface.h”) but that resulted in even more errors:

```auto
cannot open source file "AISightTargetInterface.generated.h"
cannot open source file "GenericTeamAgentInterface.h"
cannot open source file "Perception/AISense.h"
cannot open source file "AISense_Sight.generated.h"

```

All help is truly appreciated.

---

<div class="post-metadata">

**Author:** ![ViniCordeiro](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/vinicordeiro/32/486579_2.png) [@ViniCordeiro](https://forums.unrealengine.com/u/ViniCordeiro)\
**Post date:** [September 20, 2025, 1:48pm UTC](https://forums.unrealengine.com/t/using-ai-perception-sight-to-target-head-in-ue5-3/1685932/9 "2025-09-20T13:48:09Z")

</div>

This helped me out a lot, i was having problems, didn’t matter what i changed, it would not detected the character, now it works. thanks very much The only thing I did was add variable to be used on the socket, so i can select which socket on the editor

```cpp
// Header file .h
/**
 * Socket to use as a detection point based on the location of the socket
 * @example: the head or neck of the character, the neck basically is kind acceptable as it won't be "too up" so the AI doesn't see up walls 
 */
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite,Category=Perception)
FName DetectionSocketBoneName { FName(TEXT("neck")) };
	

// Source file .cpp

UAISense_Sight::EVisibilityResult ATemplateCharacter::CanBeSeenFrom(const FCanBeSeenFromContext& Context,FVector& OutSeenLocation, int32& OutNumberOfLoSChecksPerformed, int32& OutNumberOfAsyncLosCheckRequested, float& OutSightStrength, int32* UserData, const FOnPendingVisibilityQueryProcessedDelegate* Delegate )
{
	FVector SightTargetLocation = GetMesh()->GetSocketLocation(DetectionSocketBoneName); // <--- added a socket here
	FHitResult HitResult;

	bool HadBlockingHit = GetWorld()->LineTraceSingleByChannel(
		HitResult,
		Context.ObserverLocation,
		SightTargetLocation,
		ECC_Visibility,
		FCollisionQueryParams(FName("Name_AILineOfSight"), false, Context.IgnoreActor)
	);

	AActor* HitActor = Cast<AActor>(HitResult.GetActor());

	if (!HadBlockingHit || (IsValid(HitActor) && HitActor->IsOwnedBy(this)))
	{
		OutSeenLocation = HadBlockingHit ? HitResult.ImpactPoint : SightTargetLocation;
		OutNumberOfLoSChecksPerformed = 1;
		OutNumberOfAsyncLosCheckRequested = 0;
		OutSightStrength = 1;
		return UAISense_Sight::EVisibilityResult::Visible;
	}

	OutNumberOfLoSChecksPerformed = 1;
	OutNumberOfAsyncLosCheckRequested = 0;
	OutSightStrength = 0;
	return UAISense_Sight::EVisibilityResult::NotVisible;
}

```
