Search Unity

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

Discussion in 'Scripting' started by Deleted User, Oct 20, 2014.

  1. Deleted User

    Deleted User

    Guest

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

    Code (CSharp):
    1.                 for (int i = 0; i < 1; i++) {
    2.                         Vector2 start = new Vector2 (collider.bounds.min.x + (collider.bounds.size.x * i), collider.bounds.min.y);
    3.                         for (int j = 0; j < 1; j++) {
    4.                                 Vector2 end = new Vector2 (collider.bounds.min.x, collider.bounds.min.y + (collider.bounds.size.y * j));
    5.                                 RaycastHit2D hit = Physics2D.Linecast (start, end, obstacleLayer);
    6.                                 Debug.DrawLine (start, end, Color.red, 10.0f);
    7.                         }
    8.                 }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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?
     
  3. Deleted User

    Deleted User

    Guest

    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:

    Code (CSharp):
    1.                         for (int i = 0; i < 2; i++) {
    2.                                 Vector2 start = new Vector2 (collider.bounds.min.x, collider.bounds.min.y + (collider.bounds.size.y * i));
    3.                                 RaycastHit2D hit = Physics2D.Raycast (start, Vector2.right, collider.bounds.size.x, obstacleLayer);
    4.                                 if (hit) {
    5.                                 }
    6.                                 Debug.DrawRay (start, new Vector2 (1, 0), Color.red, 10.0f);
    7.                         }
    8.                         for (int i = 0; i < 2; i++) {
    9.                                 Vector2 start = new Vector2 (collider.bounds.min.x + (collider.bounds.size.x * i), collider.bounds.min.y);
    10.                                 RaycastHit2D hit = Physics2D.Raycast (start, Vector2.up, collider.bounds.size.y, obstacleLayer);
    11.                                 Debug.DrawRay (start, new Vector2 (0, 1), Color.white, 10.0f);
    12.                         }
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Code (CSharp):
    1.             Vector2[] corners = new Vector2[] { new Vector2( 1, 1 ), new Vector2( -1, 1 ), new Vector2( -1, -1 ), new Vector2( 1, -1 ) };
    2.             int j = 3;
    3.             for ( int i = 0; i < 4; j = i++ ) {
    4.                 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 );
    5.                 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 );
    6.             }
    Linecast from start to end

    This is a pretty pointless refactoring