I am running into a strange issue when I try to use Unity.Physics.CalculateDistance in my program. I have a very simple setup, one entity at (0,0,100) and create a distance query like:
var input= new PointDistanceInput{
Position = new float3(0,0,100),
MaxDistance = 1
};
var hits = new NativeList<DistanceHit>(Allocator.Temp);
World.CalculateDistance(colliderDistanceInput, ref hits);
hits comes back with 1, which is correct. The strange part is that if I display the hit.position, it shows something like (18, .003, 100) and when I move my camera, it changes the X and Y of the hit position. Seemingly changing to smaller values the closer the camera gets to the entity.
What is the world is happening here? Any help would be greatly appreciated.
Hi @TonyG_Plexsys , did you maybe unintentionally use a wrong var in World.CalculateDistance call?
I see you pasted:
World.CalculateDistance(colliderDistanceInput, ref hits);
Looking at the first part of your code snippet, I’d expect:
World.CalculateDistance(input, ref hits);
I’d suggest having a look at 3. Query\AllHitsDistanceTest scene, especially the UnityPhysicsSamples\Assets\Demos\3. Query\Scripts\QueryTester.cs script.
Thank you very much for the reply.
The code I gave above was just from recollection of the code I’m running, a more accurate version is the following:
BuildPhysicsWorld physicsWorld = World.DefaultGameObjectInjectionWorld.GetExistingSystem<BuildPhysicsWorld>();
CollisionWorld collisionWorld = physicsWorld.PhysicsWorld.CollisionWorld;
var filter = new CollisionFilter {
BelongsTo = ~ou,
CollidesWith = ~0u,
GroupIndex = 0
};
var pointInput = new PointDistanceInput {
Position = new float3(0,0,100),
Filter = filter,
MaxDistance = 1
};
var hits = new Unity.Collections.NativeList<DistanceHit>(Unity.Collections.Allocator Temp);
collisionWorld .CalculateDistance(pointInput, ref hits);
I ultimately am trying to do a marquee selection feature and am using the dots physics for picking already. My idea was to use a ColliderDistance call and provide a frustum mesh that I create to match the marquee rectangle. To get things rolling I wanted to do some testing and found that the calculate distance function was giving strange values back.
I have no doubt I’m doing something wrong somewhere, it’s just that I’m at a loss as to where to look since the functionality seems straight forward and I’m using the same physics world type code for my picking algorithm and that seems to work well.
Hmmm, not sure, I don’t see anything obviously wrong there.
Could you please try reproducing the problem on 3. Query\AllHitsDistanceTest scene?
I just tried it and didn’t see any diff in hit.Position when moving the camera.
I added an empty game object to the scene and a “Query Tester” script to it, playing with different script param values. Just keep Collider Mesh at “None”, so that CalculateDistance from point is used (the point will be game object position, set in Transform component).
1 Like
Thanks again for your help 
Apologies but I didn’t test with the sample code, however I did try using collider input instead of point distance input. The collider is a sphere and the transform is the location I was testing against.
Using the collider input seemed to have solved my issue of the positions/distances changing based on the camera distance. I don’t understand exactly why this works over the other way but am very happy that my results are making sense now.
1 Like