- Home /
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.
2) 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.
3) 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.
4) 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.
So why don't you use SphereCast? I think it's the best option for this case.
Answer by bubzy · Dec 06, 2014 at 05:31 AM
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.
http://docs.unity3d.com/ScriptReference/Physics.SphereCast.html
http://docs.unity3d.com/ScriptReference/LayerMask.html
layermask is important, because its not just a case of typing in the layer number on the spherecast (as i thought initially :P)
@quansatthu and bubzy:
$$anonymous$$y concern on the spherecast is the mismatch between the shape of the sphere vs the shape of the FOV mesh. Granted, I could do a Bounds.contains after a object is detected...
Answer by MrSoad · Dec 09, 2014 at 05:15 PM
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.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Mouse click breaks trigger variable C# 1 Answer
A node in a childnode? 1 Answer
Loop Sounds when within trigger 1 Answer
Save Position script. 2 Answers