GameObject positions not lining up
I have a prefab that I am using for all of my walls currently which contains a wall (cube), a floor (plane), and five empty GameObjects, which I am using to detect the walls, assembled into the shape of the picture below.
Now, each of these is positioned in a grid at intervals of 1 from each other. The issue I'm encountering is, Given all these coordinates, if the prefabs are at the same rotation, the yellow and green GameObjects of connecting wall tiles SHOULD be at the same transform position. Getting the sqrmagnitude of the distance between a yellow and it's nearest green however, yields differences of anywhere from 0.1 to 5 whole units away which makes little sense to me. I hope I have provided enough information for someone to tell me that I'm not crazy and what I think should be happening, should be happening. Thanks.
Also, if necessary, the code that I'm using to find the closest Empty Object:
GameObject FindClosest(GameObject[] obj, GameObject tar)
{
GameObject closest = null;
float coverDist = Mathf.Infinity;
foreach(GameObject go in obj)
{
if(go != this.gameObject)
{
Vector3 diff = go.transform.position - tar.transform.position;
float curDistance = diff.sqrMagnitude;
if (curDistance < coverDist)
{
closest = go.gameObject;
coverDist = curDistance;
}
}
}
return closest;
}
Isn't Vector3.Distance better suited for finding distance? sqr$$anonymous$$agnitude will give you the squared magnitude so it will not return distance.
Using sqr$$anonymous$$agnitude is more efficient if you don't care about getting the actual distance which I don't in FindClosest. Sqr$$anonymous$$agnitude saves the computer the effort of finding the square root of the squared x + z value and comparing them is still accurate. If you actually need the exact distance though, Vector3.Distance is better.