Search Unity

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,352
    Far easier to get the center point from the two end points.

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