the middle is draggable and the “planets” are connected to it. the problem are the connections. right now i have cylinders but i don’t know how to tell unity to scale these cylinders so that they stay connected to the middle (sun).
i have a script in aftereffects that works the same way in 2D. unfortunately my programming skills are very limited so i don’t how to translate the afx script into unity syntax.
the only thing i know is that the cylinder has to know the distance between him and the middle sphere and convert this information into an appopriate scale command.
The distance between two points is calculated by subtracting the two locations and getting the magnitude of the resulting vector:
var distance = (pos1 - pos2).magnitude;
Unity also has a convenience function called Vector3.Distance that does the same thing:
var distance = Vector3.Distance(pos1, pos2);
You should probably base the local scale of one of the axis of the cylinder object on this distance. Something like this:
// this goes somewhere inside a behaviour
// attached to the cylinder
var centerBall : Transform ; // drag the center ball onto this variable
var outerBall : Transform ; // and the other one here
var origLength = 2.0; // the original length of the cylinder
function Update() {
var distance = Vector3.Distance(centerBall.position, outerBall.position);
transform.localScale.z = distance / origLength;
// You might need to change the above line
// to one of the below depending on how the
// cylinder is orientated:
// transform.localScale.y = distance / origLength;
// transform.localScale.x = distance / origLength;
}
great! thank you for the script. it works kinda but unfortunately the cylinder gets scaled in the middle?how do i tell it to scale from the end/beginning?
right now my hierarchy is the following:
(outer)sphere:gameobject:cylinder
i use the gameobject because the “lookat” function didn’t work correctly with the cylinder. so i dropped the cylinder in the gameobject and rotated it accordingly?
is it possible to drag the middle ponit of the cylinder to the end like in standard 3d packages?