create mesh from a 2D line

I am a beginner with vertice and mesh.
I want to get a user drawing, it will be a line/spline in one surface (2D) (X,Z) .
I think I could do this with a line renderer, maybe.

From this line, (i see with line render, i have an array with point) generate a mesh in one direction (for example 2 axis).
And then add a collider and texture to this mesh.

At the end it will be like a maze create by the user and then he could move in.

Have you some idea of the different step I have to do ?

heres one example using linerenderer and edgecollider,

but you could also manually generate mesh yourself too, and then add mesh collider to it,
even easier if you are only moving in straight lines (basically in grid)

1 Like

Hi @mgear ,
I checkout this code, it’s a great example for how I have to do to add a collider.

What I want to do and don’t know how to do is :

  • draw a line with mouse : this is normally ok with line renderer
  • get all this point that represent the line : I saw the array in the inspector, if think it should be lineRenderer.GetPosition fonction
    -create a mesh from these points in one direction ( if the lien was in (x,y) plane, i want to create an mesh along z axis) : don’t know how to do this
  • add a collider to this mesh : seems to be like the code you send to me .

Now I have that, but I don’t know how to close the loop/ see image where I just draw a circle

 void Start()
    {
        Debug.Log("message");
        m_Points = new List<Vector2> (); 
        player.AddComponent<LineRenderer>();
        m_LineRenderer = player.GetComponent<LineRenderer>();
        vertices = new List<Vector3> ();
        triangles = new List<int> ();
        mesh= mesher.GetComponent<MeshFilter>().mesh;
        loop=0;

    }
    void Update()
    {
        if ( Input.GetMouseButton ( 0 ) )
        {
            Vector3 mousePosition = Camera.main.ScreenToWorldPoint ( Input.mousePosition );

            if ( !m_Points.Contains ( mousePosition ) )
            {
                m_Points.Add ( mousePosition );
                m_LineRenderer.positionCount = m_Points.Count;
                m_LineRenderer.SetPosition ( m_LineRenderer.positionCount - 1, mousePosition );

                float size = 10;
                point0 = new Vector3(
                        m_LineRenderer.GetPosition(m_LineRenderer.positionCount - 2).x ,
                        m_LineRenderer.GetPosition(m_LineRenderer.positionCount - 2).y ,
                        m_LineRenderer.GetPosition(m_LineRenderer.positionCount - 2).z
                        );
                point1 =new Vector3(
                        m_LineRenderer.GetPosition(m_LineRenderer.positionCount - 2).x ,
                        m_LineRenderer.GetPosition(m_LineRenderer.positionCount - 2).y ,
                        m_LineRenderer.GetPosition(m_LineRenderer.positionCount - 2).z + size
                        );
                point2 =new Vector3(
                        mousePosition.x ,
                        mousePosition.y ,
                        mousePosition.z + size
                        );
                point3 =new Vector3(
                        mousePosition.x ,
                        mousePosition.y ,
                        mousePosition.z
                        );

                if(  m_LineRenderer.positionCount >= 2 ){
                MakeMesh(point0,point1,point2,point3);
                }
            }
        }
    }


    void MakeMesh (Vector3 point0,Vector3 point1,Vector3 point2, Vector3 point3){
        vertices.Add(point0);
        vertices.Add(point1);
        vertices.Add(point2);
        vertices.Add(point3);
        triangles.Add(0+loop*4);
        triangles.Add(2+loop*4);
        triangles.Add(1+loop*4);
        triangles.Add(0+loop*4);
        triangles.Add(3+loop*4);
        triangles.Add(2+loop*4);
        loop = loop + 1;
        mesh.vertices=vertices.ToArray();
        mesh.triangles=triangles.ToArray();
    }
}

And how to add a collider to each mesh…

4015141--346855--upload_2018-12-19_9-58-1.png
4015141--346858--upload_2018-12-19_9-57-47.png

Ok now I have add a collider with share mesh of a meshCollider and it work.
But I still have trouble to close the loop…

Could you please not to make duplicates of your posts? If someone have ideas they will share with you in your original post.
Occasionally, and if you have any substantial things to add, you can comment on your original post and it will go back up in the list of posts. It’s against the forum rules to open more than one thread on one subject.