- Home /
Physics.OverlapSphere with a min radius?
So I'm trying to detect things within x radius and without y radius. A donut shaped area of detection. My plan is to use two OverlapSpheres and remove colliders from the small one from the collection the large one returns.
Is this the best way to do this? If it is, what's the best way to filter an array like that? There don't seem to be any helper functions like Array.Without.
just a hint (maybe)... ins$$anonymous$$d of calling it "x radius" and "y radius" it could be better to call 'em "`a` radius" and "`b` radius". small detail, but I think it generates lot less confusion while reading because x and y look like axis coordinates.
Answer by Bunny83 · Feb 25, 2011 at 01:30 AM
Well that's quite simple:
// C# and needs using System.Collections.Generic;
public static List<Collider> GetDonut(Vector3 pos, float InnerRadius, float OuterRadius)
{
List<Collider> outer = new List<Collider>(Physics.OverlapSphere(pos,OuterRadius));
Collider[] inner = Physics.OverlapSphere(pos,InnerRadius);
foreach (Collider C in inner)
outer.Remove(C);
return outer;
}
EDIT
Here the JS version:
function GetDonut(pos : Vector3,InnerRadius : float, OuterRadius : float) : List.<Collider>
{
var outer : List.<Collider> = new List.<Collider>(Physics.OverlapSphere(pos,OuterRadius));
var inner : Collider[] = Physics.OverlapSphere(pos,InnerRadius);
for (var C : Collider in inner)
outer.Remove(C);
return outer;
}
Well, the Name is not really good but it's just a sample ;)
This function returns a List<> but with .ToArray() you can convert it back to an array if you really want.
I'm writing in javascript though. JS doesn't have a List collection.
What? who said that? If you would talk about JScript you might know from websites you're right, but UnityScript is a totally different language. It compiles also on the mono platform like C#. The greatest differences between C# and UnityScript is the syntax. JScript also don't support inheritance (it use prototypes) but in Unity it does. In the end you can do quite everything in both languages but in JS it's often the longer way and not as "save" as C#.
Is this not a spherical shell ins$$anonymous$$d of a donut shape?
Sure :D but he described it as donut. If you really want some kind of a donut shape (only a kind of) just use this function from a OnTriggerEnter Event of a box collider that specifies the "cut-out" area. If you just need it global axis-aligned flat on the floor you can check the relative y from the center and exclude all that lays outside. As I said it's just a volume that have the general form of a donut but it isn't
@Patyrn you could have your C# file separated and still use that function within your JS code: http://answers.unity3d.com/questions/21676/is-it-possible-for-c-script-using-class-written-in-js