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:
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
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
#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!
