OverlapSphere like Unity.Physics?

I modified the SphereCast example from this: https://docs.unity3d.com/Packages/com.unity.physics@0.5/manual/collision_queries.html
to return all hits. Basically just adding this:

NativeList<ColliderCastHit> allHits = new NativeList<ColliderCastHit>(Allocator.Temp);
bool haveHit = collisionWorld.CastCollider(input, ref allHits);

The thing is that it's returning the same entity multiple times because it hits each entity multiple times. Is it just about removing duplicates or am I missing something? Goal is for it to work similar to Unity.Physics.OverlapSphere where it return all colliders (/entities) that are inside or touch the sphere.

have you considered just using CalculateDistance instead of CastCollider?

physicsWorld.CalculateDistance(PointDistanceInput input, ref NativeList<DistanceHit> allHits);

1 Like

That actually works better for my use case so thank you, but it still doesn't solve the problem I'm having with the same entity ending up multiple times in allHits for some reason.

You want to check the ColliderKey of the hits as well. They should be different. The reason is that you are hitting multiple instances of the same CompoundCollider, represented by one entity (one body), but different collider keys representing different child colliders. Use CompoundCollider.GetChild() to get these colliders. You'll see they are different hits.

Ok, that makes sense. So if I want a list of unique entities in allHits I have to remove the duplicates? There's no way around that?

No, there is no way around that. Also, I wouldn't remove duplicates unless you only need the actual entities that were hit.

We had an ask once to only report one hit per entity. It's a reasonable thing to ask for, but we haven't found a performant way to achieve that due to the multithreaded nature of the physics solver. We'll keep looking.

Hi, there might be an underlying performance lagging related to this, u see when using CastCollider with allHits, it gets a massive amount of ColliderCastInput, especially colliding with Mesh Collider.


CastCollider with no ColliderCastHit
CastCollider with closestHit
CastCollider with allHits

They've been tested under an updating system at every frame, they don't make any bumping and all run smoothly when collided with simple colliders. But with mesh collider, hundreds of hits are generated and FPS is 10 lower, even I only asked for closestHit it's still 10 times slower than the 1st one(see pic).

1 Like