- Home /
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?
to caclulate the distance why dont you use distance = Vector3.Distance (playerA.transform.position , playerB.transform.position);
then is it a 3d or a 2d Game? Cause you are adjusting a boxCollider2D!
Are you both scaling and setting the size of the collider? that would be bad.
thanks for your responses.
@$$anonymous$$ill$$anonymous$$obil: It's a 2D-Game. I'm not using Vector3.Distance because I need to find the vector I called "distance" between playerA and playerB anyway to calculate the rotation of the object I'm enabling between the two of them (thus making it visible). For some reason I can't remember right now I had to use vector3s ins$$anonymous$$d of vector2s as I had expected for a purely 2D game.
@whydoidoit Well, I'm scaling the object that has the collider component, in order to adjust its sprite size (which feels like a hack, but it was the best way I knew to "create" a visible link of variable size between two objects). Isn't the collider's size independent of the transform it's attached to, though? Is it relative? Will test.
Well the scale and the size are related and multiplied - it sounds like just leaving the size and setting the scale is what you want.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134351im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Difficulties in Changing the Scale of an Object 2 Answers
Scaling Planets based on Distance from Camera 1 Answer
Handling iPhone / iPad screen sizes? 0 Answers
how to reduce library size? 2 Answers