Answers for "Collider for Line Renderer?"
http://answers.unity.com/questions/470943/collider-for-line-renderer.html
The latest answers for the question "Collider for Line Renderer?"Answer by bertrand_arnaud
http://answers.unity.com/answers/1621151/view.html
Since Unity 2018.2, you can now use the public method BakeMesh of LineRenderer. This will give you the mesh along the LineRenderer with all the segments. You can then use this mesh with a mesh collider in order to detect Collisions and Raycasts.
LineRenderer lineRenderer = line.GetComponent<LineRenderer>();
MeshCollider meshCollider = line.AddComponent<MeshCollider>();
Mesh mesh = new Mesh();
lineRenderer.BakeMesh(mesh, true);
meshCollider.sharedMesh = mesh;
I tested it and it worked on my project. Let me know what you think about it and if it helps you !Thu, 11 Apr 2019 12:33:47 GMTbertrand_arnaudAnswer by GLeBaTi
http://answers.unity.com/answers/1560764/view.html
You can use EdgeCollider:
var points = new List<Vector3>() { p1, p2, p3, p4 };
_lineRenderer.SetPositions(points.ToArray());
_edgeCollider.points = points.Select(x =>
{
var pos = _edgeCollider.transform.InverseTransformPoint(x);
return new Vector2(pos.x, pos.y);
}).ToArray();Mon, 08 Oct 2018 14:07:34 GMTGLeBaTiAnswer by bugz313
http://answers.unity.com/answers/1375494/view.html
@Immanuel-Scholz You could try this: https://www.assetstore.unity3d.com/en/#!/content/32762Fri, 07 Jul 2017 18:18:14 GMTbugz313Answer by ZackOfAllTrades
http://answers.unity.com/answers/1361987/view.html
Here is a quick tutorial on how I solved this problem.
First a list of what we need, write these down and fill them in:
**startPoint** of the line
**endPoint** of the line
**lineLength** which between these two points
**lineWidth** which you can get from the line renderer by calling YourLineRendererHere.endWidth
**midPoint** which is simply (startPoint + endPoint) / 2
**slope** which recall is (y2 - y1)/(x2-x1)
Thats It!
let's get the slope
y2 = endPoint.z
y1 = startPoint.z
x2= endPoint.x
x1 = startPoint.x
lets plug it in, don't calculate this just leave it
slope = (endPoint.z - startPoint.z)/ (endPoint.x - startPoint.x)
!!! IMPORTANT !!!
In 3d space you use the x and z axis becuase the y is pointing up and we don't care about that.
In 2d space you use the x and y axis, you would also use Vector2 points in your function not Vector 3
Here's your function--
private void AddColliderToLine(LineRenderer line, Vector3 startPoint, Vector3 endPoint)
{
//create the collider for the line
BoxCollider lineCollider = new GameObject("LineCollider").AddComponent<BoxCollider>();
//set the collider as a child of your line
lineCollider.transform.parent = line.transform;
// get width of collider from line
float lineWidth = line.endWidth;
// get the length of the line using the Distance method
float lineLength = Vector3.Distance(startPoint, endPoint);
// size of collider is set where X is length of line, Y is width of line
//z will be how far the collider reaches to the sky
lineCollider.size = new Vector3(lineLength, lineWidth, 1f);
// get the midPoint
Vector3 midPoint = (startPoint + endPoint) / 2;
// move the created collider to the midPoint
lineCollider.transform.position = midPoint;
//heres the beef of the function, Mathf.Atan2 wants the slope, be careful however because it wants it in a weird form
//it will divide for you so just plug in your (y2-y1),(x2,x1)
float angle = Mathf.Atan2((endPoint.z - startPoint.z), (endPoint.x - startPoint.x));
// angle now holds our answer but it's in radians, we want degrees
// Mathf.Rad2Deg is just a constant equal to 57.2958 that we multiply by to change radians to degrees
angle *= Mathf.Rad2Deg;
//were interested in the inverse so multiply by -1
angle *= -1;
// now apply the rotation to the collider's transform, carful where you put the angle variable
// in 3d space you don't wan't to rotate on your y axis
lineCollider.transform.Rotate(0, angle, 0);
}
Thanks for checking the tutorial out!!!
Heres the same thing with pictures: [http://zackgrizzle.ninja/tutorials.html][1]
[1]: http://www.zackgrizzle.ninja/tutorials.htmlTue, 06 Jun 2017 07:54:13 GMTZackOfAllTradesAnswer by Immanuel-Scholz
http://answers.unity.com/answers/471130/view.html
So to answer my own question: There does not seem to be any support for installing own collider types.
But the same result can be somehow achieved by a script component instead that creates dynamically an capsule collider.
The tricky part here is, that you can not retrieve the vectors from the line renderer. Once they are set, they are not back accessible.
Luckily, that wasn't a very big problem for me, since I was updating the line renderer every frame with new start and end positions anyway.
So that's what I used:
// "attaches" a line renderer between two other game objects and also create
// a collider so you can cast raycasts on the line (or listen for collisions)
// only works for single-segment line renderers.
public class AttachLineRenderer : MonoBehaviour
{
public Transform start;
public Transform target;
LineRenderer line;
CapsuleCollider capsule;
public float LineWidth; // use the same as you set in the line renderer.
void Start()
{
line = GetComponent();
capsule = gameObject.AddComponent();
capsule.radius = LineWidth / 2;
capsule.center = Vector3.zero;
capsule.direction = 2; // Z-axis for easier "LookAt" orientation
}
void Update()
{
line.SetPosition(0, start.position);
line.SetPosition(1, target.position);
capsule.transform.position = start.position + (target.position - start.position) / 2;
capsule.transform.LookAt(start.position);
capsule.height = (target.position - start.position).magnitude;
}
}Sat, 08 Jun 2013 16:24:42 GMTImmanuel-Scholz