- Home /
Easiest way to find a length I need?
I've got 2 objects that I need to connect with a prefab cylinder that acts as a wire. What I thought what would work makes it too long though :/
Thought I could just apply a^2 + b^2 = length I need squared, but it still comes out a bit too long.
wireTest = Instantiate (wire1, wirePos, Quaternion.identity) as GameObject; wireTest.transform.Rotate(new Vector3(0,0,270)); wireTest.transform.localScale += new Vector3(0, GetLength (getSide1, getSide2), 0);
And the method:
float GetLength(float side1, float side2) { return Mathf.Sqrt (side1 + side2); }
side1 and side2 are simply (x2 - x1)^2 and (z2 - z1)^2. Where have I gone awry here, and is there an easier way?
EDIT:
{ // Putting the two children into variables. line1a = GameObject.Find ("Poles/pole1/line1"); line1b = GameObject.Find ("Poles/pole2/line1");void Start ()
// Height of all wires. wireHeight = line1a.transform.position.y;
// returns magnitude of two vector3's. length = GetLength (line1a.transform.position, line1b.transform.position);
// Used to calculate where the center is between two poles. float dist1 = (line1a.transform.position.x + line1b.transform.position.x) / 2; float depth1 = (line1a.transform.position.z + line1b.transform.position.z) /2;
// Vector3 to be used in placing wire in center between two poles. Vector3 wirePos = new Vector3(dist1, wireHeight, depth1);
// Instantiating wire, rotating, and attempting to get it correct length, but not angle yet. wireTest = Instantiate (wire1, wirePos, Quaternion.identity) as GameObject; wireTest.transform.Rotate(new Vector3(0,0,270)); wireTest.transform.localScale += new Vector3(0,length , 0); }
float GetLength(Vector3 obj1, Vector3 obj2) { return new Vector3(Mathf.Abs (obj2.x - obj1.x),0, Mathf.Abs (obj2.z - obj1.z)).magnitude; }// Returns magnitude between two vector3's.
Sorry about that formatting, it looked correct when I posted it :/
Answer by robertbu · Feb 02, 2013 at 07:47 PM
Assuming the wire is one unit long to start and you have two the points p1, p2 as Vector3's, the scale will be (p2 - p1).magnitude.
I have em as just the distance between x coordinates and z coordinates. But I'll just get the vector distance and try magnitude real quick and see if it works out!
I'm not sure how your code is setup, but the transform.position of these children will be in world space.
Edited in more code so hopefully my ignorance can be cured.
Answer by Wolfram · Feb 02, 2013 at 10:34 PM
Your length is correct, even the first version. But you are adding that length onto localScale, instead of multiplying/setting it.
So if you start from a unit scale (1,1,1), adding (0,length,0) results in a scale of (1,1+length,1). Use this instead:
wireTest.transform.localScale *= new Vector3(1,length , 1);
$$anonymous$$ono screams at me that *= isn't valid between two Vector3's.
$$anonymous$$ono is correct ^^
Well, since you just instantiateed that object, the scale should be (1,1,1) anyway, so just override it:
wireTest.transform.localScale = new Vector3(1,length , 1);
Ugh, dunno whats going on. Even with that it's way too long. Just from some simple trial and error, the length is just under 20 units too long.
Well, this approach only works if your "wire1" prefab has a y-dimension of 1. Is that the case?
EDIT: ...and it has no localScale (shoudl be (1,1,1))
It is 1, yes. And if it matters, the scale for x and z of the cylinder I'm using as a wire are .2 and .2.
Your answer