I need help writing the code to connect two spheres with cylinders so that they are the correct length and perfectly in the center of the spheres. This needs to be done so when rendering, there isn’t unnecessary extra stuff to be rendered. I know I need some sort of distance formula but since I am new to coding I was hoping someone could help/point me in the right direction. Thanks!
To make this work, you 1) place the cylinder half way between the two spheres, scale the cylinder to the distance between the two spheres, and rotate the cylinder to match the angle between the two spheres. ‘v3Start’ and ‘v3End’ are the positions of the two spheres, and this script is attached to the cylinder:
// Position it
transform.position = (v3End-v3Start)/2.0f + v3Start;
var v3T = transform.localScale; // Scale it
v3T.y = (v3End-v3Start).magnitude;
transform.localScale = v3T;
// Rotate it
transform.rotation = Quaternion.FromToRotation(Vector3.up, v3End-v3Start);
Hi, In my case, with this exact script, one end of my cylinder shoots way beyond the end point, and start point is somewhere between the objects. Any hints of what I’m doing wrong?