for 2D physics, just use an edge collider with the same points. Caveat: The edge collider will always use local coordinates, but you can set the line renderer to use world coordinates.
For 3D physics, the line renderer can bake the mesh with LineRenderer.BakeMesh(). Use that mesh with a mesh collider. This potentially creates a non-convex mesh (also: it’s flat), that won’t collide dynamically. It will, however, answer to raycasts. Another option is to simplify the line and approximate it with overlapping capsule colliders.
I’m trying to get a raycast to work with a mesh collider on a line rendered. I have code with properly bakes a mesh from the line renderer and gives it to the mesh collider. In the scene view I can see the mesh is generated and looks proper. Like you said, this is not a convex collider. But my problem is it’s not answer to raycasts. If I set it to convex it will answer to ray casts but it also just turns the collider into a rectangle which is useless to me. I’m trying to figure out a way around this problem.
they were attached to a rigidbody or something like that
raycasts won’t hit the backface of a triangle so make sure they’re facing the right way
world-space vs. local space: from your answer I assume you already understood this, but you can either bake the mesh in world space or local space.
I just checked and I set it up so the mesh collider and the line renderer are on the same game object. I’m not actually using the Physics.Raycast() directly, but I’m intercepting pointer events with a Physics Raycaster components, which should do exactly the same thing.
This was definitely related to my issue. In my attempts at troubleshooting I got the bright idea to rotate the game object the line renderer was on so it was facing the opposite direction and see if the raycast hit, it did. However this wasn’t a solution, the orientation of the object was important so I had to figure out why the hell the collider was facing the complete opposite direction that I needed it to. I have since solved the issue however this is unfortunately a case where I don’t really know why the issue was solved.
My application instantiated a prefab with a line renderer on it and then set the line renderer’s positions via script. My code to create the mesh collider looks like this:
This works for me now. The reason I don’t really know what solved my issue is that everything I’m doing here I was doing at a point where the mesh was facing the wrong direction. When I brought this code into a different script just to test this it worked and then at some point it started working in the original script. If the order at which I do things here was the issue I don’t know it. Because direction was the issue I believe this had something to do with the Normals of the mesh.
Maybe you had multiple cameras? Usually, the line renderer creates a mesh where each face faces the camera. I don’t know which one is used when there are multiple. Maybe that?