Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by claudiaq16 · Jun 10, 2013 at 08:23 PM · spherecylinderconnecting

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moghes · Jun 10, 2013 at 10:11 PM 0
Share

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?

avatar image claudiaq16 · Jun 11, 2013 at 02:29 PM 0
Share

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!

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);


Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image claudiaq16 · Jun 11, 2013 at 04:00 PM 0
Share

This may be a dumb question but this is JavaScript right? I am not using CSharp for this. Thanks for your help!

avatar image robertbu · Jun 11, 2013 at 04:11 PM 0
Share

Now it is Javascript.

avatar image claudiaq16 · Jun 11, 2013 at 05:09 PM 0
Share

Thanks!! :)

avatar image claudiaq16 · Jun 12, 2013 at 01:55 PM 0
Share

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){

}

avatar image robertbu · Jun 12, 2013 at 03:32 PM 0
Share

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);
 }
Show more comments
avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Connecting two spheres with a cylinder 1 Answer

how add cylinder with rigid body inside a sphere with rigid body? 0 Answers

How to make a cylinder follow two sphere 1 Answer

Move Spheres In a Hollow Cylinder 1 Answer

LookAt function not working properly(cylinder looking at sphere) 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges