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 /
avatar image
0
Question by pippopoppi · Apr 23, 2014 at 09:30 AM · followspherecylinder

How to make a cylinder follow two sphere

Hi, i'm new and i'm trying to make a simple project in unity. I have 3 object : a cylinder, and two spheres. I'd like to make the cylinder follow the two sphere like a sort of line through two point. Any suggestions ?? I've tried to use the Quaternion.LookRotation() and the Quaternion.FromToRotation() functions but it didn't works.

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 HarshadK · Apr 23, 2014 at 09:41 AM 0
Share

Can you elaborate a more about how you want cylinder to follow? with an illustration maybe?

avatar image pippopoppi · Apr 23, 2014 at 10:08 AM 0
Share

This is the illustration : there are the red sphere and the blue one connected by the cylinder. In the project i can move the red and the blue sphere in the 3d space and i want that the gray cylinder always connect the spheres. thanks

alt text

immagine.png (310.9 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by robertbu · Apr 23, 2014 at 09:40 AM

Here is a bit of code to get you started:

 #pragma strict
  
  var sphere1 : Transform;
  var sphere2 : Transform;
  
 function Start() {
     transform.localScale = Vector3 (0.25, 1.0, 0.25);
 }
 
 function Update() {
     var dir = sphere2.position - sphere1.position;
     transform.position = sphere1.position + 0.5 * dir;
     transform.localScale.y = dir.magnitude * 0.5;
     transform.rotation = Quaternion.FromToRotation(Vector3.up, dir);
 }

You have to initialize Sphere1 and Sphere2 in the Inspector by drag and drop. Start() set the x and z to 0.25 as the width of the cylinder. Set as appropriate to your application.

Comment
Add comment · Show 3 · 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 pippopoppi · Apr 23, 2014 at 10:15 AM 0
Share

thanks, but i can't use that code. There are some errors. $$anonymous$$aybe because i try to write it in c# ??? ins$$anonymous$$d of var sphere I use public Transform sphere1 and ins$$anonymous$$d of var dir = Sphere2.position - Sphere1.position; i use Vector3 dir = ...

is it ok ??

avatar image robertbu · Apr 23, 2014 at 10:30 AM 0
Share

Here is a C# translation of the code. Before integrating it into your app, test it in a new scene. In a new scene, create two spheres and a cylinder. Attach the script to a cylinder and initialize 'Sphere1' and 'Sphere2' through drag and drop. Wile running, move the spheres around.

 using UnityEngine;
 using System.Collections;
 
 public class Bug25b : $$anonymous$$onoBehaviour {
     public Transform sphere1;
     public Transform sphere2;
     
     void Start() {
         transform.localScale = new Vector3 (0.25f, 1.0f, 0.25f);
     }
     
     void Update() {
         Vector3 dir = sphere2.position - sphere1.position;
         transform.position = sphere1.position + 0.5f * dir;
         Vector3 scale = transform.localScale;
         scale.y = dir.magnitude * 0.5f;
         transform.localScale = scale;
         transform.rotation = Quaternion.FromToRotation(Vector3.up, dir);
     }
 }
avatar image pippopoppi · Apr 23, 2014 at 10:46 AM 0
Share

thank you !! it works !! :)

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

20 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 avatar image avatar image avatar image avatar image

Related Questions

Unity camera follow along cylinder looking for C# Expert!,Unity camera follow along cylinder Looking For C# Expert! 1 Answer

Camera Follow sphere with ridgidbody? 4 Answers

Connecting two spheres with a cylinder 1 Answer

Camera rotation around player while following. 6 Answers

how add cylinder with rigid body inside a sphere with rigid body? 0 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