Drawing a ray through vertices

This is my first unity scripting project I’ve done in a long time so it might as well be my first, therefore the solution may be obvious. I’m trying to code a very simple water prefab that will ray cast from vertex A through vertex B, to tell whether or not its sides have any space. If so, stretch vertex on appropriate side over to the collision point.

Very little of this is implemented right now and isn’t even my problem. Right now I’m struggling with getting the local coordinates of the vertices into worldspace and having a debug ray drawn through them.

This is my code:

    private Vector3[] vertices;
    private Vector3 origin;
    private Vector3 position;
    private Vector3 size;

    private Bounds bounds;

    private Mesh mesh;

    void Start () {
        mesh = GetComponent<MeshFilter>().mesh;
        vertices = mesh.vertices;

        bounds = GetComponent<Renderer>().bounds;
        size = bounds.size;

        for (int i = 1; i < vertices.Length; i++)
        {

            if (i*2-1 > vertices.Length)
                break;

            origin = vertices[i*2-2];
            position = vertices[i*2-1];
           
            position = (Vector3.Scale(position, size) + bounds.extents + bounds.min);
            origin = (Vector3.Scale(origin, size) + bounds.extents + bounds.min);

            //Physics.Raycast(origin, position, 10);
            Debug.DrawRay(origin, position, Color.green,10);
        }
       
    }

And this is all that I get:

I’ve been trying to figure out what went wrong for a while now but hit a dead end. Anyone?

You can convert to world space relative to this transform using transform.TransformPoint(localVertex)

Thank you. This worked for me.

1 Like