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. Dismiss Notice

Help with puting cylinder between two spheres...

Discussion in 'Scripting' started by pretender, Mar 6, 2015.

  1. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    Hi guys! I am having trouble understanding why this simple thing isn't working.

    I have two speheres, and I have cylinder. I want to position cylinder between two spheres and then rotate cylinder so it connects two spheres (last step would be to scale the stick so it does actually connect the spheres)

    puting cylinder between two spheres is easy:
    Code (CSharp):
    1. var midPoint = Vector3.Lerp(a.position, b.position, 0.5f);
    2.         stick.position = midPoint;
    i tried to do the rotating like this:
    Code (CSharp):
    1.  stick.rotation = Quaternion.LookRotation((b.position - a.position).normalized);
    and the result is show in the image below
    current.png
    so it does rotate but it seems that orientation of the cylinder is wrong to begin with.
    i tried several other options like this:
    Code (CSharp):
    1. stick.rotation = Quaternion.LookRotation((b.position - a.position).normalized, transform.forward);
    2. ...
    3. stick.rotation = Quaternion.LookRotation((b.position - a.position).normalized, transform.right);
    but nothing worked like it should.

    then i made empty game object, put cylinder inside and rotated it for 90 degress on x axis
    then running above code it gives me good result like in the image below:
    good0.png

    so the result is good but cylinder should be scaled so it connects the spheres...i managed to do that like this:
    Code (CSharp):
    1. stick.SetScaleZ(distance / 2f);
    SetScaleZ is extension method that is just helper.

    result is the image below
    good.png

    so my question is how to optimize this, avoid the dummy object and initial rotation for 90 degrees. also is there better way to connect the spheres but just scaling the object?

    Thank you!
     
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    do your rotation like this
    Code (csharp):
    1.  
    2.   transform.rotation = Quaternion.LookRotation((b.position - a.position).normalized) * Quaternion.Euler(Vector3.right * 90f);
    3.  
     
  3. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Scale is done in terms of Unity unitys, right? Primitives of scale 1 are also 1 unit in height? You could try setting the z scale to half of the distance between the 2 spheres.
     
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    ya a default cylender is 1 unit long, though you mean the y axis since unity is a y-up engine
     
  5. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Yea lol.
     
  6. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    Code (csharp):
    1.  
    2.   transform.position = Vector3.Lerp(a.position, b.position, 0.5f);
    3.   transform.rotation = Quaternion.LookRotation((b.position - a.position).normalized) * Quaternion.Euler(Vector3.right * 90f);
    4.   transform.localScale = new Vector3(transform.localScale.x, Vector3.Distance(a.position, b.position) * 0.5f, transform.localScale.z);
    5.