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:
void Start ()
{
// Putting the two children into variables.
line1a = GameObject.Find (“Poles/pole1/line1”);
line1b = GameObject.Find (“Poles/pole2/line1”);
// 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);
}
// Returns magnitude between two vector3’s.
float GetLength(Vector3 obj1, Vector3 obj2)
{
return new Vector3(Mathf.Abs (obj2.x - obj1.x),0,
Mathf.Abs (obj2.z - obj1.z)).magnitude;
}