How to determine if an actor is on screen?

What is a good method of determining if an actor is located on screen? Essentially when the player presses a button the game stops and they can select an enemy from an array to perform an action on. I’d like to remove any enemy that is not currently on screen from that array, or not put an off screen enemy in that array in the first place.

1 Like

Every scene component holds an “Is Visible” bool that is updated during rendering. You could iterate over all your enemy actors and all their scene components to sort out those being visible. However this is probably a slow operation. Maybe you could try to improve performance by first using BoxOverlapComponents with a “bigger than screen” box to sort out those that are really far away and just iterate over the rest.

Marc

UMG widgets have functiona that can twll you if a certain point in the world is on the screen. You can use your actors locations and get that information. You can even check for more than one point on the same actor (the body maybe offscreen but the hands or weapons are on screen) by getting mesh socket (or bone) locations.

Hello,

You can try to use this setup, which will check to see if the object in question is in view. If you’re trying to check for multiple objects, you can replace TestCube with a foreach loop that will run through each of the objects you’d like to check.

Here is the setup:

18 Likes

That worked perfectly thank you very much.

If you want to check if an actor is visible at the moment, there are two downsides with this approach:

  1. This blueprint checks only if the origin of the actor is within the screen, so it may not work with large actors, for example.
  2. The actor may be occluded by others and it is still on screen, but, if one of the occluding actors is opaque, the actor in question will be invisible at that moment.

To solve each one downside, you could:

  1. Treat the object as a box and check if at least one of the 8 vertices is within the screen, using the Sean’s blueprint as base and GetActorBounds node.
  2. Use the node WasRecentlyRendered to check if the component was occluded or not.
8 Likes

could you show an example blueprint for problem 1? I’m not sure how to use GetActorBounds node here.

how would i get the number of actors that are in the viewport?

This thread is a bit old and maybe some of my implementation wasn’t available at the time. Also, I should specify this is really only good for detecting one actor not multiples, but in my case that was exactly what I needed. I think this is a fairly efficient method and it is working beautifully for me. I am using a ray cast at a starting point away from my main pawn and then casting to a specific distance out to see if it hits an actor. Please excuse some of the blueprint look I had to squeeze it down to be able to fit in one image. I did all of this as a prelude to getting the highlighting of actors working, the first obvious step being to detect the actor in the view port. Please note this is currently rendering the ray cast line, you can eliminate this by setting the draw debug type to ‘None’.

Basically how it works is you call this as a function (I am doing it after a movement of the player controller) and it will get the currently controlled pawn, find the forward vector and cast a ray straight forward for a specified distance. The two multipliers after the ‘Get Forward Vector’ call is what controls these parameters. I could hook these values out to the function parameters in order to control the ray cast distance but I didn’t do it for testing. You have to have the collisions set up properly for your actors. In this case I used WorldDynamic to filter, so for this setup it wont register a static mesh like walls or doors, it will only detect actual actors. For this case, my actor collision preset was set to OverlapAllDynamic.

The last part of it is to store if an actor was hit and which one was hit. I then call a custom event after this function call to trigger another piece of code in the controller to process the hit.

Thanks works like a charm!

will this work for VR do you know?

Here is the code for anyone interested. Is Mesh On Screen posted by EXODST | blueprintUE | PasteBin For Unreal Engine

1 Like

I previously posted the blueprint and someone let me know two verts were the same and i have since updated it. Now it should work flawlessly. The idea is to check if any bounding vertices are on screen instead of checking just the origin. Hopefully someone finds this helpful :slight_smile:

Vertex1 = BoundsMin
Vertex2 = (BoundsMin X, BoundsMin Y, BoundsMax Z)
Vertex3 = (BoundsMin X, BoundsMax Y, BoundsMin Z)
Vertex4 = (BoundsMin X, BoundsMax Y, BoundsMax Z)
Vertex5 = (BoundsMax X, BoundsMin Y, BoundsMin Z)
Vertex6 = (BoundsMax X, BoundsMin Y, BoundsMax Z)
Vertex7 = (BoundsMax X, BoundsMax Y, BoundsMin Z)
Vertex8 = BoundsMax

BoundsMin = Origin-BoxExtent
BoundsMax = Origin+BoxExtent

I would also like to add that it may be very useful to add to a blueprint function library to use across all of your blueprints.