[SOLVED] How to create a square using Linecast & loops?

I’m trying to draw a square using loops but no success so far.
This is one of my many attempts:

                for (int i = 0; i < 1; i++) {
                        Vector2 start = new Vector2 (collider.bounds.min.x + (collider.bounds.size.x * i), collider.bounds.min.y);
                        for (int j = 0; j < 1; j++) {
                                Vector2 end = new Vector2 (collider.bounds.min.x, collider.bounds.min.y + (collider.bounds.size.y * j));
                                RaycastHit2D hit = Physics2D.Linecast (start, end, obstacleLayer);
                                Debug.DrawLine (start, end, Color.red, 10.0f);
                        }
                }

Yeah, drawing a square using a loop would be a real pain in the neck, because there is no way to index over or compute the four corners of the square.

I’d just use four individual DrawLine statements, not in a loop. (Possibly with a helper method to factor out any repeated code.)

Of course I don’t understand what you’re raycasting for. Perhaps if you explain a bit more about what you’re trying to accomplish?

I’m trying to check for collision around a specific area of an object which already has a collider and I don’t want to add a child to it.
I changed my script to this and it works but I’m wondering if it can be improved:

                        for (int i = 0; i < 2; i++) {
                                Vector2 start = new Vector2 (collider.bounds.min.x, collider.bounds.min.y + (collider.bounds.size.y * i));
                                RaycastHit2D hit = Physics2D.Raycast (start, Vector2.right, collider.bounds.size.x, obstacleLayer);
                                if (hit) {
                                }
                                Debug.DrawRay (start, new Vector2 (1, 0), Color.red, 10.0f);
                        }
                        for (int i = 0; i < 2; i++) {
                                Vector2 start = new Vector2 (collider.bounds.min.x + (collider.bounds.size.x * i), collider.bounds.min.y);
                                RaycastHit2D hit = Physics2D.Raycast (start, Vector2.up, collider.bounds.size.y, obstacleLayer);
                                Debug.DrawRay (start, new Vector2 (0, 1), Color.white, 10.0f);
                        }
            Vector2[] corners = new Vector2[] { new Vector2( 1, 1 ), new Vector2( -1, 1 ), new Vector2( -1, -1 ), new Vector2( 1, -1 ) };
            int j = 3;
            for ( int i = 0; i < 4; j = i++ ) {
                Vector2 start = new Vector2( collider.bounds.center.x + collider.bounds.extents.x * corners[j].x, collider.bounds.center.y + collider.bounds.extents.y * corners[j].y );
                Vector2 end = new Vector2( collider.bounds.center.x + collider.bounds.extents.x * corners[i].x, collider.bounds.center.y + collider.bounds.extents.y * corners[i].y );
            }

Linecast from start to end

This is a pretty pointless refactoring