Search Unity

how to draw lines between two gameobjects ?

Discussion in 'Editor & General Support' started by black6, Feb 3, 2012.

Thread Status:
Not open for further replies.
  1. black6

    black6

    Joined:
    Jan 31, 2012
    Posts:
    45
    hey

    i want to draw a line between a two gameobject using line render ,, i have tried something called gizmoz and i draw a line using this code
    Code (csharp):
    1.  
    2. Gizmos.DrawLine(transform.position,new Vector3(-45.49318f,11.54467f,-69.10096f));
    3.  
    but i need the line to be drawn in run time whether in line render or something else ?
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    I dunno about all that gizmo stuff, and whatnot -

    buttt you could always just use a raycast from one object to the other - not sure how you were going to use that? - but then you could use a debug code to tell it to draw a line (just a simple one pixel line is all I know) along the rays path. I sure couldn't tell you how to code that :(
     
  3. black6

    black6

    Joined:
    Jan 31, 2012
    Posts:
    45
    thnx for the reply

    but can give me an example that Describes your idea
     
    eisen_montalvo likes this.
  4. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
    Code (csharp):
    1.  
    2. function Update () {
    3.     var forward : Vector3 = transform.TransformDirection(Vector3.forward) * 10;
    4.     Debug.DrawRay (transform.position, forward, Color.green);
    5. }
    6.  
    But this only works in game, if you want editor.

    Then check the Gizmo.Drawline

    Code (csharp):
    1.  
    2. var target : Transform;
    3. function OnDrawGizmosSelected () {
    4.     if(target != null) {
    5.         // Draws a blue line from this transform to the target
    6.         Gizmos.color = Color.blue;
    7.         Gizmos.DrawLine (transform.position, target.position);
    8.     }
    9. }
    10.  
    And click the object when you want to see the linie.
     
    Last edited: Feb 3, 2012
    PersianKiller and Dr_Chandra like this.
  5. black6

    black6

    Joined:
    Jan 31, 2012
    Posts:
    45
    thnx snortop

    but i know how to do it in Gizmoz ,,,, i need a way to draw the line not in the scene but in the game it self
     
    Last edited: Feb 3, 2012
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
  7. black6

    black6

    Joined:
    Jan 31, 2012
    Posts:
    45
    thnx mgear ,,,

    i succeed in draw a line start from one of my game objects but the line go down not straight ,,, i need the line to be gone from one object to another also in line render ,,, how?
     
  8. Heidgerken

    Heidgerken

    Joined:
    Feb 2, 2012
    Posts:
    3
    black6,

    I recently got started playing with unity and had a similar need. I ended up using this:


    lineRenderer.SetPosition(0, new Vector3(objectOne.transform.position.x,objectOne.transform.position.y,objectOne.transform.position.z));
    lineRenderer.SetPosition(1, new Vector3(objectTwo.transform.position.x,objectTwo.transform.position.y,objectTwo.transform.position.z));

    Hope that helps.
     
    Last edited: Feb 6, 2012
  9. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
    Did you try that ^Heidgerkens code:
    LineRenderer.SetPosition(0, transform.position);
    LineRenderer.SetPosition(1, target.position);
     
    Unshackled likes this.
  10. black6

    black6

    Joined:
    Jan 31, 2012
    Posts:
    45
    thnx a lot Heidgerken that was help ,,,, thnx mgear too ,,,

    i succeed draw the line between the two objects but there is a line keep going down ?
     
  11. UnityCoder

    UnityCoder

    Joined:
    Dec 8, 2011
    Posts:
    534
    You can also use GL.line.
     
  12. black6

    black6

    Joined:
    Jan 31, 2012
    Posts:
    45
    ok guys i have another question,,,

    if i draw a line between three objects (LineRenderer) and i want to delete one of them not the whole line ,,, can i do it? ,,, how?
     
  13. sona.viswam

    sona.viswam

    Joined:
    Nov 20, 2012
    Posts:
    2
    Anybody have answer for just above question :!:
     
  14. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's easier and more flexible to do anything with line-drawing by using Vectrosity.

    --Eric
     
    mrtkhosravi likes this.
  15. mrtkhosravi

    mrtkhosravi

    Joined:
    Nov 9, 2014
    Posts:
    198
    This asset is very good. Thanks!!
     
Thread Status:
Not open for further replies.