Search Unity

Connecting the Dots (LineRenderer)

Discussion in 'Scripting' started by terminal205, Feb 6, 2015.

  1. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    I would like to draw a line between to GameObjects: A parent and it's child(ren).

    I learned in another thread that LineRenderer.SetPosition is the command I want for this. I haven't tested this yet as I am out of town away from my computer (always creating!)

    Code (javascript):
    1.  
    2.  
    3. var tObject : Transform;
    4.  
    5. function Start()
    6. {
    7.      var c1 : [URL='http://docs.unity3d.com/ScriptReference/Color.html']Color[/URL] = [URL='http://docs.unity3d.com/ScriptReference/Color-yellow.html']Color.blue[/URL];
    8.      var c2 : [URL='http://docs.unity3d.com/ScriptReference/Color.html']Color[/URL] = [URL='http://docs.unity3d.com/ScriptReference/Color-red.html']Color.green[/URL];
    9.      //var lengthOfLineRenderer : int = 20; function Start() {
    10.      var lineRenderer : [URL='http://docs.unity3d.com/ScriptReference/LineRenderer.html']LineRenderer[/URL] = gameObject.AddComponent([URL='http://docs.unity3d.com/ScriptReference/LineRenderer.html']LineRenderer[/URL]);
    11.      lineRenderer.material = new [URL='http://docs.unity3d.com/ScriptReference/Material.html']Material[/URL] ([URL='http://docs.unity3d.com/ScriptReference/Shader.Find.html']Shader.Find[/URL]("Particles/Additive"));
    12.      lineRenderer.SetColors(c1, c2);
    13.      lineRenderer.SetWidth(0.2,0.2);
    14.     // lineRenderer.SetVertexCount(lengthOfLineRenderer);
    15. }
    16. function Update()
    17. {
    18.      for each(Transform in transform) // for each child node in this transform
    19.      {
    20.           tObject = Transform;
    21.           lineRenderer.SetPosition(transform.position, tObject.transform.position);
    22.      }
    23. }
    24.  
    Thoughts? Should this in theory draw a line between the Parent object (transform) and the child objects (tObject)?
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    no, SetPosition take int index and Vector3 position. You copied the documentation example, read it again.
     
  3. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    Is there not a way to draw from a given .position to another .position?
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    yeah,
    Code (CSharp):
    1. setvertexcount(2)
    2. setposition(0, posA)
    3. setposition(1, posB)
    4.  
    Simplify it by having an array of objects to connect and then iterate that
    Code (CSharp):
    1. for(int i=0; i<array.length; i++){
    2.   setposition(i,array.position)
    3. }
     
  5. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    something along these lines?

    Code (javascript):
    1.  
    2. function OnMouseDown()
    3. {
    4. //var i : int = 0;
    5. var parentPos : Vector3 = gameObject.transform.position;
    6. for each(Transform in transform) // for each child node in this transform
    7. {  
    8.         tObject = Transform;
    9.         lineRenderer.SetPosition(0, parentPos);
    10.         lineRenderer.SetPosition(1, tObject.transform.position);
    11.        
    12. }
    13. }  
    14.  
    15.  
    16.  
     
  6. ensiferum888

    ensiferum888

    Joined:
    May 11, 2013
    Posts:
    317
    No, with this code you're constantly setting the first 2 vertex of your line renderer, so after your foreach loop is done you'll have a line between the parent and the last object but that's it.

    If you want to connect each object together you must use a for loop like in hpjohn's example.
     
  7. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    Ah, sorry, I didn't explain my intentions well enough.

    I am trying to draw a line between the Parent and multiple children (like a network instead of a true line). So according to you, my script would do just that: I wold then apply this same script to the child objects to further the network

    Parent -- childA
    Parent -- childB
     
  8. ensiferum888

    ensiferum888

    Joined:
    May 11, 2013
    Posts:
    317
    Ah yes, es but then you'd need one line renderer per child object and put the script on the child!
     
  9. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    So this sort of worked. But it only rendered a line to the first child.

    Also, is there a way to disable the line? Or would I need to destroy it when it's not needed?