Search Unity

Resolved How to add a collider to a line renderer?

Discussion in 'XR Interaction Toolkit and Input' started by zyonneo, Sep 23, 2019.

  1. zyonneo

    zyonneo

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


     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,438
  3. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    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.            
    24.             PolygonCollider2D poly=NewLine.gameObject.GetComponent<PolygonCollider2D>();
    25.             Vector2[] points = poly.points;
    26.  
    27.             EdgeCollider2D collider2d = NewLine.gameObject.AddComponent<EdgeCollider2D>();
    28.             collider2d.points = points;
    29.             Destroy(poly);
    30.         }
    I tried this code but not working.The collider is as shown above it is behind the gameobject on the origin.Here I added a PolygonCollider2D to the prefab.
     
    Last edited: Sep 23, 2019
  4. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
  5. prakyathd801

    prakyathd801

    Joined:
    Oct 7, 2020
    Posts:
    5
    BoxCollider3D lineCol = gameObject.AddComponent<BoxCollider3D> ();
    lineCol.transform.position = line.transform.position;
    lineCol.size = new Vector3(5f, 5f,5f);