Attach AI Sight to Pawn Head? (Perception)

I’m trying to set up AI Perception. How do I attach the field of view cone to the head of my character? Currently my character is looking around due to his animations, but the sight cone is still pointing forward (in the general Pawn direction). Could someone explain how to set this up?

Perception is not a scene component, doesn’t have it’s own transform, you cannot attach it to anything, it have no representation in the world. I think this been rigged to look forward only, and if you want something else you are going to design your own UAISense, or at least extend upon this one, Btw it is recommended to set up perception in the AIController so you won’t have hopes to use it for such customized eye sights :slight_smile:

You can maybe just do collision (overlap) checks regularly and stream these informations to your behavior tree for evaluation. Think of an array of target Actors a Cone mesh have overlapped with, then check for line trace too and make sure they are not behind obstacles (eg a wall). This will be more expensive (cpu) compared to the use of an optimized (but limited functionality) sight component, however gives you room to design your own system.

Thanks for the reply.
Well that’s too bad, and kind of a major flaw in the mechanic if you ask me.
Even in my characters idle animation it will look left or right every now and then, and not having the eyesight move with that kind of defeats it’s entire purpose.

Is there no way to change this in C++? There must be a place where the location and rotation is set of this component.

Yes of course you can change many things, and if i remember correctly i have seen cases where the sight component was simply accessing the the owned pawn in order to grab some transform informations (you maybe will find a pawn’s forward vector somewhere). Changing it is sounds like a promising idea, tho i’m not sure if you wish to roll your own engine build for such modifications, but even then what is the guarantee it would work in any situations you are just throwing at it. I’d go with the cone overlap and line traces idea, that’s not that bad performance wise, just make sure not overusing it for 10k enemies and such crazy things :slight_smile:

I would recommend just sticking to the pawn sensing component for sight radius and transform, and using ai perception in conjunction for other things. You could always just keep the pawn sensing update interval to 0.5 seconds and I doubt it would eat up more cpu time than ai perception with EQS system? Or use a commander a.i. that senses and sends orders/info to the other enemies as a group?

I know this is a really old thread, but just in case anyone was on here looking for the answer, it IS possible in way.

Create a pawn actor with the pawnsense in it. Attach this actor into the blueprint you want as a child actor. This can be attached to the head of your ai character mesh an it will move accordingly.

Bit more code inside the pawnsense actor to send a message to the parent actor and it will work perfectly.

I use this method for sentry guns as it’s more realistic than line tracing.

1 Like

You can also add the AIPerception component to the AI controller and attach that to the pawn. Less blueprints.

How do i for aiperception? it doesn’t seem to work

I tried attaching a pawn containing an AIPerception component to a socket. When the socket (or the whole skeletal mesh) rotates, the Perception debug cylinder only rotates in the z-axis. In other words, the NPC can look left and right, but not up and down.

I don’t want to use PawnSensing, it’s too basic.

Here’s the function that determines the Sense’s orientation (from AIPerceptionComponent.cpp)


void UAIPerceptionComponent::GetLocationAndDirection(FVector& Location, FVector& Direction) const
{
    const AActor* OwnerActor = Cast<AActor>(GetOuter());
    if (OwnerActor != nullptr)
    {
        FRotator ViewRotation(ForceInitToZero);
        OwnerActor->GetActorEyesViewPoint(Location, ViewRotation);
        Direction = ViewRotation.Vector();
    }
}


Thankfully AActor::GetActorEyesViewPoint is a virtual function, so you can override it for your pawn, but only in c++.

5 Likes

Hey i was searching for workaround and i found one by myself.
Just make arrowComponent as your root component of an actor and then set its location and rotation as desired at runtime (tick). Not sure if its bugless but works for my pawn with physics simulated movement in ue5. goodluck!

Thank you very much, this is the correct way to do it.

In you Pawn/Character’s header file, override GetActorEyesViewPoint:

virtual void GetActorEyesViewPoint(FVector& OutLocation, FRotator& OutRotation) const override;

And in the C++ file, I used a socket in the skeletal mesh that pointed forward X in the direction of sight:

void AEnemyCharacter::GetActorEyesViewPoint(FVector& OutLocation, FRotator& OutRotation) const
{
static const FName VisualSense = FName(TEXT(“VisualSense”));
const FTransform ViewTransform = GetMesh()->GetSocketTransform(VisualSense);

OutLocation = ViewTransform.GetLocation();
OutRotation = ViewTransform.GetRotation().Rotator();

}

Confirmed to work in UE5.3.2.

1 Like