x


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.

more ▼

asked Feb 25 '11 at 01:07 AM

Patyrn gravatar image

Patyrn
542 49 54 64

just a hint (maybe)... instead 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.

Feb 28 '11 at 03:48 PM Cawas

Probably a good idea.

Feb 28 '11 at 06:17 PM Patyrn
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Feb 25 '11 at 01:30 AM

Bunny83 gravatar image

Bunny83
45.2k 11 49 207

I'm writing in javascript though. JS doesn't have a List collection.

Feb 25 '11 at 02:22 AM Patyrn

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#.

Feb 25 '11 at 05:25 AM Bunny83

Is this not a spherical shell instead of a donut shape?

Feb 25 '11 at 07:32 AM Herman Tulleken

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

Feb 25 '11 at 10:56 AM Bunny83

@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

Feb 28 '11 at 03:59 PM Cawas
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3458
x1360
x36

asked: Feb 25 '11 at 01:07 AM

Seen: 1759 times

Last Updated: Feb 25 '11 at 01:07 AM