Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

create a cylinder with start and ending points

Discussion in 'General Graphics' started by Dazzid, Jun 23, 2016.

  1. Dazzid

    Dazzid

    Joined:
    Apr 15, 2014
    Posts:
    61
    Hi All
    I'm trying to draw a cylinder between two Vector3. Don't find the way to do it.
    p1 is starting
    p2 is ending
    p3 is center
    Code (CSharp):
    1. public void create_cylinder(Vector3 p1, Vector3 p2, Vector3 p3) {
    2. GameObject segObj = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
    3. segObj.transform.localPosition = p3;
    4. segObj.transform.localScale = new Vector3(segObj.transform.localScale.x,Vector3.Distance(p1,p2)/2.0f,segObj.transform.localScale.z);
    5. segObj.transform.position = p3; //placebond here
    6. segObj.transform.LookAt(p1);
    7. }
     
  2. Dazzid

    Dazzid

    Joined:
    Apr 15, 2014
    Posts:
    61
    Actually p3 is not the centre, I have that point also wrong
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,248
    Far easier to get the center point from the two end points.

    Vector3 center = (p1 + p2) * 0.5f;