- Home /
Check whether something is inside a cone of raycasts
Here's what I have:
I'm wondering how I would check if an object is inside of the cone?
I don't understand. raycasts are for checking. in your case run 5 raycasts and check each for what you've hit.
Answer by iceberg · Mar 14, 2017 at 05:30 PM
Does it have to be raycasts? It will be easier to just use a cone shaped collider. There is a related discussion here: https://forum.unity3d.com/threads/raycasting-a-cone-instead-of-single-ray.39426/
Answer by loki-of-mute · Dec 23, 2019 at 09:32 AM
an even better solution - for some circumstances, e.g. if the list of possible objects to check is quite small and known in advance - is to check if the angle of your-transform-versus-targets-transform falls within a certain range, representing a cone. This can easily be done by calculating the arccos of the dot product between both transforms, or using Unity's Vector3.angle (https://docs.unity3d.com/ScriptReference/Vector3.Angle.html). When the angle is low enough to fit in a cone, you can do a single raycast to the object to figure out if it is effectively line-of-sight visible from the first transform's point. (to make sure there's nothing in the way, blocking the direct view of the target..e.g. a wall.) I use this technique to check if enemies are within a sniper's rifle scope view, and that works nicely. I know upfront which enemies to check for (since I have less than some 50 enemies in one scene), and it keeps the calculations very cheap.
Your answer
Follow this Question
Related Questions
What's with this absurd index array input? 1 Answer
(Scripting) Getting a circle of points in an array 1 Answer
How do I get this code to run specified amount of times 1 Answer
Change orbit while rotating. 2 Answers
Creating an UI GPS map 0 Answers