Change height of perception source?

I’m using AI sight, and just noticed the enemies lose sight of the player character when I get behind short boxes.

Apparently this is due to the fact that the enemy traces a line from their head to the player character’s origin, which is at its waist.

Can I somehow change this line trace to end at the player’s head as well? I can’t seem to change the position of the character’s capsule component, or change the end point on either the enemy’s AI perception component, or character’s stimuli source.

Hi @fael097 ,

You can implement the AISightTargetInterface on your character to allow greater control over Line of Sight checks. I’m not looking at UE5 code presently so it may have changed since 4.27.

By overriding IAISightTargetInterface::CanBeSeenFrom you can do any number of LoS checks against different locations on your character and also provide a strength based on your information. Obviously be mindful of your performance budget.

That information is used in AISense_Sight::Update to register the stimulus.

class AIMODULE_API IAISightTargetInterface
{
	GENERATED_IINTERFACE_BODY()

	/**	
	 * The method needs to check whether the implementer is visible from given observer's location. 
	 * @param ObserverLocation	The location of the observer
	 * @param OutSeenLocation	The first visible target location
	 * @param OutSightStrengh	The sight strength for how well the target is seen
	 * @param IgnoreActor		The actor to ignore when doing test
	 * @param bWasVisible		If available, it is the previous visibility state
	 * @param UserData			If available, it is a data passed between visibility tests for the users to store whatever they want
	 * @return	True if visible from the observer's location
	 */
	virtual bool CanBeSeenFrom(const FVector& ObserverLocation, FVector& OutSeenLocation, int32& NumberOfLoSChecksPerformed, float& OutSightStrength, const AActor* IgnoreActor = nullptr, const bool* bWasVisible = nullptr, int32* UserData = nullptr) const
	{ 
		NumberOfLoSChecksPerformed = 0;
		OutSightStrength = 0;
		return false; 
	}
};
1 Like

Hey, thanks.

I’m not really familiar with C++, but if you could point some directions on how to do that or where to put that code, I’m comfortable trying it

Hi @fael097 -

The documentation for interfaces is here.

I don’t have time to verify it at the moment but it will probably have to be implemented on the class which contains the stimuli source of the target that the AI agent is observing. In your case, the player character class.

Just from looking at it you’ll have to probably do something like the following. Again, this is completely untested code it’s just a guideline to help you get started.

Include the interface header in your character’s .h file

#include "Perception/AISightTargetInterface.h"

Inherit from the interface for your character class

class YOURMODULE_API AMyCharacter : public ACharacter, public IAISightTargetInterface

Override the interface function in your character class’ .h and .cpp files

MyCharacter.h

virtual bool CanBeSeenFrom(const FVector& ObserverLocation, FVector& OutSeenLocation, int32& NumberOfLoSChecksPerformed, float& OutSightStrength, const AActor* IgnoreActor = nullptr, const bool* bWasVisible = nullptr, int32* UserData = nullptr) const override

MyCharacter.cpp

bool AMyCharacter::CanBeSeenFrom(const FVector& ObserverLocation, FVector& OutSeenLocation, int32& NumberOfLoSChecksPerformed, float& OutSightStrength, const AActor* IgnoreActor /** = nullptr */, const bool* bWasVisible /* = nullptr */, int32* UserData /* = nullptr */) const
{
	// For each LoS check you want to perform

		// Perform the check

		// Increment NumberOfLosChecksPerformed

		// Increase the OutSightStrength based on a heuristic you determine

		// Check to see if you can early out of the loop if you decide the strength check is high enough

	// Return true/false if you determine the actor can be seen
}

I don’t think the gameplay debugger will capture and display the information regarding your additional LoS traces since they are being done in your character class, but I would expect it to at least capture the strength.

If you wanted to display that additional trace information in the visual debugger I think you’d either have to create a new sense config from UAISenseConfig_Sight and override the DescribeSelfToGameplayDebugger function, or add another FGameplayDebuggerCategory and register it.

Godspeed developer