Closest object?

Hey, all,

Come up with a bit of a problem I don’t seem to be able to get over, and I was wondering if any of you could help?

I have a group of objects, all parented together (that bit isn’t important), none of which have colliders or rigidbodies attached (for performance purposes).

They are covered by a “hull” collider (a sphere), on which they all sit pretty close to the edge.

The objects within the collider are of different sizes - some of them significantly bigger than the other objects.

What I want to do is detect a collision on the Hull, and then choose and perform functions on the closest object to the hit point.

Finding the hit point is a breeze, but due to the differences in size of the objects within the hull collider, I can’t seem to pick the correct object from within all the time.

I do a simple radial distance check on every object that’s within the hull, like so:

     Distances[i] = Vector3.Distance ( ObjectPos[i], CollisionPoint );

The problem being that this measures the distance to the centre point of the object, rather than the closest point on the object, which means that, if I have an impact point which is on, but close to the edge of a large object, which is adjacent to a small object, the small object is always chosen as the closest, even though the point is actually on the larger object.

All of the systems Unity provides (that I’m aware of) for testing this type of thing rely on a collider and\or rigidbody being present, but I can’t add these for performance purposes (the game is for iPhone), and I really don’t want to be getting down and dirty trying to check the distances between vertices to sort this out, as I think that’d be too slow too, so, has anyone got any ideas?

If any of that isn’t clear, please let me know - this looks like the last hurdle before completion!

Cheers,

SB

I would make a sphere collider set to be a trigger, and scale it up in size (by like .1 or something) until it collides with something. Then whatever it hits is what the closest object is. This only works if the other objects have a collider I believe. Get what I mean?

Yeah - and you’re right - that would work, but as I said, there’s only one collider (the outer hull one), so it wouldn’t pick out individual sub-objects… :frowning:

Any other ideas?

OK - the logic works, for sure, but how do you define the “size” of an object as a single number?

You could also use the above mentioned trigger method to get a generalized closest object group, and then do a raycast for each object inside of that group and compare distances. Lowest is closest then obviously.

Physics.Raycast and RaycastHit.distance are the two main things you would need using that method. Assuming you only have 10-15 objects, it shouldn’t take any time at all. Probably could even do many more then that without major slow down.

Again, nice work, but Physics.raycast only works with colliders. There’s only ONE collider in the scene - the hull of the collective of mesh objects. I can detect the position of impact on the Hull using Physics.Raycast, but what I need to do is find is the closest mesh to the point of impact on the hull collider - like so:

Does that explain it any better?

437656--15200--$Problem.jpg