Reputation: 886
Image link (for those who don't see the inline image)
I am trying to calculate the distance between objects, (trying both position and localPosition using the Vector3.Distance method) but, it would seem there is a problem.
As you can see in the image, the two objects in the scene are not located at the same position however, their transforms tell a different story - they have the exact same transform info (position, rotation, and scale).
Assured it is something simple being missed here but, how to calculate the distance between two objects which are not located at the same location yet have identical transforms?
Upvotes: 0
Views: 716
Reputation: 2884
The following solutions applies to the problem described in the original post, not to programmatically generated meshes
I see that one of them doesn't have its pivot centered.
Make both of your objects child of an empty gameobject, this way you can move them relative to their parent. This will be like changing their pivot.
Calculate the distance based on the parent gameobjects.
Example:
Parent object1 to an empty gameobject. Move object1. Notice the transform of the parent doesn't change. So, you just have to move object1 so that its center aligns with the parent transform. Then, from there, create a prefab. This is your new object1.
Do this for any objects you want to have control on their pivot.
ALTERNATIVE:
If the preceding solution doesn't suit your needs, an easy way to achieve what you want would be to calculate the distance based on the transform.renderer.bounds.center
Your calculation would then be based on the center of the bounds of the object, not its actual position, eliminating the pivot problem.
Upvotes: 2