How to add a colllider for Line Renderer?

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.

 if(Input.GetMouseButtonDown(0))
        {
            Debug.Log("Mouse down");
            currenline = Instantiate(linePrefab);
            currenline.transform.SetParent(LineParent);
            NewLine = currenline.GetComponent<LineRenderer>();

            CurrmousePos0 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            temp0 = new Vector3(CurrmousePos0.x, CurrmousePos0.y, -4);

            NewLine.SetPosition(0, temp0);
            NewLine.SetPosition(1, new Vector3(CurrmousePos0.x, CurrmousePos0.y, -4));


        }
        if(Input.GetMouseButtonUp(0))
        {


            CurrmousePos1 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            temp1 = new Vector3(CurrmousePos1.x, CurrmousePos1.y, -4);
            NewLine.SetPosition(1, temp1);
            // bounds = NewLine.bounds;
            //PolygonCollider2D poly=NewLine.GetComponent<PolygonCollider2D>();
            //Vector2[] points = poly.points;

            //EdgeCollider2D collider2d = NewLine.gameObject.AddComponent<EdgeCollider2D>();
            //collider2d.points = points;
            //Destroy(poly);
        }


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.

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.

1 Like

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.

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.
2 Likes

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.

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.

I tried you method with the below code as u said.

            CapsuleCol.transform.position = (temp0 + temp1) * 0.5f ;
            CapsuleCol.transform.LookAt(temp1);
            CapsuleCol.GetComponent<CapsuleCollider>().height = Vector3.Distance(temp0, temp1);
            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.

Instantiate(CapsuleCol);
            CapsuleCol.transform.position = (temp0 + temp1) * 0.5f ;
            CapsuleCol.transform.LookAt(temp1);
            CapsuleCol.GetComponent<CapsuleCollider>().height = Vector3.Distance(temp0, temp1);
            CapsuleCol.GetComponent<CapsuleCollider>().radius =0.06f;

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

 if (Input.touchCount > 0)
        {


            Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
            // Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            if (this.GetComponent<LineRenderer>() == true)
            {
                bool overSprite = this.GetComponent<LineRenderer>().bounds.Contains(mousePos);
                //Debug.Log("Values " + overSprite);


                if (overSprite)
                {
                    Debug.Log("Values  of sprite = " + overSprite);
                }
                else
                {
                    Debug.Log("Not on Sprite");
                }
            }
            else
            {
                Debug.Log("No Line Renderer Found");
            }
        }

You missed the last part of step 1 then, which would have oriented the capsule to make “LookAt” easier.

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.

         RaycastHit hit;
        // Does the ray intersect any objects excluding the player layer
        if (Physics.Raycast(Camera.main.transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity))
        {
            Debug.DrawRay(Camera.main.transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
            Debug.Log("Name is = " + hit.collider.name);
        }

Next I tried IPointerHandler with Physics 2d Raycaster on main camera

       if (eventData.pointerEnter.gameObject.name == "CapsuleColl(Clone)")
        {
            Debug.Log("Pointer on Line");
            sound[3].source.clip = sound[3].clip;
            sound[3].source.loop = true;
            sound[3].source.Play();
        }

Why are you checking against the line still if you have the collider to use?

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)”)