Getting Objects Inside a Trigger (without OnTriggerStay)

Hello everyone, I’ve got a situation where Player/NPC boats are being instantiated, and I would like for them to do a check when created that looks for any objects of interest for each ship gun. The trouble is that there is a chance that potential target objects will be within a FOV trigger mesh that each gun has, thus bypassing OnTriggerEnter/Exit.

I’ve considered a few options:

Ideas I’ve considered:

  1. Lists of Objects

My choice so far, create a list for NPC/Player objects and use a coroutine for each gun to periodically run through and choose a target from that list. Would need to make sure some sort of logic tests (followed by bounds.contains with the point) are done to minimize guns from checking against obviously impossible target objects.

  1. OnTriggerStay:

Would require a bool or other simple test to prevent it from doing anything after a very brief initial amount of time to acquire objects when instantiated. Would need to make sure that the same objects are not added multiple times. And even with basic tests, if a lot of targets are inside a FOV mesh, that could still lead to lots of unnecessary work each physics update.

  1. Spherecast / Capsulecast:

Would need to make sure objects don’t get added multiple times, the shapes really don’t match the FOV mesh very well, spheres/capsules may miss objects or hit unwanted objects.

  1. Bounds.contains:

Only takes a vector3 point, could repeat the test multiple times but at that point I’m already more/less doing idea 1.

That is where am at this point. If I’m not thinking of another way of doing this, please let me know. Thank you for reading.

i would say a spherecast is a good option, you can add a boolean to each object that you are detecting, and set it to true when its detected, then you can add code to only add the object if the boolean is false, that will avoid multiple detections, also you can set up collision layers to ignore certain objects.

layermask is important, because its not just a case of typing in the layer number on the spherecast (as i thought initially :P)

You can try this method if you want to :

If you scale the FOV Trigger(or a similar duplicate) from very very small to the size you want then it will detect all the objects within it provided you have a rigidbody on the FOV Trigger and the objects you want to detect are setup in a way that is reciprocal to this. Do a test and output the names or tags of the objects that are detected to make sure it is picking up everything that you want it to.