Search Unity

How to add a colllider for Line Renderer?

Discussion in 'Scripting' started by zyonneo, Sep 23, 2019.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    I have added a box collider2D to test whether the collider is present.When I tested the collider is present,not on the gameobject but behind the gameobject .How to add a collider to line renderer and detect collisions.

    Code (CSharp):
    1.  if(Input.GetMouseButtonDown(0))
    2.         {
    3.             Debug.Log("Mouse down");
    4.             currenline = Instantiate(linePrefab);
    5.             currenline.transform.SetParent(LineParent);
    6.             NewLine = currenline.GetComponent<LineRenderer>();
    7.  
    8.             CurrmousePos0 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    9.             temp0 = new Vector3(CurrmousePos0.x, CurrmousePos0.y, -4);
    10.  
    11.             NewLine.SetPosition(0, temp0);
    12.             NewLine.SetPosition(1, new Vector3(CurrmousePos0.x, CurrmousePos0.y, -4));
    13.  
    14.  
    15.         }
    16.         if(Input.GetMouseButtonUp(0))
    17.         {
    18.  
    19.  
    20.             CurrmousePos1 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    21.             temp1 = new Vector3(CurrmousePos1.x, CurrmousePos1.y, -4);
    22.             NewLine.SetPosition(1, temp1);
    23.             // bounds = NewLine.bounds;
    24.             //PolygonCollider2D poly=NewLine.GetComponent<PolygonCollider2D>();
    25.             //Vector2[] points = poly.points;
    26.  
    27.             //EdgeCollider2D collider2d = NewLine.gameObject.AddComponent<EdgeCollider2D>();
    28.             //collider2d.points = points;
    29.             //Destroy(poly);
    30.         }

     
  2. shawndingo

    shawndingo

    Joined:
    Oct 4, 2018
    Posts:
    83
    I'm no expert in Line Render but I'm assuming you can't add one for the reason is that the line render acts as a ray. It's not defined as a solid object. Instead you can use a bool to test whether or not the line is being it. Then if it is do something. Or using the box collider you can do something after a collision.
     
  3. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Yeah, I think you are trying to go about it backwards. Instead of seeing if something interacts with a line, you should check to see if the line interacts with anything else. Use a raycast for this.
     
    shawndingo likes this.
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    The LineRenderer is more than a straight line, so it's not a ray nor easily representable by a ray, unless it's actually straight. I definitely remember seeing someone use a LineRenderer as a "drawn ground" sort of thing, and then have a ball roll over it. So adding collisions should be possible.
    I believe in the video i saw he added an EdgeCollider 2D, but i'm not sure what you would in 3D. Technically tho, the a line is always 2D, and it may only be the visuals you can add that make it appear to be 3D. Definitely not an expert here either, just thought i'd mention the above.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    The short answer is "you can't", the longer answer is "you can't, but you can make your own collider that kinda acts like one". If the line in question is just one segment (2 points), then this is actually pretty easy. Do the following anytime your LineRenderer's positions are changed:
    1. Create a new GameObject, add a Capsule Collider, set the "direction" to match the Z axis (2).
    2. Set this object's position to (A + B) * 0.5, where A and B are your two line points.
    3. transform.LookAt(B);
    4. Set "height" to Vector3.Distance(A, B)
    5. Set radius to whatever best matches your LineRenderer's visual width.
     
    zyonneo and shawndingo like this.
  6. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    What is happening in 2,3 and 4th steps.How do you calculate ones position by adding two vector and divide by two?what is the need for LookAt?

    For some reason the collider is not getting detected.Has it anything to do it with adding 3D collider and detecting using Physics2D raycaster in Main Camera? After adding a 3D box collider into a sprite renderer,it is not getting detected where as 2d box collider is getting detected.
     
    Last edited: Sep 25, 2019
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    That puts the cylinder in the middle of the line segment, aligns it in the direction of the line, and stretches it lengthwise to match the line.
     
  8. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    I tried you method with the below code as u said.
    Code (CSharp):
    1.  
    2.             CapsuleCol.transform.position = (temp0 + temp1) * 0.5f ;
    3.             CapsuleCol.transform.LookAt(temp1);
    4.             CapsuleCol.GetComponent<CapsuleCollider>().height = Vector3.Distance(temp0, temp1);
    5.             CapsuleCol.GetComponent<CapsuleCollider>().radius =0.06f;
    But I have to add 90 to the rotation of x axis. to come align to it.I am drawing several lines so i made capsule collider prefab.When I instantiate it it is coming on different positions.
    Code (CSharp):
    1.  
    2. Instantiate(CapsuleCol);
    3.             CapsuleCol.transform.position = (temp0 + temp1) * 0.5f ;
    4.             CapsuleCol.transform.LookAt(temp1);
    5.             CapsuleCol.GetComponent<CapsuleCollider>().height = Vector3.Distance(temp0, temp1);
    6.             CapsuleCol.GetComponent<CapsuleCollider>().radius =0.06f;
    7.  

    I have tried the following code with sprite renderer.Will it work with Line renderer.I tried but touch not detecting the colllider.

    Code (CSharp):
    1.  if (Input.touchCount > 0)
    2.         {
    3.  
    4.  
    5.             Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    6.             // Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    7.  
    8.             if (this.GetComponent<LineRenderer>() == true)
    9.             {
    10.                 bool overSprite = this.GetComponent<LineRenderer>().bounds.Contains(mousePos);
    11.                 //Debug.Log("Values " + overSprite);
    12.  
    13.  
    14.                 if (overSprite)
    15.                 {
    16.                     Debug.Log("Values  of sprite = " + overSprite);
    17.                 }
    18.                 else
    19.                 {
    20.                     Debug.Log("Not on Sprite");
    21.                 }
    22.             }
    23.             else
    24.             {
    25.                 Debug.Log("No Line Renderer Found");
    26.             }
    27.         }
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You missed the last part of step 1 then, which would have oriented the capsule to make "LookAt" easier.
     
  10. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    Yes Now the collider comes on the line renderer.How to check whether the touch is registered on the Line renderer.
    Methods I tried was not successful.
    Code (CSharp):
    1.  
    2.          RaycastHit hit;
    3.         // Does the ray intersect any objects excluding the player layer
    4.         if (Physics.Raycast(Camera.main.transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity))
    5.         {
    6.             Debug.DrawRay(Camera.main.transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
    7.             Debug.Log("Name is = " + hit.collider.name);
    8.         }
    9.  
    10.  
    Next I tried IPointerHandler with Physics 2d Raycaster on main camera
    Code (CSharp):
    1.  
    2.        if (eventData.pointerEnter.gameObject.name == "CapsuleColl(Clone)")
    3.         {
    4.             Debug.Log("Pointer on Line");
    5.             sound[3].source.clip = sound[3].clip;
    6.             sound[3].source.loop = true;
    7.             sound[3].source.Play();
    8.         }
     
  11. shawndingo

    shawndingo

    Joined:
    Oct 4, 2018
    Posts:
    83
    Why are you checking against the line still if you have the collider to use?
     
  12. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    The second method I am checking against the capsule collider having the game object called CapsuleColl(Clone) which was instantiated along with the line ......if (eventData.pointerEnter.gameObject.name == "CapsuleColl(Clone)")