Hit a line made with LineRenderer by RayCasting

Hey folks,

I wanted to build a panel which should appear when something for example clicks on a line made by LineRenderer.

The first issue I experienced is, that there is somehow no respondence when clicking the target even though it should give me a console output.
I’ve read that I need to set up a mesh for the LineRenderer and then add a collider to make things work, I’ve tried it but it still does not work. Maybe you guys can help me out and tell me what I’m currently doing wrong.

Here’s what I tried:

public void MeshCollider CreatePath()
    {
        List<Vector3> pathPoints = new List<Vector3>();
        GameObject pathObj = new GameObject("pathObj");
        pathObj.AddComponent<LineRenderer>();
        LineRenderer pathRend = pathObj.GetComponent<LineRenderer>();
        MeshCollider meshCollider = pathObj.AddComponent<MeshCollider>();

        pathRend.material = new Material(Shader.Find("Standard"));
        pathRend.material.color = Color.red;
        pathRend.positionCount = 5;

        pathRend.startWidth = 0.08f;
        pathRend.startWidth = 0.08f;

        pathPoints.Add(new Vector3(0, 0.01f, 0));
        pathPoints.Add(new Vector3(0.25f, 0.01f, 0.25f));
        pathPoints.Add(new Vector3(0.25f, 0.01f, 0.5f));
        pathPoints.Add(new Vector3(0.75f, 0.01f, 0.5f));
        pathPoints.Add(new Vector3(0.75f, 0.01f, 1f));

        pathRend.SetPositions(pathPoints.ToArray());
        Mesh meshPath = new Mesh();
        pathRend.BakeMesh(meshPath, true);
        meshCollider.sharedMesh = meshPath;
    }
void Update()
    {
       
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (meshCollider.Raycast(ray, out hit, 100))
            {
                Debug.Log("Clicked on Mesh");
            }
        }
    }

Would be cool if you could help a noob out. :smile:

Cheers

Well right away we know THIS code doesn’t compile, so your posted code isn’t even running.

public void MeshCollider CreatePath()

How to report problems productively in the Unity3D forums:

http://plbm.com/?p=220

Help us to help you.

Hey, that MeshCollider thing in the declaration happened because of copying the code. It’s obviously not like this in my script, my script is running without issues, it just doesn’t do what I intended to do.