- Home /
Connecting Two Spheres w/ Cylinders
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!
please can you explain more of what you want to achieve? I mean what the final outcome you want to be? and why you want to do that with scripting?
I am going to be creating a 3D "map" of nodes and connectors (cylinders) to show the relationships between entities. I would like to be able to zoom in on the nodes when they are clicked and have a description pop up. Also I have been asked to make this interactive so that the user can add additional nodes and connectors as needed with buttons on the screen. I need this to be as precise as possible so the program can handle big data, therefore needing the connectors to all be exactly in the center. I was told it was best to do this via coding in order to make the prefab connector uniform throughout, no matter the size of the nodes (spheres). I hope this explanation helps! Thanks!
Answer by robertbu · Jun 11, 2013 at 02:46 PM
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);
This may be a dumb question but this is JavaScript right? I am not using CSharp for this. Thanks for your help!
Assets/Scripts/connector_link.js(7,29): BCE0051: Operator '-' cannot be used with a left hand side of type 'UnityEngine.GameObject' and a right hand side of type 'UnityEngine.GameObject'.
I am getting this ^ error when I try to input the code. What should I do to fix this? Right now this is what I have in $$anonymous$$onoDevelop.....
pragma strict
ar v3Start : GameObject; var v3End : GameObject;
function Start () { // 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);
}
function Update () {
}
function connect(node1, node2){
}
When posting code, please select your code and use the 101/010 button to format. It is very difficult to read otherwise. As for your problem. v3Start and v3End are Vector3 positions, not game objects. Here is the code added to in Start():
#pragma strict
var goStart : GameObject;
var goEnd : GameObject;
function Start () {
var v3Start = goStart.transform.position;
var v3End = goEnd.transform.position;
transform.position = (v3End - v3Start)/2.0f + v3Start;
var v3T = transform.localScale;
v3T.y = (v3End - v3Start).magnitude; transform.localScale = v3T;
transform.rotation = Quaternion.FromToRotation(Vector3.up, v3End - v3Start);
}
Answer by ariel-manka · Oct 20, 2016 at 08:01 AM
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?
Your answer