How do i create a mesh on a line renderer in world space?

Hi, I’m able to create a mesh on a Line Renderer through .BakeMesh but the issue I’m having is that the mesh is created in local space, I need it in world space to match the Line Renderer.

Here’s my code:

lineChild = Instantiate(linePrefab, lineManager.startObject.gameObject.transform);

// Create a new LineRenderer component
lineRenderer = lineChild.GetComponent<LineRenderer>();
MeshCollider meshCollider = lineChild.AddComponent<MeshCollider>();

lineRenderer.material = lineMateral;
lineRenderer.startWidth = 0.04f; // Set the starting width of the line
lineRenderer.endWidth = 0.02f; // Set the ending width of the line

// Set the start and end points of the line renderer
lineRenderer.SetPositions(new Vector3[] { lineManager.startObject.transform.position, lineManager.endObject.transform.position });

Mesh mesh = new Mesh();
lineRenderer.BakeMesh(mesh, true);
meshCollider.sharedMesh = mesh;

Vector3[] vertices = mesh.vertices;
for( int i=0 ; i<vertices.Length ; i++ )
{
    vertices[i] = lineRenderer.transform.TransformPoint(vertices[i]);
}
mesh.vertices = vertices;

mesh.RecalculateBounds();
meshCollider.sharedMesh = mesh;

Debug.Log("Line Added");

lineManager.lineIsActive = false;

lineManager.startObject = null;
lineManager.endObject = null;

I’ve tried fixing it using .TransformPoint but that didn’t work, anyone got any ideas?

Here’s a pick of where the mesh is being generated.

[206158-mesh-in-local.jpg|206158]

Hello, my solution to match the LineRenderer with the baked mesh was the other way round:
→ Don’t use World Space for the LineRenderer.

// The baked mesh doesn't use World Space
// and would be offset if this is set to true
lineRenderer.useWorldSpace = false;

Hope this helps.

I solved it this way: In case anyone is stuck on this threat as I was:

  1. Use LineRenderer.Bake(yourMesh) or LineRenderer.Bake(yourMesh, yourCamera, useTransformBool) as you normally would. This will result in world space coordinates that seem offset at first.

  2. Create a Vector3 Array with all the resulting vertices of the freshly created mesh.

  3. Loop through all vertices and offset them by the offset value you need (for example the parents localPosition)

  4. Feed the vertices back into the mesh

// create mesh for this line renderer
Mesh lrMesh = new Mesh();
// create (add) a mesh collider for the resulting mesh
MeshCollider lrMeshCollider = gameObject.AddComponent<MeshCollider>();
 
// 'Bake' the line renderer mesh – which results in world space coordinates
wpConnectionLineRenderer.BakeMesh(lrMesh, Camera.main, true);
 
// Get the vertices of the freshly created mesh
Vector3[] vertices = lrMesh.vertices;
 
// Apply the offset by subtracting the offset (here:  parent's local position) from each vertex position
for (int i = 0; i < vertices.Length; i++)
{
    vertices[i] -= transform.parent.localPosition;
}
 
// Assign the adjusted vertices back to the mesh
lrMesh.vertices = vertices;
 
// Set the shared mesh of the mesh collider
lrMeshCollider.sharedMesh = lrMesh;

Hope this helps :grinning: