Checking whether the player cannot see the AI

I’ve seen some questions and tutorials about the AIs sensing things (pawn sensing component, line of sight), but I have a somewhat reversed problem - I want the AI to perform some action if the player cannot see the AI. Think “student making faces at the teacher if the teacher cannot see him”.

The distinction is important - I don’t care if the AI can see the player, I need to know if the player can see the AI. The tutorials also usually describe how to do an action when the AI spots something, while I need an event that happens when the AI is not visible (I’m fine with checking for it every delta seconds). It is also crucial to check that no part of the AI mesh is visible to the player, and it should work with various screen resolutions (don’t just test 180 degrees around the player, but check what is actually drawn on the screen - wider screens can show a larger field of vision AFAIK). EDIT: Take barriers (i.e. walls) between the player and AI into consideration.

Extra cookie points: (I didn’t look into this yet) Test if the AI isn’t visible in mirrors either.

EDIT: Possible simple solution could be to check if the AI mesh is being rendered on screen, but I couldn’t find anything about this. Maybe I’d need a custom shader?

Hey,

your problem is quite similar to what I have seen earlier here: https://answers.unrealengine.com/questions/177899/two-player-top-down-stop-players-running-off-scree.html

As IanBreeg explained, you’ll need some vector math. There currently isn’t any node that can check what the player can see, so you’ll have to do it manually. You’ll effectively create a set of coordinates that make up the boundaries of what the player can see. You can then use those to check whether the AI is within those boundaries.

I can try and go into a bit more detail in a bit. Just gotta head home first.

Thanks, that might be part of the solution. However, I also need to find out if there are any objects between the player and AI that are hiding it (i.e. the AI is behind a wall and the player cannot see it), which adds even more complications…I really hope there is some simpler way to do this than computing it all by myself.

To check if there are objects between the player and the AI you can do a simple line trace on the visibility channel, starting from the player position and ending at the AI position. Ignoring both the player and the AI itself. If it hits something, then there is something in the way. This is where it gets complicated again though, as you would need to take in that objects size and see if it’s big enough to cover the whole AI, or whether the AI is still partially visible, for example if it’s a small object, like a chair.

It’s all possible, but quite complex. As far as I know there isn’t a simple way, unfortunately.

Why don’t you just have the player send a signal to the ai? The player can see the ai = cast to ai and turn bool on. When the player stops being able to view the ai cast to the ai again and turn bool off. Just a thought.

What do you mean by “send a signal”?

Communicate to the Ai blueprint? From the player rather then the Ai trying to figure out if the player is looking at him on his own.

Well, yes… The question was HOW to check if the player can see the AI :stuck_out_tongue: As far as I know there is no node that can do that.

I don’t think there’s any default functionality for this. I could see using some math to simulate the view frustum’s angles and see if the actor’s bounds are inside them.

A simple example could just be a dot product of the camera forward vector versus the vector formed by the camera + AI. If the normalized result is greater than the equivalent angle of the field of view (with aspect ratio taken into account) then he is off camera.

This is all very high level. If you want to be inaccurate and just say the AI responds to being behind the player then its way easier and you just look for a negative result from the dot product.

Cutting lots of corners, but it works well enough.

I basically used a combination of line trace and a giant sphere component attached to my Character that acts as my vision range. If the AI is in my vision range, and my line trace hits him, I can see him. If he is in my vision range, but the line trace does not hit him, I cannot see him. If he is not in my vision range and the trace hits him, I cannot see him. You could make a trapezoid mesh to better fit the field of view then the sphere component if you want more precision. The trapezoid would pretty much give you as good as it gets results.

Maybe this will be good enough for you. If you want more in depth details on how the blueprint works, let me know and I’ll make a quick video tutorial or something.

Ah, added an answer. I guess a little “none-traditional” logic was needed here, my speciality :stuck_out_tongue:

This is probably what I’ll end up implementing if I don’t find any existing functionality, but I really hoped there was some function that would do the math for me :frowning:

That’s…a very inventive solution. I might do that for the prototype, as it’s easier to implement than doing vector math. The problem is that it doesn’t take screen ratio into consideration. I guess it also means that it cannot recognize things that are further away, but that could be worked around by not allowing the AI to do the action until it’s within that distance. Thanks for the video, I’m creating a horror game and now I can see that it’s creepy even when using a silly looking AI :slight_smile:

haha yeah about the creepy thing, it was pretty funny :stuck_out_tongue: I figured it was for something Horror like.
With the sphere component I can see the limits, but with a trapezoid, you could scale it on both axis on level begin using the get viewport size node (although only one axis is probably relevant). And the trapezoid could also potentially go as far as the eye can see(though I like the limited vision range via fog or something for creep factor). I was also thinking that if you had many actors that needed to behave that way, the Ai could cast the trace rather then the player. The advantage of this method is that you can easily check if an actor(s) is or isn’t within the view field of the player via the overlap/end overlap events.

Well that’s it for me :slight_smile: Glad it brought some kind of insight :stuck_out_tongue: And good luck with your project! I hope I get to try it some time, I like those creepy games hehe

How goes your project? Do you mind accepting an answer or closing your thread if you found another solution for tracking purposes, thank you:)