I have seen something really strange in unity.
I don’t know if someone already seen this :
I made a cube A with rigidbody and a second one with rigidbody.
Constrain on X Y and Z position.
I tried to calculate distance between the 2 objects.
In window play mode, I checked the distance var to make my detection.
The terrain plane is totally straight but the distance between the 2 objects is variable.
For example, it is 3.124567 and it is not at this value but change to more or less 0.3.
The objects are not moving but the distance between the 2 object changes according to the time :o
Here are a few questions:
-
Are the cubes the same size? (not directly related to the problem but gives a sense of idea)
-
Did you set the transforms of each to an exact number, like 256 10 32 (basically, whole integers for sanity) (Not always possible with terrain but works well for testing functionality)
-
When you mean constrain on XYZ, do you mean you made them static or is kinematic?
-
Is it possible gravity is affecting the two objects?
-
Exactly how are you calculating distance? Raycasts, and exactly how?
Sorry if I asked “are the objects moving” several different ways even though you clearly said they aren’t moving, but I wanted to try to confront all the likely possibilities in one post.
Hope this leads to an answer!
Well, the cube are the default ones. Scale X,Y,Z as 1
But to be honest, no I don’t give them the place they have, I just clicked on create and leave them where they are then, the rigidbody has use gravity disabled, constrain on x,y,z position not on rotation.
I don’t think the gravity works on them because, it has been disabled.
I have tried with
distance = Vector3.Distance(target.position, transform.position);
and
var dist : Vector3 =target.position-transform.position;
distance=dist.magnitude;
Same results with the 2 options
I didn’t tried with Raycast because I really don’t like this option and I’m using it only if necessary to have no object between 2 objects in detection.
Just tested it with two positioned cubes (on a plane, so didn’t bother with rigidbodies or constraints), with absolute integer coordinates for simplicity (about 4,0,0 and -3,0,0) and got a distance of 7, as it should.
Also, just for clarification, “target” is a transform? Realized I couldn’t just copy and paste the code and test it. 
Additionally, how exactly did you “constrain” the positions? I tried a simple method to test the method you used to find the distance, and inherently, it IS correct. So the problem may lie more or less in the objects themselves. First try to absolutely position them on integers to check if the problem reoccurs- however, a variation of 0.3 shouldn’t be because of that, at the very least.
(Also, I’m assuming - for simplicity’s sake - “target” was a public variable declared earlier as “var target : transform;” and you dragged the second cube into the appropriate variable slot in the inspector)
Edit: I realized that the plane was completely unnecessary, as the cubes wouldn’t move without a rigidbodies. Added a rigidbody to each, let gravity affect them, still the same value. Then unchecked the gravity box and still the same value. All I can do is see what DOESN’T cause your problem, as I can’t recreate it. Try to look more into the objects (and the environment). I even tried to move the objects in real time, and I didn’t get an unexplainable variation at all.
I just find that a Character controller was in on of my cube. I deleted it, and I don’t have this issue anymore. The Character Controller seems to use some physics features in it and maybe, althought I was not able to see it, my object should move alone.
Well… I’ve updated Unity too.
Thanks for your help