- Home /
resizing spherecast radius over distance?
Hi, I am using a spherecast to detect if there is something in front of me, but only want it to "see" anything within a certain screenspace. Doing this works fine, but doesn't when too close or too far from the object, as I am checking in 3D, not in 2D screenspace.
So what I'd like to do is change the sphere's radius according to the distance it travels. I am not sure whether this is possible or not, or if I should simply start over.
Any input is highly appreciated!
Thanks in advance,
~Nick
Answer by senad · Jul 16, 2012 at 02:34 PM
I do not think you can achieve this with the sphere cast, because it will search in a constant tunnel around the search ray. This tunnel will become smaller in screen space with distance.
However, if you shoot some rays from the eye point through the bounds of your search area (in screen space), those rays will drift apart when moving further away. This way, your searching volume will always have the same size in the screen. Now you will have the problem that as the rays divert from each other, there is more room to miss objects, so maybe you will need in-between sampling rays, which is bad for performance. How well this works depends a lot on your geometry.
Another way would be to project all objects in the view frustum onto the viewing plane and check in screen space if they intersect with your search area. However, this only makes sense performance-wise, if there are not many objects.
There might be also easier solutions. And I whish I could draw it, it would be much easier to understand. :D
Hmm didn't think about sending a ray from the middle to the bounds, tho this is basically the same thing as I was trying to achieve using a growing spherecast of some sort. But I guess I'll be better off just getting a list of visible objects, then checking if they are within my searching area. might not be the best way to do stuff, but I doubt the way I'm doing it now is any better, haha!
Actually, setting up a second camera to test against a smaller view frustum might be what you need. That is if you can accept a rectangular seach area.
Answer by senad · Jul 16, 2012 at 02:37 PM
Oh and another idea would be to render all your geometry in an extra render pass with the stencil buffer enabled and set up the stencil buffer to only render the geometry which will lie in your defined search area.
Answer by flaviusxvii · Jul 16, 2012 at 02:48 PM
I think you are talking about testing against the view frustum.
Here are several ways to do this.
http://answers.unity3d.com/questions/8003/how-can-i-know-if-a-gameobject-is-seen-by-a-partic.html
yes, I suppose I was sort of trying to create a fake view frustum for my spherecast if you will. had a look at the link, and I think I'll figure something out using the suggestions in there, thanks! :D
Answer by MikeTheMonster · May 06, 2020 at 11:49 AM
Late anwser , but maybe this helps someone. You can use SphereCast and thereafter check the angle of the returned collider to see if it would have fit inside a cone. The radius of the sphere should be the radius of the cone's base and the distance of the cast, the height of the cone. Use Mathf.Asin(radius/height) to determine the angle you want to check and Vector3.AngleBetween to get the angle of the point relative your gameobject.
I think it would be cheaper to have a capsule collider and then use the suggested code within the OnTriggerStay event. Then you would need to cast a ray to the obect to check for obstruction.
This post was inspired by walterellisfun's ConeCast extension method. https://github.com/walterellisfun/ConeCast