# Calculating distances: where's the error?

I have two movable objects. At the press of a button, I create the vector “distance” between them and get it’s magnitude. Then I move a disabled box collider to the point halfway between those two objects (objectA.transform.position - distance.magnitude/2), rotate it appropriately and enable the collider. So far so good.

Now, when I set the collider’s size (distance.magnitude/2, someValue), something weird happens: when the two objects are close together, the collider is too small, when the objects are far apart it’s too large, reaching beyond the objects’ origins. I expected the collider to extend always from one object’s transform position to the other’s.

``````distance = new Vector3 (playerA.transform.position.x -playerB.transform.position.x, playerA.transform.position.y -playerB.transform.position.y, playerA.transform.position.z -playerB.transform.position.z);

//setting the collider's position halwfway between A & B
linkObject.transform.position = (playerA.transform.position - distance / 2);

// rotating collider
float angle = Vector3.Angle (distance, Vector3.right);
float sign = (Vector3.Dot (distance, Vector3.down) > 0.0f) ? -1.0f : 1.0f;
float finalAngle = sign * angle;
linkObject.transform.eulerAngles = new Vector3 (0f, 0f, finalAngle);

//this is the bit I don't understand why it doesn't work
boxCollider2D.size = new Vector2 (distance.magnitude / 2, 0.5f);

//scaling this doesn't work correctly either, btw
linkObject.transform.localScale = new Vector3 (distance.magnitude , 0.1f, 0.1f);
``````

What am I missing?

bump