Sensing Invisible Pawns - BluePrints

I’m trying to use PawnSensing on my character to see AI characters in order to turn their visibility on and off. I have the pawn sensing working for OnSeePawn when the AI character is already visible, but when the AI character starts invisible, I cannot find a way to sense the pawn with OnSeePawn. I’m assuming this is by design, since you can’t see something that is invisible.

I’m still very new to UE4, and I haven’t quite gotten the terminology down. As such, my couple hours of forum searching has yielded me results that aren’t quite useful to me.

I’m using the TopDown BluePrint template. In the end result, I would like for all AI characters to load up invisible, and only be visible when they are within my character’s sight cone. The end result will not be like a boolean on/off. Rather, when in the cone they will be instantly visible, but when they leave the cone they will fade out and sound sensing will take over to indicate AI character direction relative to the player.

Could someone give me a hint or two about what blueprint tool would be best to achieve the “visible while inside sight cone” part of my part of my request?

I ended up reading about “traces” and figured, “Sweet, I’ll do a cone trace in front of me”. The problem I ran into was that there is no cone trace or collision node. No biggie, it just took a little guess and check, and I was able to have my system do a bunch of traces (1080) to be exact in front of my, in the shape of a cone, with every tick.

I then added a function/dispatcher to my AI pawn so that when the dispatcher is triggered, a retriggerable delay is fired off. After .5 seconds, the AI pawn will set its “Hidden” attribute to true.

For each tick, when one of my traces hits a Pawn, it calls the Pawn’s dispatcher, which tells the pawn to hide again in .5 seconds. Since the delay is retriggerable, every tick that the pawn is within the cone of traces, the delay starts over again at .5.

I don’t know if this is the most efficient way to handle this, but considering I am brand new to game programming and UE4 I was pretty proud of myself. :slight_smile:

I will say, when I first coded this, it was doing 100 swipes of this (108000 traces) per tick, and that ended up giving me about 1.2 to 1.5 SECONDS per frame, not FRAMES per second. hehehe

Seeing your own answer: Oh boy that is a lot of traces which will probably never hit the desired object.

I guess you could put a collision box/sphere on your character and then use the “OnComponentBeginOverlap”-Event to check if your AI Pawn just overlapped/entered your collision box/sphere. You can adjust the size of the box or radius of your sphere if you need a “better” or “weaker” sensing. The collision box will pretty much listen to what happens and will only fire the event if it is really needed.

I don’t know if the collision will hit when you want to set it to “insivible” or “hidden”, you have to test it, but you could also let the pawn stay “visible” but make it disappear. I assume that you want to give your AI Pawn a static mesh as a visual representation and therefore it will have a material. You can give this static mesh a material which is totally masked and therefore not opaque. You would be able to see through the mesh and let the collision box/sphere react to the pawn, you don’t even need to switch the visibility off or make it hidden I would say.

I was going to do that, but I couldn’t get a cone affect that I wanted. The collision was not firing on “Invisible” objects, but was firing on “Hidden” objects. So toggling the “Hidden” attribute is what I went after. Eventually I will have a custom function that does a fade effect.

Then, I figured that I would just test the angle of the character in reference to my forward vector to see if it was within certain parameters (angle, distance, etc.). The issues I ran into:

  1. I wasn’t exactly sure how to calculate the angle of the collided object in reference to my forward vector (didn’t know enough at the time, but probably could figure it out now that I put so much work into these traces).
  2. The collision was hitting the object even when there was something in between my character and the pawn that should have obstructed the view. I could probably have done more specific traces at that point to test if I should be able to see the pawn before making it unhidden, but I didn’t even know about traces and their power till I finally got this figured out.
  3. The on-collide function was only firing once. So, even if I did figure out how to find the angle between my forward vector and the object, it would only do it once and not update if the pawn enters my visible range after on-collision.

In the future, I plan on this same detection working for multiple AI pawns, traps, key items, etc. and am alright with my method for now. However, knowing what I do now, and after seeing your input, I will come back around to optimize my “invisible detection” logic later.

I really like your answer and am marking it as a solution as well because it uses built in logic to trigger, cuts WAY down on CPU because of not doing unnecessary traces, and offers more than just this one use case because I could use it for detection in all directions and do what I want with the detection based on the situation.