IAISightTargetInterface is used to customize how AISense Sight checks the visibility of an actor.

Usage

This interface should be implemented by the actor that is being seen - in other words, for example if you want to customize how the player character is checked, you should implement this interface on the player character actor.

To implement the interface, you need to implement the CanBeSeenFrom function. A basic implementation is shown below.

Note: The implementation example shown below is still functional as of Unreal Engine 5.2, but there’s a new override you should use instead:

virtual UAISense_Sight::EVisibilityResult CanBeSeenFrom(const FCanBeSeenFromContext& Context, FVector& OutSeenLocation, int32& OutNumberOfLoSChecksPerformed, int32& OutNumberOfAsyncLosCheckRequested, float& OutSightStrength, int32* UserData = nullptr, const FOnPendingVisibilityQueryProcessedDelegate* Delegate = nullptr)

It works mostly the same, but check the IAISightTargetInterface.h file for details on how the EVisibilityResult is intended to be used.

AMyCharacter::AMyCharacter()
{
	//This gets the sight trace channel that's configured in project settings
	DefaultSightCollisionChannel = GET_AI_CONFIG_VAR(DefaultSightCollisionChannel);
}
 
bool AMyCharacter::CanBeSeenFrom(const FVector& ObserverLocation, FVector& OutSeenLocation, int32& NumberOfLoSChecksPerformed, float& OutSightStrength, const AActor* IgnoreActor) const
{
	FHitResult HitResult;
	//check against location first - this is about in the center of the character
	bool IsHit = GetWorld()->LineTraceSingleByChannel(HitResult, ObserverLocation, GetActorLocation()
        , DefaultSightCollisionChannel, FCollisionQueryParams(SCENE_QUERY_STAT(AILineOfSight), true, IgnoreActor));
 
	NumberOfLoSChecksPerformed = 1;
	
	if(!IsHit || HitResult.GetActor() != this)
	{
		//if we don't see the center of the character, look for the camera location which is where the
		//player's point of view is, which allows the enemies to see the player in a more reasonable fashion
		//if the body is partially obscured
		IsHit = GetWorld()->LineTraceSingleByChannel(HitResult, ObserverLocation, Camera->GetComponentLocation()
			, DefaultSightCollisionChannel, FCollisionQueryParams(SCENE_QUERY_STAT(AILineOfSight), true, IgnoreActor));
 
		NumberOfLoSChecksPerformed++;
	}
 
	OutSeenLocation = HitResult.Location;
	OutSightStrength = 1;
	
	return IsHit && HitResult.GetActor() == this;
}
 

The DefaultSightCollisionChannel value assigned in the sample constructor is the default sight channel which can be adjusted from Project Settings under the AI settings.