# What is the most efficient way to trigger an action based on proximity?

I am developing a 3rd person platformer that entails a grapple hook system.

This grapple hook system relies on a series of 'grapple-able' points.

I would like these points to shine every so often.

These points will shine when:

• Within the player's view cone AND
• Within a certain distance of the player.

This Grapple object class will contain the method Shine() to trigger the effect.

Initial research

My initial research was based upon using a cone trace from the player for these Grapple objects, then in each one found, calling the Shine method.

I could then tweak the cone's angle and height to better represent the view cone.

Problems

Unreal by default does not have a cone trace, and I'm not sure if using a multibox trace followed by an angle check is the most efficient solution.

• It sounds like what you really want is a frustum check, not a proximity check or cone check. – DMGregory Sep 5 '19 at 11:33
• @DMGregory that's exactly what I want. Those are more mathematical to do as well. Please excuse my lack of expertise, I haven't programmed in 3 months. Are there preferred ways to do this in Unreal? – Natalo77 Sep 5 '19 at 11:49
• Have you observed any measurable performance impact from the way you're doing it now? Maybe it's fine as-is, and your time is better spent on other parts of your game. – DMGregory Sep 5 '19 at 11:51
• @DMGregory Well, I haven't developed a way to do it yet, and I'd rather go about it the best way. Am i ivory towering too soon? – Natalo77 Sep 5 '19 at 11:57
• Ah, it sounded like you'd tried a box first, then narrowing it with an angle. The thing is that game developers don't necessarily know or care what the "best" way is — we don't typically do proofs of optimality as part of our process. All we know is "solution X was good enough for our needs on game Y". When something isn't"good enough", we profile to identify where the problem is and try to fix that problem. I'd say these checks are unlikely to be what determines your game's performance, so I'd lean toward the simplest way you can try first, then measure to see if there's a problem to solve. – DMGregory Sep 5 '19 at 12:00

You actually have 2 ways of doing the frustum check:

1) Every scene component (or most fo them) has a property WasRecentyRendered() -- so if it was recently rendered - they are 99% in the camera frustum.

2) You can manually check the frustum with something like described here: UE4_AnswersHub_Link

To make it all performant you can trigger some object-sphere-trace to get a subset of close enough points - and then sieve them through the frustum/WasRecentyRendered queries.

Instead of a cone check you can use a frustum check.

If there is no built-in method for that you can do this by multiplying the vec4 position you want to check with the MVP matrix as you would in the vertex shader and then checking that the point is within the [-w,w] cube (you may need to take [0,w] for z if you use have a 0 to 1 depth).

You can move the virtual camera backwards a bit to allow objects on the edge of the screen to also shine when the control point is just off screen but the shine would still be on screen. And the far parameter of the perspective matrix can be adjusted to implement the range check.