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
1
Question by pickledzebra · Jul 01, 2010 at 12:31 AM · cylinderprimitive

Create cylinder primitive between 2 endpoints?

I'm playing a bit with reading model coordinates from a text file, and wondering how one would instantiate a cylinder primitive (arbitrary radius) between 2 fixed endpoints, x1,y1,z1 and x2,y2,z2.

Just wondering if anyone has an efficient quick way to do it.

Comment
Add comment
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

3 Replies

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

Answer by Mike 3 · Jul 01, 2010 at 01:41 AM

This takes a default unity cylinder (must be added as a prefab) and stretches it out between two points, with a specified width. If you change it to a different cylinder, the scale may need to be manipulated slightly (right now it's divided by two as the default cylinder is 2m tall):

var cylinderPrefab : GameObject; //assumed to be 1m x 1m x 2m default unity cylinder to make calculations easy

//added the start function for testing purposes function Start() { CreateCylinderBetweenPoints(Vector3.zero, new Vector3(10, 10, 10), 0.5); }

function CreateCylinderBetweenPoints(start : Vector3, end : Vector3, width : float) { var offset = end - start; var scale = new Vector3(width, offset.magnitude / 2.0, width); var position = start + (offset / 2.0);

 var cylinder = Instantiate(cylinderPrefab, position, Quaternion.identity);
 cylinder.transform.up = offset;
 cylinder.transform.localScale = scale;

}

Comment
Add comment · Show 9 · 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 AnaRhisT · Jul 01, 2010 at 02:21 AM 0
Share

Why the "Quaternion.identity" doesn't take effecT? It's not 0,0,0...

avatar image Mike 3 · Jul 01, 2010 at 02:23 AM 0
Share

It's there as a filler. The next line is used to set the rotation properly

avatar image AnaRhisT · Jul 01, 2010 at 02:30 AM 0
Share

ook, anyways, I upgraded my code, what do u think? lol urs is better :@

avatar image Mike 3 · Jul 01, 2010 at 03:01 AM 0
Share

It's... original.

avatar image __zipper · Feb 07, 2013 at 02:02 AM 1
Share

To anyone following this via a Google search, I wonder if perhaps GameObject.CreatePrimitive isn't maybe a more suitable way to do this without a prefab..?

Show more comments
avatar image
0

Answer by doukasd · Dec 11, 2012 at 06:09 AM

Have a look at the source of this module, it creates a procedural cylinder. You might be able to easily make it create it from A to B.

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

Answer by andrewluck · Oct 01, 2019 at 10:50 AM

Here's the working modified code for newer versions of Unity/C#:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class point2point : MonoBehaviour
 {
     public Rigidbody cylinderPrefab; //assumed to be 1m x 1m x 2m default unity cylinder to make calculations easy
 
     // Start is called before the first frame update
     void Start()
     {
         CreateCylinderBetweenPoints(Vector3.zero, new Vector3(10, 10, 10), 0.5f);
     }
 
     void CreateCylinderBetweenPoints(Vector3 start, Vector3 end, float width)
     {
         var offset = end - start;
         var scale = new Vector3(width, offset.magnitude / 2.0f, width);
         var position = start + (offset / 2.0f);
 
         var cylinder = Instantiate(cylinderPrefab, position, Quaternion.identity);
         cylinder.transform.up = offset;
         cylinder.transform.localScale = scale;
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 }
 
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

6 People are following this question.

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

Related Questions

Connecting two spheres with a cylinder 1 Answer

Dynamically change Vector3.up/right?,How to make a cylinder chase mechanic? 2 Answers

Making an array "spread" out evenly? 1 Answer

Cylinders running downhill. Gravity is not enough. 3 Answers

How do you bend a cylindrical gameobject? 1 Answer


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