Custom Perception LineTraceSingleByChannel Issues

I’m currently trying to get enemies in my project to look at the player’s head bone instead of the default location used by the Sight Perception System so the player can crouch behind cover to hide, and the enemy will see the player over cover while the player is not crouched.

I found a couple guides online and have managed to get the target location working correctly, the enemy does look for the head socket now. But now the LineTraceSingleByChannel is not blocked by any of the level geometry any more. I’m really not sure what’s up, all the geometry it is ignoring is set to block the visibility channel, I have triple checked. It is still detecting a hit on the player I believe as the sight perception debug sphere is getting updated as I move around and is registering as a sighting.

Example Image:

Code:

If anyone can see what I’m doing wrong and can steer me in the right direction I’d be very very grateful!

Edit:
fixed up the missing scope variables in cpp file:
MyAISense_Sight.zip (5.5 KB)



Made a custom sight sense with modified ComputeVisibility

main change is a redirect of the target location variable to a characters head if the cast is successful.

FVector TargetLocation = TargetActor->GetActorLocation();

if (ACharacter* character = Cast<ACharacter>(TargetActor)) {
	TargetLocation = character->GetMesh()->GetSocketLocation("head");
	
}

h. and cpp file in zip. Just rename the _API in the header definition to your project name.

1 Like

Dang thanks! I changed the _API as mentioned and threw this in the source folder but when I tried to compile I got all kinds of errors. I’m using 4.27 not sure if that’s the problem it looks like you built this in 5. I also just kinda have no idea what I’m doing with C++ in general :sweat_smile:

Yep I made it in 5.3. I’ll try to do a rewrite for 4.27

Edit:

header and cpp file for 4.27 version
MyAISense_Sight.zip (4.5 KB)

Trace detection is working. So if you go behind a wall it will save the last seen location.
Sight is set in the sight config of the perception component just like in 5.3

1 Like

This is working great. You are an absolute legendary human being, Thank you so much!

You could probably also make the “head” in the cpp file an exposed FString UPROPERTY and expose it to the config if you want the enemy ai to look at different bones of the character. But this would probably require a custom sense config with exposed variables.

1 Like

That’s a really cool idea!I’m definitely going to try to get that working on my own. Thanks again for you help!

Hey sorry to bug ya again! I just tried a test package got the following error:

MyAISense_Sight.cpp.obj : error LNK2019: unresolved external symbol "protected: float __cdecl UAISense_Sight::CalcQueryImportance(struct FPerceptionListener const &,struct FVector const &,float)const " (?CalcQueryImportance@UAISense_Sight@@IEBAMAEBUFPerceptionListener@@AEBUFVector@@M@Z) referenced in function “pro
tected: virtual float __cdecl UMyAISense_Sight::Update(void)” (?Update@UMyAISense_Sight@@MEAAMXZ)
UATHelper: Packaging (Windows (64-bit)): C:\Users\User\Documents\Unreal Projects\MyFPScontroller 4.27\Binaries\Win64\MyFPScontroller.exe : fatal error LNK1120: 1 unresolved externals

The first time I tried packaging, it was actually successful but the perception wasn’t working and it crashed when I tried turning on the debug. Now I’m getting the above error when I try packaging or launching standalone. So confused. I haven’t altered the .h or .ccp file aside from the _API change required.

From googling the error it looks like something required might not be being included?

Seems it needed 1 more function (CalcQueryImportance) to pack. Even though it was a protected function in the parent class. Added it in and it packed correctly.

MyAISense_Sight_v3.zip (4.7 KB)

1 Like

Thanks once again so much, that fixed the packaging error and everything’s working as intended in the packaged build. Awesome stuff!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.